Exemplo n.º 1
0
        public FixedHandDetectionAreasExample01App()
            : base()
        {
            Egs.BindableResources.Current.CultureChanged += delegate
            {
                ApplicationCommonSettings.HostApplicationName = "FixedHandDetectionAreasExample01";
            };

            Egs.BindableResources.Current.ChangeCulture("");

            if (DuplicatedProcessStartBlocking.TryGetMutexOnTheBeginningOfApplicationConstructor() == false)
            {
                Application.Current.Shutdown();
                return;
            }

            Device = EgsDevice.GetDefaultEgsDevice();
            Device.Settings.IsToDrawBordersOnCameraViewImageByDevice.Value = true;
            Device.Settings.IsToDetectFaces.Value                 = true;
            Device.Settings.IsToDetectHands.Value                 = true;
            Device.Settings.IsToFixHandDetectionRegions.Value     = true;
            Device.Settings.CameraViewImageSourceBitmapSize.Value = Egs.PropertyTypes.CameraViewImageSourceBitmapSizes.Size_640x480;

            {
                CursorViewModels    = new CursorViewModel[Device.TrackableHandsCountMaximum];
                CursorViewModels[0] = new CursorViewModel();
                CursorViewModels[1] = new CursorViewModel();
                CursorViews         = new CursorForm[Device.TrackableHandsCountMaximum];
                CursorViews[0]      = new CursorForm();
                CursorViews[1]      = new CursorForm();
                var imageSetListRight = new List <ImageInformationSet>();
                var imageSetListLeft  = new List <ImageInformationSet>();
                imageSetListRight.Add(new ImageInformationSet()
                {
                    ImageSetIndex = 0, FolderPath = "Resources\\", SampleImageFileRelativePath = "Resources\\Sample.png"
                });
                imageSetListRight[0].AddImage((int)CursorImageIndexLabels.OpenHand, "Right_OpenHand.png");
                imageSetListRight[0].AddImage((int)CursorImageIndexLabels.CloseHand, "Right_CloseHand.png");
                imageSetListLeft.Add(new ImageInformationSet()
                {
                    ImageSetIndex = 0, FolderPath = "Resources\\", SampleImageFileRelativePath = "Resources\\Sample.png"
                });
                imageSetListLeft[0].AddImage((int)CursorImageIndexLabels.OpenHand, "Left_OpenHand.png");
                imageSetListLeft[0].AddImage((int)CursorImageIndexLabels.CloseHand, "Left_CloseHand.png");
                CursorViews[0].InitializeOnceAtStartup(CursorViewModels[0], imageSetListRight);
                CursorViews[1].InitializeOnceAtStartup(CursorViewModels[1], imageSetListLeft);
                Device.EgsGestureHidReport.ReportUpdated += EgsGestureHidReport_ReportUpdated;
                CameraViewBackgroundWindowModel           = new CameraViewModel();
                CameraViewBackgroundWindow = new FixedHandDetectionAreasExample01MainWindow();
                CameraViewBackgroundWindowModel.InitializeOnceAtStartup(Device);
                Device.CameraViewImageSourceBitmapCapture.CameraViewImageSourceBitmapChanged += delegate
                {
                    // Unfortunately, DataBinding does not seem to be able to solve "different thread" InvalidOperationException.
                    CameraViewBackgroundWindow.CameraViewImage.Dispatcher.InvokeAsync(() =>
                    {
                        CameraViewBackgroundWindow.CameraViewImage.Source = CameraViewBackgroundWindowModel.CameraViewWpfBitmapSource;
                    });
                };
                CameraViewBackgroundWindow.KeyDown += (sender, e) =>
                {
                    switch (e.Key)
                    {
                    case System.Windows.Input.Key.Escape:
                        CameraViewBackgroundWindow.Close();
                        break;
                    }
                };
                base.MainWindow = CameraViewBackgroundWindow;
                CameraViewBackgroundWindow.Show();
            }

            InitializeFaceDetection();
        }
