private void onDownloadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { Stopwatch watch = Stopwatch.StartNew(); var result = _mobilenet.Recognize(_imageFiles[0], 0.5f); watch.Stop(); NativeImageIO.Annotation[] annotations = new NativeImageIO.Annotation[result.Length]; for (int i = 0; i < result.Length; i++) { NativeImageIO.Annotation annotation = new NativeImageIO.Annotation(); annotation.Rectangle = result[i].Rectangle; annotation.Label = String.Format("{0}:({1:0.00}%)", result[i].Label, result[i].Score * 100); annotations[i] = annotation; } NativeImageIO.JpegData jpeg = NativeImageIO.ImageFileToJpeg(_imageFiles[0], annotations); //NativeImageIO.JpegData jpeg = NativeImageIO.ImageFileToJpeg(_imageFiles[0]); //String names = String.Join(";", Array.ConvertAll(result, r => r.Label)); SetImage(jpeg.Raw, jpeg.Width, jpeg.Height); String resStr = String.Format("Detected {1} objects in {0} milliseconds.", watch.ElapsedMilliseconds, result.Length); SetMessage(resStr); }
private static NativeImageIO.Annotation[] GetAnnotations(CocoSsdMobilenet.RecognitionResult[] result) { NativeImageIO.Annotation[] annotations = new NativeImageIO.Annotation[result.Length]; for (int i = 0; i < result.Length; i++) { NativeImageIO.Annotation annotation = new NativeImageIO.Annotation(); annotation.Rectangle = result[i].Rectangle; annotation.Label = String.Format("{0}:({1:0.00}%)", result[i].Label, result[i].Score * 100); annotations[i] = annotation; } return(annotations); }
public static NativeImageIO.Annotation[] FilterResults(MultiboxGraph.Result[] results, float scoreThreshold) { List <NativeImageIO.Annotation> goodResults = new List <NativeImageIO.Annotation>(); for (int i = 0; i < results.Length; i++) { if (results[i].Scores > scoreThreshold) { NativeImageIO.Annotation r = new NativeImageIO.Annotation(); r.Rectangle = results[i].DecodedLocations; r.Label = String.Empty; //r.Label = String.Format("{0:0.00}%", results[i].Scores * 100); goodResults.Add(r); } } return(goodResults.ToArray()); }
private void RecognizeAndUpdateText(Texture2D texture) { if (_mobilenet == null) { _displayMessage = "Waiting for mobile net model to be loaded..."; return; } Stopwatch watch = Stopwatch.StartNew(); CocoSsdMobilenet.RecognitionResult[] results = _mobilenet.Recognize(texture, true, false, 0.5f); watch.Stop(); if (drawableTexture == null || drawableTexture.width != texture.width || drawableTexture.height != texture.height) { drawableTexture = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false); } drawableTexture.SetPixels(texture.GetPixels()); NativeImageIO.Annotation[] annotations = new NativeImageIO.Annotation[results.Length]; for (int i = 0; i < results.Length; i++) { NativeImageIO.Annotation annotation = new NativeImageIO.Annotation(); annotation.Rectangle = results[i].Rectangle; annotation.Label = String.Format("{0}:({1:0.00}%)", results[i].Label, results[i].Score * 100); annotations[i] = annotation; } String objectNames = String.Empty; foreach (NativeImageIO.Annotation annotation in annotations) { float left = annotation.Rectangle[0] * drawableTexture.width; float top = annotation.Rectangle[1] * drawableTexture.height; float right = annotation.Rectangle[2] * drawableTexture.width; float bottom = annotation.Rectangle[3] * drawableTexture.height; Rect scaledLocation = new Rect(left, top, right - left, bottom - top); scaledLocation.y = texture.height - scaledLocation.y; scaledLocation.height = -scaledLocation.height; NativeImageIO.DrawRect(drawableTexture, scaledLocation, Color.red); objectNames = objectNames + annotation.Label + ";"; } drawableTexture.Apply(); //MultiboxGraph.DrawResults(drawableTexture, results, 0.2f, true); if (!String.IsNullOrEmpty(objectNames)) { objectNames = String.Format("({0})", objectNames); } String resStr = String.Empty; if (results != null) { resStr = String.Format("{0} objects detected{1}. Recognition completed in {2} milliseconds.", annotations.Length, objectNames, watch.ElapsedMilliseconds); //resStr = String.Format("Object is {0} with {1}% probability. Recognition completed in {2} milliseconds.", results[0].Label, results[0].Probability * 100, watch.ElapsedMilliseconds); } _displayMessage = resStr; }