public static extern void GetInitialEyeCenter(out PixelPoint leftCenter_initial, out PixelPoint rightCenter_initial);
public static extern void GetEyeCenter(out PixelPoint leftEyeCenter, out PixelPoint rightEyeCenter, out PixelPoint leftRelocation, out PixelPoint rightRelocation);
void OnTracking() { //load_whiteimage(); while (IsRunning2) { int result0 = 0; int result1 = 0; bool resultrelocation; bool flag = true; if (a == 1) { TrackState = "Starting..."; } bool result; try { while (!StartTracking()) ; GetInitialEyeCenter(out leftCenter_initial, out rightCenter_initial); TrackState = "Tracking..."; int i = 0; int num_lost = 0; PixelPoint left_pre = new PixelPoint(); PixelPoint right_pre = new PixelPoint(); left_pre = leftCenter_initial; right_pre = rightCenter_initial; int left_pre_x = leftCenter_initial.X; int left_pre_y = leftCenter_initial.Y; int right_pre_x = rightCenter_initial.X; int right_pre_y = rightCenter_initial.Y; while (IsRunning2) { time1 = DateTime.Now; if (!ImageCapture()) { MessageBox.Show("Fail to capture image!", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); break; } use_time3 = (int)(DateTime.Now - time1).TotalMilliseconds + use_time3; result = Tracking(); use_time4 = (int)(DateTime.Now - time1).TotalMilliseconds + use_time4; resultrelocation = Result_Relocation_aa(); if (resultrelocation == false) result0++; resultrelocation = Result_Relocation_bb(); if (resultrelocation == false) result1++; use_time5 = (int)(DateTime.Now - time1).TotalMilliseconds + use_time5; GetEyeCenter(out leftEyeCenter, out rightEyeCenter, out leftRelocation, out rightRelocation); //两眼基本应处于水平位置,此外,人眼跟丢时,meanshift数值一直不变 if (abs(leftEyeCenter.Y - rightEyeCenter.Y) > 50 || abs(leftRelocation.Y - rightRelocation.Y) > 50) { TrackState = "Relocation"; flag = false; a = 2; } else if (left_pre.Equals(leftEyeCenter) && right_pre.Equals(rightEyeCenter)) { num_lost++; if (num_lost > 150) { num_lost = 0; TrackState = "Relocation"; flag = false; a = 2; } } left_pre = leftEyeCenter; right_pre = rightEyeCenter; use_time6 = (int)(DateTime.Now - time1).TotalMilliseconds + use_time6; //if (result0 > 10 || result1 > 10) //{ // TrackState = "Relocation"; // result0 = 0; // result1 = 0; // flag = false; // a = 2; //} use_time1 = (int)(DateTime.Now - time1).TotalMilliseconds + use_time1; UIUpdate(flag); use_time2 = (int)(DateTime.Now - time1).TotalMilliseconds + use_time2; if (i < 20) i++; else i = 100; if (flag == false) break; } } //发现异常,终止该进程 catch (System.Exception ex) { if (TrackingThread.IsAlive) { TrackingThread.Abort(); TrackState = "出Bug了..."; //重新开始 //TrackingThread = new Thread(new ThreadStart(OnTracking)); TrackingThread = new Thread(new ThreadStart(delegate { RecordAvi("test.avi"); })); TrackingThread.SetApartmentState(ApartmentState.STA); TrackingThread.IsBackground = true; TrackingThread.Start(); } } } }