1

单击按钮以对用户的面部和眼睛进行监视后,我的 WPF 窗口冻结。为此,我正在使用 Intel Perceptual Computing SDK。

private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        pipeline = new MyPipeline();
        if (isRunning)
        {
            isRunning = false;
            pipeline.PauseFaceLocation(true);
            pipeline.Close();
        }
        else
        {
            isRunning = true;
            pipeline.LoopFrames();
        }
    }

而这个Util[M]Pipeline用于逐帧识别人脸。识别人脸后,我的目的是获取眼睛的状态(关闭/打开)。

    class MyPipeline : UtilMPipeline
    {
        //Core variables
        ulong timeStamp;
        int faceId;
        uint fidx = 0;
        bool eyeClosed = false;
        int ctrlTimerStart = 5, ctrlTimerStop = 3;

        DispatcherTimer screenOffTimer = new DispatcherTimer();
        DispatcherTimer gotoSleepTimer = new DispatcherTimer();

        [DllImport("user32.dll", SetLastError = true)]
        private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

        const int SC_MONITORPOWER = 0xF170;
        const int WM_SYSCOMMAND = 0x0112;

        const int MONITOR_ON = -1;
        const int MONITOR_OFF = 2;
        const int MONITOR_STANBY = 1;

        int HWND_BROADCAST = 0xffff;


        //Statuses
        pxcmStatus locationStatus;
        pxcmStatus landmarkStatus;
        pxcmStatus attributeStatus;
        public bool takeRecoSnapshot = false;

        //Form variables
        MainWindow parent;
        Bitmap lastProcessedBitmap;

        //Attribute array                
        uint[] blink = new uint[2];

        //PXCM variables
        PXCMFaceAnalysis faceAnalysis;
        PXCMSession session;
        PXCMFaceAnalysis.Detection faceLocation;
        PXCMFaceAnalysis.Landmark faceLandmark;
        PXCMFaceAnalysis.Attribute faceAttributes;
        PXCMFaceAnalysis.Detection.Data faceLocationData;
        PXCMFaceAnalysis.Landmark.LandmarkData[] faceLandmarkData;
        PXCMFaceAnalysis.Landmark.ProfileInfo landmarkProfile;
        PXCMFaceAnalysis.Attribute.ProfileInfo attributeProfile;

        public MyPipeline()
        {
            lastProcessedBitmap = new Bitmap(640, 480);

            attributeProfile = new PXCMFaceAnalysis.Attribute.ProfileInfo();

            EnableImage(PXCMImage.ColorFormat.COLOR_FORMAT_RGB24);
            EnableFaceLocation();
            EnableFaceLandmark();
        }


        public override bool OnNewFrame()
        {
            faceAnalysis = QueryFace();
            faceAnalysis.QueryFace(fidx, out faceId, out timeStamp);

            //Get face location
            faceLocation = (PXCMFaceAnalysis.Detection)faceAnalysis.DynamicCast(PXCMFaceAnalysis.Detection.CUID);
            locationStatus = faceLocation.QueryData(faceId, out faceLocationData);

            //Get face landmarks (eye, mouth, nose position, etc)
            faceLandmark = (PXCMFaceAnalysis.Landmark)faceAnalysis.DynamicCast(PXCMFaceAnalysis.Landmark.CUID);
            faceLandmark.QueryProfile(1, out landmarkProfile);
            faceLandmark.SetProfile(ref landmarkProfile);
            faceLandmarkData = new PXCMFaceAnalysis.Landmark.LandmarkData[7];
            landmarkStatus = faceLandmark.QueryLandmarkData(faceId, PXCMFaceAnalysis.Landmark.Label.LABEL_7POINTS, faceLandmarkData);

            //Get face attributes (eye blink)
            faceAttributes = (PXCMFaceAnalysis.Attribute)faceAnalysis.DynamicCast(PXCMFaceAnalysis.Attribute.CUID);
            faceAttributes.QueryProfile(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED, 0, out attributeProfile);
            attributeProfile.threshold = 50; //Must be here!
            faceAttributes.SetProfile(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED, ref attributeProfile);
            attributeStatus = faceAttributes.QueryData(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED, faceId, out blink);


            ComputationOfTimer();
            return true;
        }

ComputationOfTimer(); 函数在每一帧上调用,当眼睛闭上一定时间时,它会启动一个计时器。

private void ComputationOfTimer()
        {
            if (blink[0] == 100)        //If eye Closed detected
            {
                ctrlTimerStop = 3;
                ctrlTimerStart = ctrlTimerStart - 1;
                System.Console.Write("\n\t Eyes Closed");                    
                timerStarting();
            }
            else                        //If eyes are open we have to stop timer
            {
                ctrlTimerStart = 5;
                ctrlTimerStop -= 1;
                System.Console.Write("\n\t\t\t\t\t Opened");
                timerStopping();
            }
        }

        public void timerStarting()
        {
            if (ctrlTimerStart <= 0)
            {

                screenOffTimer.Interval = new TimeSpan(0, 0, 5);
                screenOffTimer.Tick += screenOffTimer_Tick_ScreenOff;
                screenOffTimer.Start();
                System.Console.Write("Timer is running");
                SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
            }
        }

        void screenOffTimer_Tick_ScreenOff(object sender, EventArgs e)
        {
            System.Console.Write("Eyes Closed For long time!");
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);


            //gotoSleepTimer.Interval = new TimeSpan(0, 0, 20);
            //gotoSleepTimer.Tick += gotoSleepTimer_Tick_SleepOff;
            //gotoSleepTimer.Start();

        }

        //void gotoSleepTimer_Tick_SleepOff(object sender, EventArgs e) 
        //{
        //    System.Windows.Forms.Application.SetSuspendState(PowerState.Suspend,false,false);
        //}

        public void timerStopping()
        {
            if (ctrlTimerStop <= 0)
            {
                //timer stop logic
                screenOffTimer.Stop();
                SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
                System.Console.Write("Timer stopped");
            }
        }
    }

此功能运行良好,但我的 WPF 窗口停止响应。那么你能告诉我如何在循环中调用函数的地方使用线程。

这是窗口的屏幕截图: WPF 窗口和生成事件的按钮是

4

1 回答 1

1

您可以使用线程或使用异步,但很难判断您是否正在触摸代码中的任何 UI

线:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem((o) =>
            {
                pipeline = new MyPipeline();
                if (isRunning)
                {
                    isRunning = false;
                    pipeline.PauseFaceLocation(true);
                    pipeline.Close();
                }
                else
                {
                    isRunning = true;
                    pipeline.LoopFrames();
                }
            });
    }

异步

    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {
       await Task.Factory.StartNew(() =>
            {
                pipeline = new MyPipeline();
                if (isRunning)
                {
                    isRunning = false;
                    pipeline.PauseFaceLocation(true);
                    pipeline.Close();
                }
                else
                {
                    isRunning = true;
                    pipeline.LoopFrames();
                }
            });
    }
于 2013-08-27T05:29:29.160 回答