Beispiel #1
0
        private void ShowFromFaceApi(Size frameSize, CustomFaceModel[] customFaces, EmojiNum emojiNum)
        //private void ShowFromFaceApi(Size frameSize, CustomFaceModel[] customFaces)
        {
            SolidColorBrush lineBrush     = new SolidColorBrush(Windows.UI.Colors.Yellow);
            double          lineThickness = 3.0;
            SolidColorBrush fillBrush     = new SolidColorBrush(Windows.UI.Colors.Transparent);

            PaintingCanvas.Children.Clear();
            if (_state == StreamingState.Streaming && customFaces != null)
            {
                double widthScale  = frameSize.Width / PaintingCanvas.ActualWidth;
                double heightScale = frameSize.Height / PaintingCanvas.ActualHeight;
                for (int i = 0; i < customFaces.Length; i++)
                {
                    Rectangle box = new Rectangle()
                    {
                        Width           = (uint)(customFaces[i].Width / widthScale),
                        Height          = (uint)(customFaces[i].Height / heightScale),
                        Fill            = fillBrush,
                        StrokeThickness = lineThickness,
                        Stroke          = lineBrush,
                        Margin          = new Thickness((uint)(customFaces[i].Left / widthScale), (uint)(customFaces[i].Top / heightScale), 0, 0)
                    };
                    PaintingCanvas.Children.Add(box);

                    Grid grid = new Grid()
                    {
                        Background          = lineBrush,
                        Width               = (customFaces[i].Width / widthScale),
                        VerticalAlignment   = VerticalAlignment.Center,
                        HorizontalAlignment = HorizontalAlignment.Center,
                        Margin              = new Thickness((uint)(customFaces[i].Left / widthScale), (uint)(customFaces[i].Top / heightScale), 0, 0)
                    };

                    TextBlock t = new TextBlock()
                    {
                        Text     = (emojiNum.Emoji).ToString(),
                        FontSize = 28,
                        HorizontalTextAlignment = TextAlignment.Right,
                        Foreground = new SolidColorBrush(Windows.UI.Colors.Black)
                    };
                    grid.Children.Add(t);
                    PaintingCanvas.Children.Add(grid);
                }
            }
        }
Beispiel #2
0
        private async void ProcessCurrentVideoFrame(ThreadPoolTimer timer)
        {
            // If state is not Streaming, return.
            if (_state != StreamingState.Streaming)
            {
                return;
            }

            // If there has a process still running, return.
            if (!_semaphoreSlim.Wait(0))
            {
                return;
            }

            const BitmapPixelFormat PixelFormat = BitmapPixelFormat.Nv12;

            try
            {
                using (VideoFrame currentFrame = new VideoFrame(PixelFormat, (int)_videoProperties.Width, (int)_videoProperties.Height))
                {
                    // Get current preview frame from _mediaCaputre and copy into currentFrame.
                    await _mediaCapture.GetPreviewFrameAsync(currentFrame);

                    // Detected face by _faceTracker.
                    IList <DetectedFace> builtinFaces = await _faceTracker.ProcessNextFrameAsync(currentFrame);

                    SoftwareBitmap tempBitmap = SoftwareBitmap.Convert(currentFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8);

                    if (builtinFaces.Count != 0)
                    {
                        var frameSize = new Size(currentFrame.SoftwareBitmap.PixelWidth, currentFrame.SoftwareBitmap.PixelHeight);
                        //await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        //{
                        //    ShowResult(frameSize, builtinFaces);
                        //});

                        // Get picture from videoframe.
                        IRandomAccessStream stream  = new InMemoryRandomAccessStream();
                        BitmapEncoder       encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

                        encoder.SetSoftwareBitmap(tempBitmap);
                        await encoder.FlushAsync();

                        CustomFaceModel[] customFaces = await _faceApiHelper.GetDetectEmojiAsync(stream.AsStream());

                        CustomFaceEmojiModel customFaceEmojiModel = new CustomFaceEmojiModel();
                        EmojiNum             emojiNum = new EmojiNum();
                        float upperleft = 0, upperrignt = 0, buttomleft = 0, buttomright = 0, averageX = 0, averageY = 0;
                        foreach (var eachemoliModel in customFaces)
                        {
                            averageX += eachemoliModel.Left;
                            averageY += eachemoliModel.Top;
                        }
                        averageX /= customFaces.Length;
                        averageY /= customFaces.Length;

                        for (int i = 0; i < customFaces.Length; i++)
                        {
                            emojiNum.Emoji = -1 * (customFaces[i].Anger + customFaces[i].Contempt + customFaces[i].Disgust + customFaces[i].Fear + customFaces[i].Sadness) + customFaces[i].Happiness + customFaces[i].Neutral + customFaces[i].Suprise;
                            EmojiNum model = new EmojiNum
                            {
                                Emoji = -1 * (customFaces[i].Anger + customFaces[i].Contempt + customFaces[i].Disgust + customFaces[i].Fear + customFaces[i].Sadness) + customFaces[i].Happiness + customFaces[i].Neutral + customFaces[i].Suprise
                            };
                            //customFaceEmojiModel.Emojis[i] = model;
                            //customFaceEmojiModel.Emojis[i].Emoji = -1 * (customFaces[i].Anger + customFaces[i].Contempt + customFaces[i].Disgust + customFaces[i].Fear + customFaces[i].Sadness) + customFaces[i].Happiness + customFaces[i].Neutral + customFaces[i].Suprise;
                            customFaceEmojiModel.EmojiSum += model.Emoji;
                            //customFaceEmojiModel.EmojiSum += customFaceEmojiModel.Emojis[i].Emoji;
                            if (customFaces[i].Left <averageX && customFaces[i].Top> averageY)
                            {
                                upperleft += emojiNum.Emoji;
                            }
                            else if (customFaces[i].Left < averageX && customFaces[i].Top < averageY)
                            {
                                buttomleft += emojiNum.Emoji;
                            }
                            else if (customFaces[i].Left > averageX && customFaces[i].Top > averageY)
                            {
                                upperrignt += emojiNum.Emoji;
                            }
                            else if (customFaces[i].Left > averageX && customFaces[i].Top < averageY)
                            {
                                buttomright += emojiNum.Emoji;
                            }
                        }
                        customFaceEmojiModel.UpperLeft  /= upperleft;
                        customFaceEmojiModel.ButtomLeft /= buttomleft;
                        customFaceEmojiModel.UpperRight /= upperrignt;
                        customFaceEmojiModel.ButtoRight /= buttomright;

                        //CustomFaceEmojiModel customFaceEmojiModel = await _faceApiHelper.GetEmojiResult(customFaces);
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                                                  ShowFromFaceApi(frameSize, customFaces, emojiNum));

                        await _eventHubHelper.SendMessagesToEventHub(customFaceEmojiModel);
                    }
                    else
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                                                  PaintingCanvas.Children.Clear());
                    }
                }
            }
            catch (Microsoft.ProjectOxford.Face.FaceAPIException faceEx)
            {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                                          ShowErrorHelper.ShowDialog(faceEx.ErrorMessage, faceEx.ErrorCode));
            }
            catch (Exception ex)
            {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                                          ShowErrorHelper.ShowDialog(ex.Message));
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }