diff --git a/IntelligentAPI_EmotionRecognizer/EmotionRecognizer.cs b/IntelligentAPI_EmotionRecognizer/EmotionRecognizer.cs deleted file mode 100644 index deadfe7..0000000 --- a/IntelligentAPI_EmotionRecognizer/EmotionRecognizer.cs +++ /dev/null @@ -1,164 +0,0 @@ -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; -using Windows.Storage.Streams; - -namespace CommunityToolkit.Labs.Intelligent.EmotionRecognition -{ - public class DetectedEmotion - { - public int emotionIndex; - public string emotion; - } - public class EmotionRecognizer - { - private LearningModel _model = null; - private LearningModelSession _session = null; - private LearningModelBinding _binding = null; - private static EmotionRecognizer instance = null; - - private static List labels; - - - private async Task InitModelAsync() - { - // load model file - var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///IntelligentAPI_EmotionRecognizer/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" - }; - } - - LearningModelDeviceKind GetDeviceKind() - { - return LearningModelDeviceKind.Cpu; - } - - private async static Task> DetectFacesInImageAsync(SoftwareBitmap bitmap) - { - FaceDetector faceDetector = await FaceDetector.CreateAsync(); - var convertedBitmap = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Gray8); - return await faceDetector.DetectFacesAsync(convertedBitmap); - - } - - public async static Task DetectEmotion(SoftwareBitmap bitmap) - { - if (instance == null) - { - instance = new EmotionRecognizer(); - } - - return await instance.EvaluateFrame(bitmap); - } - - public async Task EvaluateFrame(SoftwareBitmap softwareBitmap) - { - await InitModelAsync(); - LoadLabels(); - DetectedFace detectedFace = await DetectFace(softwareBitmap); - if (detectedFace != null) - { - return await EvaluateEmotionInFace(detectedFace, softwareBitmap); - } - return null; - } - - public async Task EvaluateEmotionInFace(DetectedFace detectedFace, SoftwareBitmap softwareBitmap) - { - - var boundingBox = new Rect(detectedFace.FaceBox.X, - detectedFace.FaceBox.Y, - detectedFace.FaceBox.Width, - detectedFace.FaceBox.Height); - - softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8); - - var croppedFace = await Crop(softwareBitmap, boundingBox); - LearningModelEvaluationResult emotionResults = await BindAndEvaluateModelAsync(croppedFace); - - // to get percentages, you'd need to run the output through a softmax function - // we don't need percentages, we just need max value - TensorFloat emotionIndexTensor = emotionResults.Outputs["Plus692_Output_0"] as TensorFloat; - - var emotionList = emotionIndexTensor.GetAsVectorView().ToList(); - var emotionIndex = emotionList.IndexOf(emotionList.Max()); - - return new DetectedEmotion() { emotionIndex = emotionIndex, emotion = labels[emotionIndex] }; - - - } - - private static async Task DetectFace(SoftwareBitmap softwareBitmap) - { - var faces = await DetectFacesInImageAsync(softwareBitmap); - - // if there is a face in the frame, evaluate the emotion - var detectedFace = faces.FirstOrDefault(); - return detectedFace; - } - - public static async Task Crop(SoftwareBitmap softwareBitmap, Rect bounds) - { - VideoFrame vid = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); - vid = await Crop(vid, bounds); - return vid.SoftwareBitmap; - - } - public static async Task Crop(VideoFrame videoFrame, Rect bounds) - { - BitmapBounds cropBounds = new BitmapBounds() - { - Width = (uint)bounds.Width, - Height = (uint)bounds.Height, - X = (uint)bounds.X, - Y = (uint)bounds.Y - }; - VideoFrame result = new VideoFrame(BitmapPixelFormat.Bgra8, - (int)cropBounds.Width, - (int)cropBounds.Height, - BitmapAlphaMode.Premultiplied); - - await videoFrame.CopyToAsync(result, cropBounds, null); - - return result; - } - - - 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 deleted file mode 100644 index 46aeb73..0000000 --- a/IntelligentAPI_EmotionRecognizer/IntelligentAPI_EmotionRecognizer.csproj +++ /dev/null @@ -1,24 +0,0 @@ - - - IntelligentAPI.EmotionRecognition - - - CommunityToolkit.Labs.Intelligent.EmotionRecognition2 - - This package performs Emotion Recognition on an input image by using the Emotion FERPlus model. - - 0.0.2 - - - - - - - - - - - - - - diff --git a/IntelligentAPI_EmotionRecognizer/Properties/AssemblyInfo.cs b/IntelligentAPI_EmotionRecognizer/Properties/AssemblyInfo.cs deleted file mode 100644 index 08d9d0d..0000000 --- a/IntelligentAPI_EmotionRecognizer/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,21 +0,0 @@ -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: 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: 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 deleted file mode 100644 index 0496fc7..0000000 --- a/IntelligentAPI_EmotionRecognizer/Properties/IntelligentAPI_EmotionRecognizer.rd.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - diff --git a/IntelligentAPI_EmotionRecognizer/Scripts/script.ps1 b/IntelligentAPI_EmotionRecognizer/Scripts/script.ps1 deleted file mode 100644 index dced905..0000000 --- a/IntelligentAPI_EmotionRecognizer/Scripts/script.ps1 +++ /dev/null @@ -1,15 +0,0 @@ -$ProgressPreference = 'SilentlyContinue' - -$emotionferplusfile = "./Assets/model_emotion.onnx" -if (-not(Test-Path -Path $emotionferplusfile -PathType Leaf)) { - try { - Invoke-WebRequest -URI "https://github.com/onnx/models/raw/master/vision/body_analysis/emotion_ferplus/model/emotion-ferplus-8.onnx" -OutFile $emotionferplusfile - Write-Host "The file [$emotionferplusfile] has been created." - } - catch { - throw $_.Exception.Message - } - } -else { - Write-Host "The file [$emotionferplusfile] exists." -} \ No newline at end of file