问题描述
我的WPF窗口冻结后,我点击一个按钮来执行监视用户的脸和eyes.I使用英特尔感知计算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
{
//核心变量
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;
//状态
pxcmStatus locationStatus;
pxcmStatus landmarkStatus;
pxcmStatus attributeStatus;
public bool takeRecoSnapshot = false;
//表单变量
MainWindow parent;
位图lastProcessedBitmap;
//属性数组
uint [] blink = new uint [2];
// PXCM变量
PXCMFaceAnalysis faceAnalysis;
PXCMSession会话;
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;
$ b $ public MyPipeline()
{
lastProcessedBitmap = new Bitmap(640,480);
attributeProfile = new PXCMFaceAnalysis.Attribute.ProfileInfo();
EnableImage(PXCMImage.ColorFormat.COLOR_FORMAT_RGB24);
EnableFaceLocation();
EnableFaceLandmark();
$ b public override bool OnNewFrame()
{
faceAnalysis = QueryFace();
faceAnalysis.QueryFace(fidx,out faceId,out timeStamp);
//获取脸部位置
faceLocation =(PXCMFaceAnalysis.Detection)faceAnalysis.DynamicCast(PXCMFaceAnalysis.Detection.CUID);
locationStatus = faceLocation.QueryData(faceId,out faceLocationData);
//获取人脸地标(眼睛,嘴巴,鼻子位置等)
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);
//获取脸部属性(眨眼)
faceAttributes =(PXCMFaceAnalysis.Attribute)faceAnalysis.DynamicCast(PXCMFaceAnalysis.Attribute.CUID);
faceAttributes.QueryProfile(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED,0,out attributeProfile);
attributeProfile.threshold = 50; //必须在这里!
faceAttributes.SetProfile(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED,ref attributeProfile);
attributeStatus = faceAttributes.QueryData(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED,faceId,out blink);
ComputationOfTimer();
返回true;
和 ComputationOfTimer(); 每一帧当眼睛关闭一段时间时启动一个计时器。
private void ComputationOfTimer()
{
if(blink [0] == 100)//如果闭眼检测到
{
ctrlTimerStop = 3;
ctrlTimerStart = ctrlTimerStart - 1;
System.Console.Write(\\\
\t Eyes Closed);
timerStarting();
}
else //如果眼睛打开,我们必须停止计时器
{
ctrlTimerStart = 5;
ctrlTimerStop - = 1;
System.Console.Write(\\\
\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);
$ b无效screenOffTimer_Tick_ScreenOff(对象发件人,EventArgs e)
{
System.Console.Write(闭上眼睛很长时间! );
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)
{
//定时器停止逻辑
screenOffTimer.Stop();
SendMessage(HWND_BROADCAST,WM_SYSCOMMAND,SC_MONITORPOWER,MONITOR_ON);
System.Console.Write(Timer stopped);
$ b $ p这个函数执行得很好,我的WPF窗口停止响应。
所以你可以告诉我如何使用线程函数在循环中调用。
$ b $ p这是Window的截图:
线程:
pre $ 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();
}
});
$ b $异步
$ pre $ 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();
}
});
}
My WPF window freezes after I click on a button to perform monitoring on user's face and eyes.I am using Intel Perceptual Computing SDK for this purpose.
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();
}
}
And this Util[M]Pipeline is used to identify face frame by frame.After Face is Identified,my purpose here to get state of eyes(Closed/Open).
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;
}
And ComputationOfTimer(); function is called on each frame which starts a timer when eyes are closed for a certain duration.
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");
}
}
}
This function is performing fine but my WPF window stops responding.So can you please tell me how to use threading where Functions are called in a loop.
This is the screenshot of Window:
解决方案 You could Thread or use Async, but its hard to tell if you are touching any UI in your code
Thread:
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();
}
});
}
Async
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();
}
});
}
这篇关于WPF GUI停止响应,因为在循环中调用了一些函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-20 16:11