Newer
Older
IRIS_REFACTOR / irisRefactor / IrisThread / PreIdentifyTh.cs
TAN YUE on 2 Jul 2021 17 KB 20210702 重新修改为3个线程
using irisHelper;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace irisRefactor.IrisThread
{
    /**
     * 识别之前的线程
     * 找眼和质量评估两步操作串行进行
     * 
     */    
    class PreIdentifyTh
    {
        private PreIdentifyTh() { }

        private static readonly PreIdentifyTh preIdentifyTh = new PreIdentifyTh();
        
        public static PreIdentifyTh GetInstance { get => preIdentifyTh; }

        public void FindEyes()
        {
            LogHelper.WriteInfoLog(MethodBase.GetCurrentMethod().DeclaringType, "PreIdentify Thread Started");

            //ProMemory.Tag_SleepTimer = 0;
            //ProMemory.irisConfig.EyeFinderThreadRun = true;

            // 用于计算操作耗时
            Stopwatch sw = new Stopwatch();

            // 没有在休眠中;没有数据同步中
            while (ProMemory.isWait == false && ProMemory.isSyning == false)
            {
                // 没有正在执行的找眼、识别线程
                if (ProMemory.identifyConfig.FlagFindingEye == false)
                {
                    if (ProMemory.irisConfig.QueueFace.Count > 0)
                    {
                        lock (ProMemory.irisConfig.QueueFace)
                        {
                            if (ProMemory.irisConfig.QueueFace.Count > 0)
                            {
                                ProMemory.irisConfig.FaceBuffer = ProMemory.irisConfig.QueueFace.Pop();
                            }
                        }

                        try
                        {
                            unsafe
                            {
                                // 申请内存,复制内存
                                IntPtr ptrFace = Marshal.AllocHGlobal(1280 * 960);
                                Marshal.Copy(ProMemory.irisConfig.FaceBuffer, 0, ptrFace, 1280 * 960);

                                // mark = 找到的眼睛数量
                                int mark;
                                // 左右眼睛的定位参数:x,y,r
                                int[] posvec = new int[] { 0, 0, 0, 0, 0, 0 };

                                fixed (int* pos = &posvec[0])
                                {
                                    int* ptrPos = pos;

                                    sw.Restart();
                                    mark = ProMemory.CaptureEye_Rec(ptrFace, ref ptrPos); // 调用找眼算法
                                    sw.Stop();
                                    LogHelper.WriteDebugLog(MethodBase.GetCurrentMethod().DeclaringType, "找眼[" + mark + "]:" + sw.ElapsedMilliseconds);

                                    int[] irisPos = { ptrPos[0], ptrPos[1], ptrPos[2], ptrPos[3], ptrPos[4], ptrPos[5] };

                                    if (mark == 1 || mark == 2)
                                    {
                                        // 1.持续没有找到眼的计数器清零
                                        ProMemory.identifyConfig.CountNoEyeLast = 0;

                                        ProMemory.identifyConfig.FlagFoundEye = true;

                                        // 一次识别过程中首次找到眼,标记开始一次新的识别
                                        if (ProMemory.identifyConfig.IdentifyTaskId == "")
                                        {
                                            ProMemory.identifyConfig.IdentifyTaskId = DateTime.Now.ToString("yyyyMMddHHmmss");

                                            // 2.黄灯闪烁
                                            ProMemory.IoControllService.setYellowFlash(true);
                                            Thread flashThrd = new Thread(ProMemory.IoControllService.YellowFlash);
                                            flashThrd.Start();

                                            // 开启识别线程
                                            ProMemory.irisConfig.Key_Identify = 1;
                                            ProMemory.irisConfig.IdentifyThreadRun = true;

                                            // 初始化识别线程
                                            Thread thIdentify = new Thread(IdentifyTh.GetInstance.Identify);
                                            thIdentify.Start();
                                        }

                                        // 3.质量评估
                                        int score = 0;
                                        Byte[] irisBytesL = new byte[640 * 480];
                                        Byte[] irisBytesR = new byte[640 * 480];

                                        IntPtr ptrIrisFace = Marshal.AllocHGlobal(1280 * 960);
                                        Marshal.Copy(ProMemory.irisConfig.FaceBuffer, 0, ptrIrisFace, 1280 * 960);

                                        unsafe
                                        {
                                            fixed (byte* irisL = &irisBytesL[0])
                                            {
                                                fixed (byte* irisR = &irisBytesR[0])
                                                {
                                                    byte* ptrIrisL = irisL;
                                                    byte* ptrIrisR = irisR;

                                                    sw.Restart();
                                                    score = ProMemory.AssessFocus_Rec(ptrIrisFace, irisPos, ref ptrIrisL, ref ptrIrisR);  //质量评估 0均不合格 1一幅合格 2均合格
                                                    sw.Stop();
                                                    LogHelper.WriteDebugLog(MethodBase.GetCurrentMethod().DeclaringType, "质量评估[" + score + "]:" + sw.ElapsedMilliseconds);
                                                }
                                            }
                                        }


                                        if (score > 0)
                                        {
                                            if (score == 1)
                                            {
                                                lock (ProMemory.irisConfig.Q4)
                                                {
                                                    ProMemory.irisConfig.Q4.Push(irisBytesL);
                                                }
                                            }
                                            else if (score == 2)
                                            {
                                                lock (ProMemory.irisConfig.Q4)
                                                {
                                                    ProMemory.irisConfig.Q4.Push(irisBytesL);
                                                    ProMemory.irisConfig.Q4.Push(irisBytesR);
                                                }
                                            }


                                            //if (ProMemory.irisConfig.Key_Identify == 0)
                                            //{
                                            //    ProMemory.irisConfig.Key_Identify = 1;

                                            //    ProMemory.irisConfig.IdentifyThreadRun = true;

                                            //    // 初始化识别线程
                                            //    Thread thIdentify = new Thread(IdentifyTh.GetInstance.Identify);
                                            //    thIdentify.Start();
                                            //}

                                        }

                                        Marshal.FreeHGlobal(ptrIrisFace);
                                    }
                                    else
                                    {
                                        ProMemory.identifyConfig.CountNoEyeLast++;
                                    }
                                }
                            }
                        } 
                        catch (Exception ex)
                        {
                            LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "眼睛搜索线程CatchError:" + ex);
                        }
                    }
                }
            }

            //try
            //{
            //    // 条件:找眼线程在运行中;休眠线程标志为0;不在数据同步;连续未找到眼睛数小于100
            //    while (ProMemory.irisConfig.EyeFinderThreadRun && ProMemory.Tag_SleepTimer == 0 && !ProMemory.isSyning && ProMemory.identifyConfig.CountNoEyeLast <= 100)
            //    {
            //        //M++;
            //        if (ProMemory.irisConfig.QueueFace.Count > 0)
            //        {
            //            lock (ProMemory.irisConfig.QueueFace)
            //            {
            //                if (ProMemory.irisConfig.QueueFace.Count > 0)
            //                {
            //                    ProMemory.irisConfig.FaceBuffer = ProMemory.irisConfig.QueueFace.Pop();
            //                }
            //            }

            //            if (ProMemory.irisConfig.FaceBuffer != null)
            //            {
            //                unsafe
            //                {
            //                    // 申请内存,复制内存
            //                    IntPtr ptrFace = Marshal.AllocHGlobal(1280 * 960);
            //                    Marshal.Copy(ProMemory.irisConfig.FaceBuffer, 0, ptrFace, 1280 * 960);

            //                    // mark = 找到的眼睛数量
            //                    int mark;
            //                    // 左右眼睛的定位参数:x,y,r
            //                    int[] posvec = new int[] { 0, 0, 0, 0, 0, 0 };

            //                    fixed (int* pos = &posvec[0])
            //                    {
            //                        int* ptrPos = pos;

            //                        sw.Restart();
            //                        mark = ProMemory.CaptureEye_Rec(ptrFace, ref ptrPos); // 调用找眼算法
            //                        sw.Stop();
            //                        LogHelper.WriteDebugLog(MethodBase.GetCurrentMethod().DeclaringType, "执行一次找眼操作[" + mark + "]:" + sw.ElapsedMilliseconds);

            //                        int[] irisPos = { ptrPos[0], ptrPos[1], ptrPos[2], ptrPos[3], ptrPos[4], ptrPos[5] };

            //                        // 找到1个或者2个眼睛
            //                        if (mark == 1 || mark == 2)
            //                        {
            //                            // 1.持续没有找到眼的计数器清零
            //                            ProMemory.identifyConfig.CountNoEyeLast = 0;

            //                            // 2.黄灯闪烁
            //                            //ProMemory.IoControllService.setYellowFlash(true);
            //                            //Thread flashThrd = new Thread(ProMemory.IoControllService.YellowFlash);
            //                            //flashThrd.Start();

            //                            // 3.质量评估
            //                            int score = 0;
            //                            Byte[] irisBytesL = new byte[640 * 480];
            //                            Byte[] irisBytesR = new byte[640 * 480];

            //                            IntPtr ptrIrisFace = Marshal.AllocHGlobal(1280 * 960);
            //                            Marshal.Copy(ProMemory.irisConfig.FaceBuffer, 0, ptrIrisFace, 1280 * 960);

            //                            unsafe
            //                            {
            //                                fixed (byte* irisL = &irisBytesL[0])
            //                                {
            //                                    fixed (byte* irisR = &irisBytesR[0])
            //                                    {
            //                                        byte* ptrIrisL = irisL;
            //                                        byte* ptrIrisR = irisR;

            //                                        sw.Restart();
            //                                        score = ProMemory.AssessFocus_Rec(ptrIrisFace, irisPos, ref ptrIrisL, ref ptrIrisR);  //质量评估 0均不合格 1一幅合格 2均合格
            //                                        sw.Stop();
            //                                        LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "执行一次质量评估操作[" + score + "]:" + sw.ElapsedMilliseconds);
            //                                    }
            //                                }
            //                            }


            //                            if (score > 0)
            //                            {
            //                                if (score == 1)
            //                                {
            //                                    lock (ProMemory.irisConfig.Q4)
            //                                    {
            //                                        ProMemory.irisConfig.Q4.Push(irisBytesL);
            //                                    }
            //                                }
            //                                else if (score == 2)
            //                                {
            //                                    lock (ProMemory.irisConfig.Q4)
            //                                    {
            //                                        ProMemory.irisConfig.Q4.Push(irisBytesL);
            //                                        ProMemory.irisConfig.Q4.Push(irisBytesR);
            //                                    }
            //                                }

                                            
            //                                if (ProMemory.irisConfig.Key_Identify == 0)
            //                                {
            //                                    ProMemory.irisConfig.Key_Identify = 1;

            //                                    ProMemory.irisConfig.IdentifyThreadRun = true;
            //                                    //Thread m_IdentifyThread = new Thread(IdentifyTh.GetInstance().Identify);
            //                                    //m_IdentifyThread.Name = "Identify";
            //                                    //m_IdentifyThread.Start();
            //                                }
                                            
            //                            }

            //                            Marshal.FreeHGlobal(ptrIrisFace);
            //                        }
            //                        else
            //                        {
            //                            ProMemory.identifyConfig.CountNoEyeLast++;
            //                        }
            //                    }

            //                    Marshal.FreeHGlobal(ptrFace);
            //                }
            //            }

            //            lock (ProMemory.irisConfig.Q2)
            //            {
            //                if (ProMemory.irisConfig.Q2.Count >= 30)
            //                {
            //                    ProMemory.irisConfig.Q2.Clear(); //手动控制堆栈容量
            //                }
            //            }

            //            lock (ProMemory.irisConfig.Q3)
            //            {
            //                if (ProMemory.irisConfig.Q3.Count >= 30)
            //                {
            //                    ProMemory.irisConfig.Q3.Clear(); //手动控制堆栈容量                   
            //                }
            //            }
            //        }
            //    }

            //    //if (ProMemory.Tag_SleepTimer == 1) //待机时间到,仍未找到眼睛
            //    if (ProMemory.identifyConfig.CountNoEyeLast > 100)
            //    {
            //        if (ProMemory.m_bGrabbing)
            //        {
            //            if (ProMemory.IoControllService.getSensorVal() == 0) //未再次触发,待机
            //            {
            //                if (ProMemory.formType == "1")
            //                    Form1.GetInstance().ReInitializeToSleep();
            //                else if (ProMemory.formType == "2")
            //                    Form2.GetInstance().ReInitializeToSleep();
            //                Thread.Sleep(200);
            //                ProMemory.cameraController.ContinuousShot_TriggerOn();
            //            }
            //            else //已再次触发,继续下一轮识别
            //            {
            //                ProMemory.cameraController.ContinuousShot_TriggerOff();
            //            }
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    LogHelper.WriteErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "眼睛搜索线程CatchError:" + ex);
            //}
        }
    }
}