private void OnClassifyImage(object sender, EventArgs e) { try { _loggingService.Log("Classify drawing has started"); var imagePreprocessor = new ImagePreprocessor(); double[] pixels = imagePreprocessor.Preprocess(_uploadImageView.Image); IPredictionModel predictionModel = Global.PredictionModel; double[] prediction = predictionModel.Predict(pixels); _uploadImageView.ProcessPrediction(prediction); _loggingService.Log("Classify drawing has completed"); } catch (NullReferenceException exception) { _loggingService.Log(exception); _messageService.ShowMessage("No image was uploaded. Please upload an image and try again.", "Upload error", icon: MessageBoxIcon.Information); } catch (Exception exception) { _loggingService.Log(exception); _messageService.ShowMessage("An error ocurred while classyfing the drawing. Please try again.", "Classification error", icon: MessageBoxIcon.Information); } }
public async Task <Prediction> PredictAsync(Image <Rgba32> image) { var preprocessor = new ImagePreprocessor(Rgba32.White, Rgba32.Black); image = preprocessor.Preprocess(image); var inputs = PrepareMLStudioInput(image); using (var client = new HttpClient()) { var requestContent = new StringContent(inputs); requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey); var response = await client.PostAsync(_apiUrl, requestContent); var responseContent = response.Content is HttpContent c ? await c.ReadAsStringAsync() : null; var prediction = JsonConvert.DeserializeObject <MLStudioResponseObject>(responseContent); var tag = prediction.results.WebServiceOutput0.FirstOrDefault()?["Scored Labels"]; return(new Prediction { Tag = Convert.ToInt32(tag), Probability = 1 }); } }
private void OnClassifyDrawing(object sender, EventArgs e) { try { _loggingService.Log("Classify drawing has started"); var imagePreprocessor = new ImagePreprocessor(); IPredictionModel predictionModel = Global.PredictionModel; Image img = _slidingWindowView.Drawing; foreach (Size windowSize in WindowSizes) { foreach (BoundingBox boundingBox in ImageUtilities.SlidingWindow(img, windowSize, 112)) { try { double[] pixels = imagePreprocessor.Preprocess(boundingBox.Image); double[] prediction = predictionModel.Predict(pixels); // If classification is over 99% draw a bounding box at this location int predicted = prediction.ArgMax(); double predictedAccuracy = prediction[prediction.ArgMax()]; if (predictedAccuracy >= 0.95) { _slidingWindowView.DrawBoundingBox(boundingBox, predicted, predictedAccuracy); } } catch (Exception exception) { _loggingService.Log(exception); } } } _loggingService.Log("Classify drawing has completed"); } catch (Exception exception) { _loggingService.Log(exception); _messageService.ShowMessage("An error ocurred while classyfing the drawing. Please try again.", "Classification error", icon: MessageBoxIcon.Information); } }