ボタンをクリックしてユーザーの顔と目を監視すると、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 ウィンドウが応答しなくなります。関数がループで呼び出されるスレッドの使用方法を教えてください。
これはウィンドウのスクリーンショットです: