Exemple #1
0
        private async Task RunSkillAsync(VideoFrame frame)
        {
            // Update input image and run the skill against it
            await m_binding.SetInputImageAsync(frame);

            await m_skill.EvaluateAsync(m_binding);

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                // Retrieve result
                if (!m_binding.IsFaceFound)
                {
                    // if no face found, hide the rectangle in the UI
                    m_faceSentimentRenderer.IsVisible = false;
                    UISkillOutputDetails.Text         = "No face found";
                }
                else // Display the face rectangle and sentiment in the UI
                {
                    m_faceSentimentRenderer.Update(m_binding.FaceRectangle, m_binding.PredominantSentiment);
                    m_faceSentimentRenderer.IsVisible = true;
                    var scores = (m_binding["FaceSentimentScores"].FeatureValue as SkillFeatureTensorFloatValue).GetAsVectorView();
                    UISkillOutputDetails.Text = "";
                    for (int i = 0; i < (int)SentimentType.contempt; i++)
                    {
                        UISkillOutputDetails.Text += $"{(SentimentType)i} : {scores[i]} {(i == (int)m_binding.PredominantSentiment ? " <<------" : "")} \n";
                    }
                }
            });
        }
        /// <summary>
        /// Evaluates the frame for scene classification
        /// </summary>
        /// <param name="vf"></param>
        /// <returns></returns>
        private static async Task EvaluateFrameAsync(VideoFrame vf)
        {
            // Process 1 frame at a time, if busy return right away
            if (0 == Interlocked.Exchange(ref m_lock, 1))
            {
                // Update input image and run the skill against it
                await m_binding.SetInputImageAsync(vf);

                await m_skill.EvaluateAsync(m_binding);

                string outText = "";
                if (!m_binding.IsFaceFound)
                {
                    // if no face found, hide the rectangle in the UI
                    outText = "No face found";
                }
                else // Display the  sentiment on the console
                {
                    outText = "Your sentiment looks like: " + m_binding.PredominantSentiment.ToString();

                    var folder = await StorageFolder.GetFolderFromPathAsync(System.AppContext.BaseDirectory);

                    var file = await folder.CreateFileAsync(m_binding.PredominantSentiment.ToString() + "Face.jpg", CreationCollisionOption.ReplaceExisting);

                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

                        SoftwareBitmap sb;
                        if (vf.SoftwareBitmap == null)
                        {
                            sb = await SoftwareBitmap.CreateCopyFromSurfaceAsync(vf.Direct3DSurface);
                        }
                        else
                        {
                            sb = vf.SoftwareBitmap;
                        }

                        encoder.SetSoftwareBitmap(
                            sb.BitmapPixelFormat.Equals(BitmapPixelFormat.Bgra8) ? sb : SoftwareBitmap.Convert(sb, BitmapPixelFormat.Bgra8)
                            );
                        await encoder.FlushAsync();
                    }
                }
                Console.Write("\r" + outText + "\t\t\t\t\t");

                // Release the lock
                Interlocked.Exchange(ref m_lock, 0);
            }
        }