private Annotation[] Recognize(Texture texture) { CocoSsdMobilenet.RecognitionResult[] results = _mobilenet.Recognize(texture, true, false, 0.5f); if (results == null) { return(null); } Annotation[] annotations = new Annotation[results.Length]; for (int i = 0; i < results.Length; i++) { Annotation annotation = new Annotation(); annotation.Rectangle = results[i].Rectangle; annotation.Label = String.Format("{0}:({1:0.00}%)", results[i].Label, results[i].Score * 100); annotations[i] = annotation; } return(annotations); }
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()); Annotation[] annotations = new Annotation[results.Length]; for (int i = 0; i < results.Length; i++) { Annotation annotation = new 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 (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; }