added license headers and overload functions

This commit is contained in:
Amrutha Srinivasan 2021-07-19 13:33:42 -07:00
Родитель ff61225259
Коммит 5364a6824e
17 изменённых файлов: 179 добавлений и 49 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -132,11 +132,17 @@
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.1</Version>
</PackageReference>
<PackageReference Include="System.Text.Json">
<Version>5.0.2</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Content Include="Assets\model.onnx" />
<Content Include="Assets\Labels.json" />
</ItemGroup>
<ItemGroup>
<None Include="ImageClassifier.licenseheader" />
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>

Просмотреть файл

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>IntelligentAPI.ImageClassifier</id>
<version>0.0.16</version>
<id>IntelligentAPI_ImageClassifier</id>
<version>0.0.17</version>
<title>Cool nuget package for ML stuff</title>
<authors>Amrutha Srinivasan</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
@ -11,6 +11,7 @@
<iconUrl>http://icon_url_here_or_delete_this_line/</iconUrl>
<description>Nuget package that exposes APIs to perform image classification</description>
<copyright>Cool nuget package for ML stuff</copyright>
<repository type="git" url="https://github.com/windows-toolkit/Labs-IntelligentAPIs.git"></repository>
</metadata>
<files>
<file src="bin\Debug\*.dll" target="lib\uap10.0.17763" />

Просмотреть файл

@ -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;

Просмотреть файл

@ -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<string> _labels = new List<string>();
@ -30,17 +35,53 @@ namespace IntelligentAPI.ImageClassification
private SqueezeNetImageClassifier()
{
}
public static async Task<List<ClassificationResult>> ClassifyImage(StorageFile selectedStorageFile, int top)
public static async Task<List<ClassificationResult>> 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<List<ClassificationResult>> ClassifyImage(SoftwareBitmap softwareBitmap, int top=3)
{
CreateInstanceIfNone();
VideoFrame videoFrame = await GenerateVideoFrameFromBitmap(softwareBitmap);
return await instance.EvaluateModel(videoFrame, top);
}
public static async Task<List<ClassificationResult>> 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<List<ClassificationResult>> EvaluateModel(StorageFile selectedStorageFile, int top)
public async Task<List<ClassificationResult>> EvaluateModel(VideoFrame inputImage, int top)
{
await LoadModelAsync();
return await EvaluateVideoFrameAsync(inputImage, top);
}
private static async Task<VideoFrame> 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<SoftwareBitmap> 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<Dictionary<string, string>>(fileString);
var fileDict = JsonSerializer.Deserialize<Dictionary<string, string>>(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;
}
}

Просмотреть файл

@ -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

Просмотреть файл

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="github" value="https://nuget.pkg.github.com/windows-toolkit/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="amrutha95" />
<add key="ClearTextPassword" value="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" />
</github>
</packageSourceCredentials>
</configuration>

Просмотреть файл

@ -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

Просмотреть файл

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="github" value="https://nuget.pkg.github.com/amrutha95/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="amrutha95" />
<add key="ClearTextPassword" value="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" />
</github>
</packageSourceCredentials>
</configuration>

Просмотреть файл

@ -136,6 +136,7 @@
<ItemGroup>
<None Include="IntelligentAPI_ObjectDetector.nuspec" />
<None Include="IntelligentAPI_ObjectDetector.pri" />
<None Include="ObjectDetector.licenseheader" />
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>

Просмотреть файл

@ -9,7 +9,7 @@
<license type="expression">MIT</license>
<description>Nuget package for real time object detection</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<tags>Tag1 Tag2</tags>
<repository type="git" url="https://github.com/windows-toolkit/Labs-IntelligentAPIs.git"></repository>
</metadata>
<files>

Просмотреть файл

@ -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.

Просмотреть файл

@ -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;

Просмотреть файл

@ -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<List<DetectionResult>> 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<List<DetectionResult>> DetectObjects(VideoFrame file)
public static async Task<List<DetectionResult>> DetectObjects(SoftwareBitmap bitmap)
{
VideoFrame videoFrame = await GenerateVideoFrameFromBitmap(bitmap);
return await DetectObjects(videoFrame);
}
public static async Task<List<DetectionResult>> 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<DetectionResult> detections = ParseResult(data.ToList<float>().ToArray());
Comparer cp = new Comparer();
detections.Sort(cp);
List<DetectionResult> final_detections = NMS(detections);
return final_detections;
}
private static async Task<VideoFrame> 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<DetectionResult> detections = ParseResult(data.ToList<float>().ToArray());
Comparer cp = new Comparer();
detections.Sort(cp);
List<DetectionResult> final_detections = NMS(detections);
return final_detections;
return inputImage;
}
private static async Task<VideoFrame> convertToVideoFrame(StorageFile file)
private static async Task<VideoFrame> 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<SoftwareBitmap> 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<DetectionResult>

Просмотреть файл

@ -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

Просмотреть файл

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="github" value="https://nuget.pkg.github.com/windows-toolkit/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="amrutha95" />
<add key="ClearTextPassword" value="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" />
</github>
</packageSourceCredentials>
</configuration>