private static async Task Predict(string fileName) { using (Image <Rgba32> image = Image.Load(fileName)) { var preprocessor = new Preprocessor(Rgba32.White, Rgba32.Black); var i = preprocessor.Preprocess(image); i.Save("result2-7.png", new PngEncoder()); var pixels = Preprocessor.ConvertImageToArray(i); //Console.WriteLine(JsonConvert.SerializeObject(pixels)); for (int j = 0; j < 784; j++) { Console.Write(pixels[j].ToString("D3")); if ((j + 1) % 28 == 0) { Console.WriteLine(); } } var recognizer = new MLStudioDigitRecognizer("API_URL", "API_KEY"); var prediction = await recognizer.PredictAsync(i); Console.WriteLine($"\n\n\nThis is a(n) {prediction.Tag}! (I'm {prediction.Probability*100}% sure.)\n\n\n"); } }
/// <summary> /// Handle attachments uploaded by users. The bot receives an <see cref="Attachment"/> in an <see cref="Activity"/>. /// The activity has a <see cref="IList{T}"/> of attachments. /// </summary> /// <remarks> /// Not all channels allow users to upload files. Some channels have restrictions /// on file type, size, and other attributes. Consult the documentation for the channel for /// more information. For example Skype's limits are here /// <see ref="https://support.skype.com/en/faq/FA34644/skype-file-sharing-file-types-size-and-time-limits"/>. /// </remarks> private async Task HandleIncomingAttachmentAsync(DialogContext dc, IMessageActivity activity) { foreach (var file in activity.Attachments) { if (file.ContentType != "image/png" && file.ContentType != "image/jpeg") { await dc.Context.SendActivityAsync("Sorry, I cannot process images other than png/jpeg."); } // Download the actual attachment using (var client = new HttpClient()) { var stream = await client.GetStreamAsync(file.ContentUrl); var memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream); var byteArray = memoryStream.ToArray(); //var recognizer = new CustomVisionDigitRecognizer( // _configuration["CustomVisionBaseUrl"], // _configuration["CustomVisionProjectId"], // _configuration["CustomVisionPublishedName"], // _configuration["CustomVisionApiKey"]); var recognizer = new MLStudioDigitRecognizer( _configuration["MLStudioApiUrl"], _configuration["MLStudioApiKey"]); var prediction = await recognizer.PredictAsync(byteArray); await SendPredictionAnswer(dc, prediction.Tag, prediction.Probability); } } }
public async Task PredictWithMLStudioAsync_ReturnsPredictionAsync() { var fileStream = new FileStream("test2.jpg", FileMode.Open, FileAccess.Read); var memoryStream = new MemoryStream(); await fileStream.CopyToAsync(memoryStream); var byteArray = memoryStream.ToArray(); var recognizer = new MLStudioDigitRecognizer( _mlStudioApiUrl, _mlStudioApiKey); var prediction = await recognizer.PredictAsync(byteArray); Assert.Equal(5, prediction.Tag); }