public byte[] DrawResultsToJpeg(String fileName, MultiboxGraph.Result detectResult, float scoreThreshold = 0.2f) { #if __ANDROID__ BitmapFactory.Options options = new BitmapFactory.Options(); options.InMutable = true; Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options); MultiboxGraph.DrawResults(bmp, detectResult, scoreThreshold); using (MemoryStream ms = new MemoryStream()) { bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms); return(ms.ToArray()); } #elif __MACOS__ NSImage img = NSImage.ImageNamed(fileName); MultiboxGraph.DrawResults(img, detectResult, scoreThreshold); var imageData = img.AsTiff(); var imageRep = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep; var jpegData = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); return(jpeg); #elif __IOS__ UIImage uiimage = new UIImage(fileName); UIImage newImg = MultiboxGraph.DrawResults(uiimage, detectResult, scoreThreshold); var jpegData = newImg.AsJPEG(); byte[] jpeg = new byte[jpegData.Length]; System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length); return(jpeg); #else throw new Exception("Not implemented"); #endif }
public Result[] Detect(Tensor imageResults) { if (_graph == null) { throw new NullReferenceException("The multibox graph has not been initialized. Please call the Init function first."); } Tensor[] finalTensor = _session.Run(new Output[] { _graph["ResizeBilinear"] }, new Tensor[] { imageResults }, new Output[] { _graph["output_scores/Reshape"], _graph["output_locations/Reshape"] }); int labelCount = finalTensor[0].Dim[1]; Tensor[] topK = GetTopDetections(finalTensor[0], labelCount); float[] encodedScores = topK[0].Flat <float>(); float[] encodedLocations = finalTensor[1].Flat <float>(); int[] indices = topK[1].Flat <int>(); float[] scores = DecodeScoresEncoding(encodedScores); Result[] results = new Result[indices.Length]; float[][] locations = MultiboxGraph.DecodeLocationsEncoding(encodedLocations, _boxPriors); for (int i = 0; i < indices.Length; i++) { results[i] = new Result(); results[i].Scores = scores[i]; results[i].DecodedLocations = locations[indices[i]]; } return(results); }
public Result[] Detect(Tensor imageResults) { Tensor[] finalTensor = _session.Run(new Output[] { _graph["ResizeBilinear"] }, new Tensor[] { imageResults }, new Output[] { _graph["output_scores/Reshape"], _graph["output_locations/Reshape"] }); int labelCount = finalTensor[0].Dim[1]; Tensor[] topK = GetTopDetections(finalTensor[0], labelCount); float[] encodedScores = topK[0].Flat <float>(); float[] encodedLocations = finalTensor[1].Flat <float>(); int[] indices = topK[1].Flat <int>(); float[] scores = DecodeScoresEncoding(encodedScores); Result[] results = new Result[indices.Length]; float[][] locations = MultiboxGraph.DecodeLocationsEncoding(encodedLocations, _boxPriors); for (int i = 0; i < indices.Length; i++) { results[i] = new Result(); results[i].Scores = scores[i]; results[i].DecodedLocations = locations[indices[i]]; } return(results); }
public Result Detect(Tensor imageResults) { Session multiboxSession = new Session(this); Tensor[] finalTensor = multiboxSession.Run(new Output[] { this["ResizeBilinear"] }, new Tensor[] { imageResults }, new Output[] { this["output_scores/Reshape"], this["output_locations/Reshape"] }); int labelCount = finalTensor[0].Dim[1]; Tensor[] topK = GetTopDetections(finalTensor, labelCount); float[] encodedScores = topK[0].Flat <float>(); float[] encodedLocations = finalTensor[1].Flat <float>(); #if __ANDROID__ float[] boxPriors = ReadBoxPriors(System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads, _modelFiles[1])); #else float[] boxPriors = ReadBoxPriors(_modelFiles[1]); #endif Result result = new Result(); result.Scores = DecodeScoresEncoding(encodedScores); result.Indices = topK[1].Flat <int>(); result.DecodedLocations = MultiboxGraph.DecodeLocationsEncoding(encodedLocations, boxPriors); return(result); }
public Result Detect(Tensor imageResults) { Tensor[] finalTensor = _session.Run(new Output[] { _graph["ResizeBilinear"] }, new Tensor[] { imageResults }, new Output[] { _graph["output_scores/Reshape"], _graph["output_locations/Reshape"] }); int labelCount = finalTensor[0].Dim[1]; Tensor[] topK = GetTopDetections(finalTensor, labelCount); float[] encodedScores = topK[0].Flat <float>(); float[] encodedLocations = finalTensor[1].Flat <float>(); Result result = new Result(); result.Scores = DecodeScoresEncoding(encodedScores); result.Indices = topK[1].Flat <int>(); result.DecodedLocations = MultiboxGraph.DecodeLocationsEncoding(encodedLocations, _boxPriors); return(result); }
public Result Detect(Tensor imageResults, SessionOptions sessionOptions = null) { Session multiboxSession = new Session(_graph, sessionOptions); Tensor[] finalTensor = multiboxSession.Run(new Output[] { _graph["ResizeBilinear"] }, new Tensor[] { imageResults }, new Output[] { _graph["output_scores/Reshape"], _graph["output_locations/Reshape"] }); int labelCount = finalTensor[0].Dim[1]; Tensor[] topK = GetTopDetections(finalTensor, labelCount); float[] encodedScores = topK[0].Flat <float>(); float[] encodedLocations = finalTensor[1].Flat <float>(); float[] boxPriors = ReadBoxPriors(_downloadManager.Files[1].LocalFile); Result result = new Result(); result.Scores = DecodeScoresEncoding(encodedScores); result.Indices = topK[1].Flat <int>(); result.DecodedLocations = MultiboxGraph.DecodeLocationsEncoding(encodedLocations, boxPriors); return(result); }
public Result Detect(Tensor imageResults) { Session multiboxSession = new Session(this); Tensor[] finalTensor = multiboxSession.Run(new Output[] { this["ResizeBilinear"] }, new Tensor[] { imageResults }, new Output[] { this["output_scores/Reshape"], this["output_locations/Reshape"] }); int labelCount = finalTensor[0].Dim[1]; Tensor[] topK = GetTopDetections(finalTensor, labelCount); float[] encodedScores = topK[0].Flat <float>(); float[] encodedLocations = finalTensor[1].Flat <float>(); float[] boxPriors = ReadBoxPriors(GetLocalFileName(_modelFiles[1])); Result result = new Result(); result.Scores = DecodeScoresEncoding(encodedScores); result.Indices = topK[1].Flat <int>(); result.DecodedLocations = MultiboxGraph.DecodeLocationsEncoding(encodedLocations, boxPriors); return(result); }