Exemplo n.º 2
0
        public Form1()
        {
            InitializeComponent();

            // It shows some debugging information.
            ApplicationCommonSettings.IsDebugging = true;


            CameraViewImagePictureBox = new PictureBox()
            {
                Dock = DockStyle.Fill, SizeMode = PictureBoxSizeMode.CenterImage
            };
            this.Controls.Add(CameraViewImagePictureBox);
            this.Size = new Size(800, 600);


            Device = EgsDevice.GetDefaultEgsDevice();
            Device.Settings.FaceDetectionMethod.Value = Egs.PropertyTypes.FaceDetectionMethods.DefaultProcessOnEgsDevice;
            Device.Settings.IsToDetectFaces.Value     = true;
            Device.Settings.IsToDetectHands.Value     = true;
#if DEBUG
            Device.Settings.IsToDrawBordersOnCameraViewImageByDevice.Value = true;
            Device.Settings.CameraViewImageSourceBitmapSize.Value          = Egs.PropertyTypes.CameraViewImageSourceBitmapSizes.Size_640x480;
#else
            Device.Settings.IsToDrawBordersOnCameraViewImageByDevice.Value = false;
            Device.Settings.CameraViewImageSourceBitmapSize.Value          = Egs.PropertyTypes.CameraViewImageSourceBitmapSizes.Size_384x240;
#endif


            // CursorForm objects observe CursorViewModel objects in the OnePersonBothHandsViewModel object, and draw the "Gesture Cursor" on desktop.
            {
                CursorViewModels    = new CursorViewModel[Device.TrackableHandsCountMaximum];
                CursorViewModels[0] = new CursorViewModel();
                CursorViewModels[1] = new CursorViewModel();
                CursorViews         = new CursorForm[Device.TrackableHandsCountMaximum];
                CursorViews[0]      = new CursorForm();
                CursorViews[1]      = new CursorForm();
                var imageSetListRight = new List <ImageInformationSet>();
                var imageSetListLeft  = new List <ImageInformationSet>();
                imageSetListRight.Add(new ImageInformationSet()
                {
                    ImageSetIndex = 0, FolderPath = "Resources\\", SampleImageFileRelativePath = "Resources\\Sample.png"
                });
                imageSetListRight[0].AddImage((int)CursorImageIndexLabels.OpenHand, "Right_OpenHand.png");
                imageSetListRight[0].AddImage((int)CursorImageIndexLabels.CloseHand, "Right_CloseHand.png");
                imageSetListLeft.Add(new ImageInformationSet()
                {
                    ImageSetIndex = 0, FolderPath = "Resources\\", SampleImageFileRelativePath = "Resources\\Sample.png"
                });
                imageSetListLeft[0].AddImage((int)CursorImageIndexLabels.OpenHand, "Left_OpenHand.png");
                imageSetListLeft[0].AddImage((int)CursorImageIndexLabels.CloseHand, "Left_CloseHand.png");
                CursorViews[0].InitializeOnceAtStartup(CursorViewModels[0], imageSetListRight);
                CursorViews[1].InitializeOnceAtStartup(CursorViewModels[1], imageSetListLeft);
                Device.EgsGestureHidReport.ReportUpdated += EgsGestureHidReport_ReportUpdated;
            }

            // You can make your original camera view.
            Device.CameraViewImageSourceBitmapCapture.CameraViewImageSourceBitmapChanged += delegate
            {
                if (CameraViewImagePictureBox.Image != null)
                {
                    CameraViewImagePictureBox.Image.Dispose();
                }
                // Bitmap.Clone() is shallow copy method.
                var bmp = (System.Drawing.Bitmap)Device.CameraViewImageSourceBitmapCapture.CameraViewImageSourceBitmap.Clone();
                using (var g = Graphics.FromImage(bmp))
                {
                    if (Device.EgsGestureHidReport.IsStandingBy)
                    {
                    }
                    else if (Device.EgsGestureHidReport.IsFaceDetecting)
                    {
                        g.DrawRectangle(Pens.LightCyan, Device.EgsGestureHidReport.FaceDetectionArea);
                        for (int i = 0; i < Device.EgsGestureHidReport.DetectedFacesCount; i++)
                        {
                            g.DrawRectangle(Pens.LightGreen, Device.EgsGestureHidReport.Faces[i].Area);
                        }
                        if (Device.EgsGestureHidReport.SelectedFaceIndex >= 0)
                        {
                            g.DrawRectangle(Pens.LightYellow, Device.EgsGestureHidReport.Faces[Device.EgsGestureHidReport.SelectedFaceIndex].Area);
                        }
                        foreach (var hand in Device.EgsGestureHidReport.Hands)
                        {
                            g.DrawRectangle(Pens.OrangeRed, hand.DetectionArea);
                        }
                    }
                    else
                    {
                        g.DrawRectangle(Pens.LightYellow, Device.EgsGestureHidReport.Faces[Device.EgsGestureHidReport.SelectedFaceIndex].Area);
                        foreach (var hand in Device.EgsGestureHidReport.Hands)
                        {
                            g.DrawRectangle(Pens.OrangeRed, hand.ScreenMappedArea);
                            int ellipseRadius = hand.IsTouching ? 5 : 10;
                            g.DrawEllipse(Pens.OrangeRed, (int)hand.XInCameraViewImage - ellipseRadius, (int)hand.YInCameraViewImage - ellipseRadius, ellipseRadius * 2, ellipseRadius * 2);
                        }
                    }
                }
                CameraViewImagePictureBox.Image = bmp;
            };

            this.FormClosed += (sender, e) =>
            {
                // If you do not detach this event handler, exceptions can happen.
                Device.EgsGestureHidReport.ReportUpdated -= EgsGestureHidReport_ReportUpdated;

                // When the application quits, please stop face detection and hand detection.
                Device.Settings.IsToDetectFaces.Value = false;
                Device.Settings.IsToDetectHands.Value = false;
                Device.Settings.IsToDrawBordersOnCameraViewImageByDevice.Value = false;
                EgsDevice.CloseDefaultEgsDevice();

                CameraViewImagePictureBox.Dispose();
                foreach (var cursorView in CursorViews)
                {
                    cursorView.Close();
                }
            };
        }
        public App()
            : base()
        {
            Egs.BindableResources.Current.CultureChanged += delegate
            {
                ApplicationCommonSettings.HostApplicationName = Egs.EgsDeviceControlCore.Properties.Resources.CommonStrings_GestureCamera;
                //ApplicationCommonSettings.HostApplicationName = "WpfApplication2";
            };

            // You can change the application CultureInfo to some cultures.
            // The next line lets it use OS culture
            Egs.BindableResources.Current.ChangeCulture("");
            //Egs.BindableResources.Current.ChangeCulture(ApplicationCommonSettings.DefaultCultureInfoName);
            //Egs.BindableResources.Current.ChangeCulture("en");
            //Egs.BindableResources.Current.ChangeCulture("ja");
            //Egs.BindableResources.Current.ChangeCulture("zh-Hans");

            if (DuplicatedProcessStartBlocking.TryGetMutexOnTheBeginningOfApplicationConstructor() == false)
            {
                var msg = string.Format(System.Globalization.CultureInfo.InvariantCulture, Egs.EgsDeviceControlCore.Properties.Resources.CommonStrings_Application0IsAlreadyRunning, ApplicationCommonSettings.HostApplicationName);
                MessageBox.Show(msg, ApplicationCommonSettings.HostApplicationName);
                Application.Current.Shutdown();
                return;
            }


            // Sorry, EgsHostSettings and EgsDevice.GetDefaultEgsDevice(EgsDeviceSettings) are no longer available.
            // Device settings are saved on Host PC,
            // and the host application transfers the device settings to connected device when it starts running or a device is connected.
            // You can use EgsDevice object directly, without EgsHostAppBaseComponents or EgsHostOnUserControl.
            Device = EgsDevice.GetDefaultEgsDevice();
            Device.Settings.FaceDetectionMethod.Value = FaceDetectionMethods.DefaultProcessOnEgsDevice;
            Device.Settings.IsToDetectFaces.Value     = true;
            Device.Settings.IsToDetectHands.Value     = true;


            // OnePersonBothHandsViewModel can receive information from EgsDevice on EgsGestureHidReport.ReportUpdated event etc.
            // It interprets the information from EgsDevice for CursorForm and so on.
            OnePersonBothHandsViewModel = new OnePersonBothHandsViewModel();
            // A lot of classes in "Egs" namespace have "InitializeOnceAtStartup..." methods.
            // Please call the initialization before they are used as arguments of some other objects, or before some event handlers are attached.
            OnePersonBothHandsViewModel.InitializeOnceAtStartup(Device);
            // CursorForm shows Gesture Cursor.  It is available on Windows 7, 8, 8.1 (contains start screen) and 10.
            CursorViews    = new CursorForm[Device.TrackableHandsCountMaximum];
            CursorViews[0] = new CursorForm();
            CursorViews[1] = new CursorForm();
            CursorViews[0].InitializeOnceAtStartup(OnePersonBothHandsViewModel.RightHand, ImageInformationSet.CreateDefaultRightCursorImageInformationSetList());
            CursorViews[1].InitializeOnceAtStartup(OnePersonBothHandsViewModel.LeftHand, ImageInformationSet.CreateDefaultLeftCursorImageInformationSetList());
            Device.EgsGestureHidReport.ReportUpdated += EgsGestureHidReport_ReportUpdated;

            // Sorry, specification is changed.  You need to make an object of CameraViewUserControlModel.
            CameraViewUserControlModel = new CameraViewUserControlModel();
            CameraViewWindowModel      = new CameraViewWindowModel();
            CameraViewWindow           = new CameraViewWindow();

            CameraViewUserControlModel.IsToDrawImageSet = true;
            Device.Settings.IsToDrawBordersOnCameraViewImageByDevice.Value = false;

            CameraViewUserControlModel.InitializeOnceAtStartup(Device);
            CameraViewWindowModel.InitializeOnceAtStartup(Device);
            CameraViewWindow.InitializeOnceAtStartup(CameraViewWindowModel, CameraViewUserControlModel);

            CameraViewWindow.MainMenuItemsPanel.Columns = 2;
            // Minimize button needs EgsHostAppBaseComponents, because there is no way to "Un"-minimize it except for tray icon in system notification area, currently.
            // AppTrayIconAndMenuItemsComponent manages the tray icon, and change the visibility of Settings Window and Camera View.
            CameraViewWindow.MinimizeButton.Visibility = Visibility.Collapsed;
            // Settings button needs EgsHostAppBaseComponents, because only EgsHostAppBaseComponents has all information about host app,
            // and SettingsWindow can change the settings of EgsHostAppBaseComponents.
            CameraViewWindow.SettingsButton.Visibility = Visibility.Collapsed;
            // EgsHostAppBaseComponents also attachs an event handler which calls EgsHostAppBaseComponents.Dispose() and closes the application.
            CameraViewWindowModel.ExitCommand.PerformEventHandler += delegate
            {
                // In this example program, the next line means the application exit.
                CameraViewWindow.Close();
            };



            var button = new System.Windows.Controls.Button()
            {
                Content = "Show Dialog"
            };

            button.Click += delegate { MessageBox.Show("Hello!"); };
            CameraViewWindow.MainMenuItemsPanel.Children.Insert(0, button);
            CameraViewWindow.KeyDown += (sender, e) =>
            {
                switch (e.Key)
                {
                case System.Windows.Input.Key.Escape:
                    // This application does not use EgsHostAppBaseComponents, so this just closes the window.
                    CameraViewWindow.Close();
                    break;
                }
            };

            // But this event handler attach close cursors and device.
            CameraViewWindow.Closed += delegate
            {
                Device.EgsGestureHidReport.ReportUpdated -= EgsGestureHidReport_ReportUpdated;

                // When you get EgsDevice by EgsDevice.GetDefaultEgsDevice, you need to call EgsDevice.CloseDefaultEgsDevice().
                Device.Settings.IsToDetectFaces.Value = false;
                Device.Settings.IsToDetectHands.Value = false;
                EgsDevice.CloseDefaultEgsDevice();

                // And then, you need to close cursorViews.
                foreach (var cursorView in CursorViews)
                {
                    cursorView.Close();
                }
            };

            this.Exit += delegate
            {
                DuplicatedProcessStartBlocking.ReleaseMutex();
            };
        }