diff --git a/IntelligentAPI_EmotionRecognizer/EmotionRecognizer.cs b/IntelligentAPI_EmotionRecognizer/EmotionRecognizer.cs new file mode 100644 index 0000000..f213271 --- /dev/null +++ b/IntelligentAPI_EmotionRecognizer/EmotionRecognizer.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Windows.AI.MachineLearning; +using Windows.Foundation; +using Windows.Graphics.Imaging; +using Windows.Media; +using Windows.Media.FaceAnalysis; +using Windows.Storage; + +namespace CommunityToolkit.Labs.Intelligent.EmotionRecognition +{ + public class EmotionRecognizer + { + private LearningModel _model = null; + private LearningModelSession _session = null; + int happinessEmotionIndex; + private LearningModelBinding _binding = null; + FaceDetector faceDetector; + + List labels; + + + private async Task LoadModelAsync() + { + // load model file + var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/model_emotion.onnx")); + + //Loads the mdoel from the file + _model = await LearningModel.LoadFromStorageFileAsync(file); + + //Creating a session that binds the model to the device running the model + _session = new LearningModelSession(_model, new LearningModelDevice(GetDeviceKind())); + } + + private void LoadLabels() + { + labels = new List() + { + "Neutral", + "Happiness", + "Surprise", + "Sadness", + "Anger", + "Disgust", + "Fear", + "Contempt" + }; + happinessEmotionIndex = 1; //happiness + } + + LearningModelDeviceKind GetDeviceKind() + { + return LearningModelDeviceKind.Cpu; + } + + private async Task> DetectFacesInImageAsync(SoftwareBitmap bitmap) + { + faceDetector = await FaceDetector.CreateAsync(); + var convertedBitmap = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Gray8); + return await faceDetector.DetectFacesAsync(convertedBitmap); + + } + + + private async Task BindAndEvaluateModelAsync(SoftwareBitmap croppedFace) + { + //Create Learning model binding which binds + _binding = new LearningModelBinding(_session); + _binding.Bind("Input3", VideoFrame.CreateWithSoftwareBitmap(croppedFace)); + var emotionResults = await _session.EvaluateAsync(_binding, "id"); + return emotionResults; + } + + } +} diff --git a/IntelligentAPI_EmotionRecognizer/IntelligentAPI_EmotionRecognizer.csproj b/IntelligentAPI_EmotionRecognizer/IntelligentAPI_EmotionRecognizer.csproj new file mode 100644 index 0000000..40f33ae --- /dev/null +++ b/IntelligentAPI_EmotionRecognizer/IntelligentAPI_EmotionRecognizer.csproj @@ -0,0 +1,138 @@ + + + + + Debug + AnyCPU + {6dc9254b-b4d9-4a30-92c9-8407ef653617} + Library + Properties + IntelligentAPI_EmotionRecognizer + IntelligentAPI_EmotionRecognizer + en-US + UAP + 10.0.19041.0 + 10.0.17763.0 + 14 + 512 + {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + prompt + 4 + + + x86 + true + bin\x86\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + false + prompt + + + x86 + bin\x86\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + false + prompt + + + ARM + true + bin\ARM\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + false + prompt + + + ARM + bin\ARM\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + false + prompt + + + ARM64 + true + bin\ARM64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + false + prompt + + + ARM64 + bin\ARM64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + false + prompt + + + x64 + true + bin\x64\Debug\ + DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP + ;2008 + full + false + prompt + + + x64 + bin\x64\Release\ + TRACE;NETFX_CORE;WINDOWS_UWP + true + ;2008 + pdbonly + false + prompt + + + PackageReference + + + + + + + + 14.0 + + + + diff --git a/IntelligentAPI_EmotionRecognizer/Properties/AssemblyInfo.cs b/IntelligentAPI_EmotionRecognizer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7f22b10 --- /dev/null +++ b/IntelligentAPI_EmotionRecognizer/Properties/AssemblyInfo.cs @@ -0,0 +1,29 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("IntelligentAPI_EmotionRecognizer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("IntelligentAPI_EmotionRecognizer")] +[assembly: AssemblyCopyright("Copyright © 2021")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: ComVisible(false)] \ No newline at end of file diff --git a/IntelligentAPI_EmotionRecognizer/Properties/IntelligentAPI_EmotionRecognizer.rd.xml b/IntelligentAPI_EmotionRecognizer/Properties/IntelligentAPI_EmotionRecognizer.rd.xml new file mode 100644 index 0000000..0496fc7 --- /dev/null +++ b/IntelligentAPI_EmotionRecognizer/Properties/IntelligentAPI_EmotionRecognizer.rd.xml @@ -0,0 +1,33 @@ + + + + + + + + +