diff --git a/ImageClassifier/ImageClassifier.licenseheader b/ImageClassifier/ImageClassifier.licenseheader new file mode 100644 index 0000000..841dbe6 --- /dev/null +++ b/ImageClassifier/ImageClassifier.licenseheader @@ -0,0 +1,5 @@ +extensions: designer.cs generated.cs +extensions: .cs +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. diff --git a/ImageClassifier/IntelligentAPI.ImageClassifier.0.0.16 - Copy.zip b/ImageClassifier/IntelligentAPI.ImageClassifier.0.0.16.zip similarity index 100% rename from ImageClassifier/IntelligentAPI.ImageClassifier.0.0.16 - Copy.zip rename to ImageClassifier/IntelligentAPI.ImageClassifier.0.0.16.zip diff --git a/ImageClassifier/IntelligentAPI_ImageClassifier.csproj b/ImageClassifier/IntelligentAPI_ImageClassifier.csproj index dc4e869..6970b5f 100644 --- a/ImageClassifier/IntelligentAPI_ImageClassifier.csproj +++ b/ImageClassifier/IntelligentAPI_ImageClassifier.csproj @@ -132,11 +132,17 @@ 13.0.1 + + 5.0.2 + + + + 14.0 diff --git a/ImageClassifier/IntelligentAPI_ImageClassifier.nuspec b/ImageClassifier/IntelligentAPI_ImageClassifier.nuspec index 7a123c4..6f9fe0d 100644 --- a/ImageClassifier/IntelligentAPI_ImageClassifier.nuspec +++ b/ImageClassifier/IntelligentAPI_ImageClassifier.nuspec @@ -1,8 +1,8 @@ - IntelligentAPI.ImageClassifier - 0.0.16 + IntelligentAPI_ImageClassifier + 0.0.17 Cool nuget package for ML stuff Amrutha Srinivasan false @@ -11,6 +11,7 @@ http://icon_url_here_or_delete_this_line/ Nuget package that exposes APIs to perform image classification Cool nuget package for ML stuff + diff --git a/ImageClassifier/Properties/AssemblyInfo.cs b/ImageClassifier/Properties/AssemblyInfo.cs index ed68b1e..85915c7 100644 --- a/ImageClassifier/Properties/AssemblyInfo.cs +++ b/ImageClassifier/Properties/AssemblyInfo.cs @@ -1,4 +1,8 @@ -using System.Reflection; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; diff --git a/ImageClassifier/SqueezeNetImageClassifier.cs b/ImageClassifier/SqueezeNetImageClassifier.cs index 4e87411..e8fee42 100644 --- a/ImageClassifier/SqueezeNetImageClassifier.cs +++ b/ImageClassifier/SqueezeNetImageClassifier.cs @@ -1,4 +1,7 @@ -using Newtonsoft.Json; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + using System; using System.Collections.Generic; using System.IO; @@ -12,6 +15,8 @@ using Windows.Media; using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Xaml.Media.Imaging; +using System.Text.Json; + namespace IntelligentAPI.ImageClassification { @@ -19,8 +24,8 @@ namespace IntelligentAPI.ImageClassification public class SqueezeNetImageClassifier { - private const string _kModelFileName = "model.onnx"; - private const string _kLabelsFileName = "Labels.json"; + private const string _modelFileName = "model.onnx"; + private const string _labelsFileName = "Labels.json"; private LearningModel _model = null; private LearningModelSession _session; private List _labels = new List(); @@ -30,17 +35,53 @@ namespace IntelligentAPI.ImageClassification private SqueezeNetImageClassifier() { } - public static async Task> ClassifyImage(StorageFile selectedStorageFile, int top) + public static async Task> ClassifyImage(StorageFile selectedStorageFile, int top=3) + { + CreateInstanceIfNone(); + SoftwareBitmap softwareBitmap = await GenerateSoftwareBitmapFromStorageFile(selectedStorageFile); + VideoFrame videoFrame = await GenerateVideoFrameFromBitmap(softwareBitmap); + return await instance.EvaluateModel(videoFrame, top); + } + + public static async Task> ClassifyImage(SoftwareBitmap softwareBitmap, int top=3) + { + CreateInstanceIfNone(); + VideoFrame videoFrame = await GenerateVideoFrameFromBitmap(softwareBitmap); + return await instance.EvaluateModel(videoFrame, top); + } + + public static async Task> ClassifyImage(VideoFrame videoFrame, int top=3) + { + CreateInstanceIfNone(); + return await instance.EvaluateModel(videoFrame, top); + } + + private static void CreateInstanceIfNone() { if (instance == null) { instance = new SqueezeNetImageClassifier(); } - return await instance.EvaluateModel(selectedStorageFile, top); } - public async Task> EvaluateModel(StorageFile selectedStorageFile, int top) + + public async Task> EvaluateModel(VideoFrame inputImage, int top) { await LoadModelAsync(); + return await EvaluateVideoFrameAsync(inputImage, top); + } + + private static async Task GenerateVideoFrameFromBitmap(SoftwareBitmap softwareBitmap) + { + SoftwareBitmapSource imageSource = new SoftwareBitmapSource(); + await imageSource.SetBitmapAsync(softwareBitmap); + + // Encapsulate the image within a VideoFrame to be bound and evaluated + VideoFrame videoFrame = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); + return videoFrame; + } + + private static async Task GenerateSoftwareBitmapFromStorageFile(StorageFile selectedStorageFile) + { SoftwareBitmap softwareBitmap; using (IRandomAccessStream stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read)) { @@ -52,14 +93,7 @@ namespace IntelligentAPI.ImageClassification softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } - // Display the image - SoftwareBitmapSource imageSource = new SoftwareBitmapSource(); - await imageSource.SetBitmapAsync(softwareBitmap); - - // Encapsulate the image within a VideoFrame to be bound and evaluated - VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); - - return await EvaluateVideoFrameAsync(inputImage, top); + return softwareBitmap; } private async Task LoadModelAsync() @@ -71,11 +105,11 @@ namespace IntelligentAPI.ImageClassification { // Parse labels from label json file. We know the file's // entries are already sorted in order. - var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///IntelligentAPI_ImageClassifier/Assets/Labels.json")); + var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///IntelligentAPI_ImageClassifier/Assets/" + _labelsFileName)); var fileString = await FileIO.ReadTextAsync(file); - var fileDict = JsonConvert.DeserializeObject>(fileString); + var fileDict = JsonSerializer.Deserialize>(fileString); foreach (var kvp in fileDict) { @@ -83,7 +117,7 @@ namespace IntelligentAPI.ImageClassification } - var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///IntelligentAPI_ImageClassifier/Assets/model.onnx")); + var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///IntelligentAPI_ImageClassifier/Assets/" + _modelFileName)); _model = await LearningModel.LoadFromStorageFileAsync(modelFile); // Create the evaluation session with the model and device @@ -167,7 +201,7 @@ namespace IntelligentAPI.ImageClassification } catch (Exception ex) { - + throw ex; } } diff --git a/ImageClassifier/model.cs b/ImageClassifier/model.cs index 5552e6e..371afc9 100644 --- a/ImageClassifier/model.cs +++ b/ImageClassifier/model.cs @@ -1,3 +1,7 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + // This file was automatically generated by VS extension Windows Machine Learning Code Generator v3 // from model file model.onnx // Warning: This file may get overwritten if you add add an onnx file with the same name diff --git a/ImageClassifier/nuget.config b/ImageClassifier/nuget.config new file mode 100644 index 0000000..4169d2f --- /dev/null +++ b/ImageClassifier/nuget.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/IntelligentLabs.sln b/IntelligentLabs.sln index 94d8d97..a6d1547 100644 --- a/IntelligentLabs.sln +++ b/IntelligentLabs.sln @@ -64,6 +64,8 @@ Global {F705B499-D0B5-408C-B1D1-D3F379D0FCD6}.Release|x86.ActiveCfg = Release|x86 {F705B499-D0B5-408C-B1D1-D3F379D0FCD6}.Release|x86.Build.0 = Release|x86 {47D87733-B357-4706-88BD-211FB7D8679D}.Debug|Any CPU.ActiveCfg = Debug|x86 + {47D87733-B357-4706-88BD-211FB7D8679D}.Debug|Any CPU.Build.0 = Debug|x86 + {47D87733-B357-4706-88BD-211FB7D8679D}.Debug|Any CPU.Deploy.0 = Debug|x86 {47D87733-B357-4706-88BD-211FB7D8679D}.Debug|ARM.ActiveCfg = Debug|ARM {47D87733-B357-4706-88BD-211FB7D8679D}.Debug|ARM.Build.0 = Debug|ARM {47D87733-B357-4706-88BD-211FB7D8679D}.Debug|ARM.Deploy.0 = Debug|ARM diff --git a/Nuget Packages/nuget.config b/Nuget Packages/nuget.config new file mode 100644 index 0000000..9cead7a --- /dev/null +++ b/Nuget Packages/nuget.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ObjectDetector/IntelligentAPI_ObjectDetector.csproj b/ObjectDetector/IntelligentAPI_ObjectDetector.csproj index 172d92b..dbe944e 100644 --- a/ObjectDetector/IntelligentAPI_ObjectDetector.csproj +++ b/ObjectDetector/IntelligentAPI_ObjectDetector.csproj @@ -136,6 +136,7 @@ + 14.0 diff --git a/ObjectDetector/IntelligentAPI_ObjectDetector.nuspec b/ObjectDetector/IntelligentAPI_ObjectDetector.nuspec index 45c6550..27fde88 100644 --- a/ObjectDetector/IntelligentAPI_ObjectDetector.nuspec +++ b/ObjectDetector/IntelligentAPI_ObjectDetector.nuspec @@ -9,7 +9,7 @@ MIT Nuget package for real time object detection Summary of changes made in this release of the package. - Tag1 Tag2 + diff --git a/ObjectDetector/ObjectDetector.licenseheader b/ObjectDetector/ObjectDetector.licenseheader new file mode 100644 index 0000000..841dbe6 --- /dev/null +++ b/ObjectDetector/ObjectDetector.licenseheader @@ -0,0 +1,5 @@ +extensions: designer.cs generated.cs +extensions: .cs +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. diff --git a/ObjectDetector/Properties/AssemblyInfo.cs b/ObjectDetector/Properties/AssemblyInfo.cs index a7c94d6..e4e3f47 100644 --- a/ObjectDetector/Properties/AssemblyInfo.cs +++ b/ObjectDetector/Properties/AssemblyInfo.cs @@ -1,4 +1,8 @@ -using System.Reflection; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; diff --git a/ObjectDetector/YOLOObjectDetector.cs b/ObjectDetector/YOLOObjectDetector.cs index ee32c2d..b2211e1 100644 --- a/ObjectDetector/YOLOObjectDetector.cs +++ b/ObjectDetector/YOLOObjectDetector.cs @@ -1,5 +1,8 @@ -using System; +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -111,18 +114,25 @@ namespace IntelligentAPI.ObjectDetection public static async Task> DetectObjects(StorageFile file) { - VideoFrame inputImage = await convertToVideoFrame(file); - return await DetectObjects(inputImage); + SoftwareBitmap bitmap = await GenerateSoftwareBitmapFromStorageFile(file); + VideoFrame videoFrame = await GenerateVideoFrameFromBitmap(bitmap); + return await DetectObjects(videoFrame); } - public static async Task> DetectObjects(VideoFrame file) + public static async Task> DetectObjects(SoftwareBitmap bitmap) + { + VideoFrame videoFrame = await GenerateVideoFrameFromBitmap(bitmap); + return await DetectObjects(videoFrame); + } + + public static async Task> DetectObjects(VideoFrame videoFrame) { if (instance == null) { instance = new YOLOObjectDetector(); } - return await instance.EvaluateFrame(file); + return await instance.EvaluateFrame(videoFrame); } private async Task InitModelAsync() @@ -150,6 +160,24 @@ namespace IntelligentAPI.ObjectDetection { await InitModelAsync(); SoftwareBitmap bitmap = inputImage.SoftwareBitmap; + inputImage = await ResizeImage(inputImage, bitmap); + _binding.Clear(); + _binding.Bind("input_1:0", inputImage); + var results = await _session.EvaluateAsync(_binding, ""); + + TensorFloat result = results.Outputs["Identity:0"] as TensorFloat; + var data = result.GetAsVectorView(); + + List detections = ParseResult(data.ToList().ToArray()); + Comparer cp = new Comparer(); + detections.Sort(cp); + List final_detections = NMS(detections); + + return final_detections; + } + + private static async Task ResizeImage(VideoFrame inputImage, SoftwareBitmap bitmap) + { using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream); @@ -167,25 +195,24 @@ namespace IntelligentAPI.ObjectDetection SoftwareBitmap newBitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, bitmap.BitmapAlphaMode); inputImage = VideoFrame.CreateWithSoftwareBitmap(newBitmap); } - _binding.Clear(); - _binding.Bind("input_1:0", inputImage); - var results = await _session.EvaluateAsync(_binding, ""); - TensorFloat result = results.Outputs["Identity:0"] as TensorFloat; - var data = result.GetAsVectorView(); - - List detections = ParseResult(data.ToList().ToArray()); - Comparer cp = new Comparer(); - detections.Sort(cp); - List final_detections = NMS(detections); - - return final_detections; + return inputImage; } - private static async Task convertToVideoFrame(StorageFile file) + private static async Task GenerateVideoFrameFromBitmap(SoftwareBitmap softwareBitmap) + { + SoftwareBitmapSource imageSource = new SoftwareBitmapSource(); + await imageSource.SetBitmapAsync(softwareBitmap); + + // Encapsulate the image within a VideoFrame to be bound and evaluated + VideoFrame videoFrame = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); + return videoFrame; + } + + private static async Task GenerateSoftwareBitmapFromStorageFile(StorageFile selectedStorageFile) { SoftwareBitmap softwareBitmap; - using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) + using (IRandomAccessStream stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); @@ -195,13 +222,7 @@ namespace IntelligentAPI.ObjectDetection softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } - // Display the image - SoftwareBitmapSource imageSource = new SoftwareBitmapSource(); - await imageSource.SetBitmapAsync(softwareBitmap); - - // Encapsulate the image within a VideoFrame to be bound and evaluated - VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); - return inputImage; + return softwareBitmap; } class Comparer : IComparer diff --git a/ObjectDetector/Yolo.cs b/ObjectDetector/Yolo.cs index 00eeb15..9efd537 100644 --- a/ObjectDetector/Yolo.cs +++ b/ObjectDetector/Yolo.cs @@ -1,3 +1,7 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + // This file was automatically generated by VS extension Windows Machine Learning Code Generator v3 // from model file Yolo.onnx // Warning: This file may get overwritten if you add add an onnx file with the same name diff --git a/ObjectDetector/nuget.config b/ObjectDetector/nuget.config new file mode 100644 index 0000000..4169d2f --- /dev/null +++ b/ObjectDetector/nuget.config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file