Esempio n. 1
0
 private void button4_Click(object sender, EventArgs e)
 {
     if (textBox1.Text.Trim() != "" && pictureBox0.Image != null)
     {
         for (int i = 0; i < listface.Count; i++)
         {
             if (listface[i].face_name == textBox1.Text.Trim())
             {
                 DialogResult dr = MessageBox.Show("该用户名的人脸已经注册,确认将覆盖该人脸!", "提示", MessageBoxButtons.OKCancel);
                 if (dr == DialogResult.Cancel)
                 {
                     textBox1.Text = "";
                     return;
                 }
                 else
                 {
                     currentfacenum = i;
                 }
             }
         }
         DetectFaceFeature picboxdetectandextractfeature = GetDetectFaceFeature((Bitmap)pictureBox0.Image, pictureBox0, true);
         if (picboxdetectandextractfeature.faceflag)
         {
             facelibrary              = new FaceLibrary();
             facelibrary.face_name    = textBox1.Text.Trim();
             facelibrary.face_Feature = picboxdetectandextractfeature.face_Feature;
             if (currentfacenum > -1)    //覆盖当前人脸信息
             {
                 listface[currentfacenum] = facelibrary;
             }
             else
             {
                 listface.Add(facelibrary);
             }
             string listfacelibrary = JsonListToString(listface);
             FileWrite(filepath, listfacelibrary);
             MessageBox.Show("人脸注册成功!");
             textBox1.Text = "";
         }
         else
         {
             MessageBox.Show("未检测到人脸,人脸注册失败!");
         }
         currentfacenum = -1;
     }
     else
     {
         if (textBox1.Text.Trim() == "" && pictureBox0.Image == null)
         {
             MessageBox.Show("请输入注册人脸的用户名和图片!");
         }
         else if (textBox1.Text.Trim() == "")
         {
             MessageBox.Show("请输入注册人脸的用户名!");
         }
         else if (pictureBox0.Image == null)
         {
             MessageBox.Show("请输入人脸图片!");
         }
     }
 }
Esempio n. 2
0
        private void ImageGrabbedProcess(object sender, EventArgs arg)
        {
            try
            {
                Mat      mat = new Mat();
                DateTime now = DateTime.Now;
                capture.Retrieve(mat);   // 捕获摄像头图片
                Bitmap bitmap = mat.Bitmap;
                detectfacefeature = GetDetectFaceFeature(bitmap, pictureBox0, false);
                if (detectfacefeature.faceflag)
                {
                    //人脸识别
                    if (checkflag)
                    {
                        float similar = 0.0f;
                        int   num     = 0;
                        for (int i = 0; i < listface.Count; i++)
                        {
                            float facesimilar = recogitionAndFacePairMatching(listface[i].face_Feature, detectfacefeature.face_Feature);
                            Console.WriteLine("编号: " + i + " -- " + "人脸姓名: " + listface[i].face_name + " -- " + "相似度: " + facesimilar);
                            if (facesimilar > similar)
                            {
                                similar = facesimilar;
                                num     = i;
                            }
                        }
                        //刷新进度条
                        refresh = new Refresh(refreshprogressbar);
                        Invoke(refresh, (similar * 100), listface[num].face_name);
                        if (similar > changecheckvalue)
                        {
                            setControlText(label1, "识别结果: " + listface[num].face_name);
                            bool sendflag = false;
                            if (radioButton1.Checked)
                            {
                                sendflag = Serial_Device_API.GateAPI.GateSendData(Serial_Device_API.DeviceSerialPort.GateSerialPort, Serial_Device_API.GateAPI.gateleft);
                            }
                            else if (radioButton2.Checked)
                            {
                                sendflag = Serial_Device_API.GateAPI.GateSendData(Serial_Device_API.DeviceSerialPort.GateSerialPort, Serial_Device_API.GateAPI.gateright);
                            }
                            if (sendflag)
                            {
                                Console.WriteLine("数据发送成功!");
                            }
                            else
                            {
                                Console.WriteLine("数据发送失败!");
                            }
                        }
                        else
                        {
                            setControlText(label1, "识别结果: " + "无此人");
                        }
                    }
                    else
                    {
                        setControlText(label1, "识别结果: " + "未开启人脸识别");
                        //刷新进度条
                        refresh = new Refresh(refreshprogressbar);
                        Invoke(refresh, 0.00f, "");
                    }

                    //添加人脸
                    if (addflag)
                    {
                        pictureBox0.Image        = null;
                        facelibrary              = new FaceLibrary();
                        facelibrary.face_name    = textBox1.Text.Trim();
                        detectfacefeature        = GetDetectFaceFeature(bitmap, pictureBox0, true);
                        facelibrary.face_Feature = detectfacefeature.face_Feature;
                        if (currentfacenum > -1)    //覆盖当前人脸信息
                        {
                            listface[currentfacenum] = facelibrary;
                        }
                        else
                        {
                            listface.Add(facelibrary);
                        }
                        addflag        = false;
                        currentfacenum = -1;
                        string listfacelibrary = JsonListToString(listface);
                        FileWrite(filepath, listfacelibrary);
                        MessageBox.Show("人脸注册成功!");
                        //清除人脸用户名
                        refreshtext = new RefreshText(refreshTextbox);
                        Invoke(refreshtext, textBox1, "");
                    }
                }
                else
                {
                    if (checkflag)
                    {
                        setControlText(label1, "识别结果: " + "未检测到人脸");
                    }
                    if (addflag)
                    {
                        MessageBox.Show("未检测到人脸,人脸注册失败!");
                        addflag = false;
                    }
                }
                //设置显示图片
                imageBox0.Image = mat;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Esempio n. 3
0
        //检测人脸、提取特征
        private DetectFaceFeature GetDetectFaceFeature(Image imageParam, PictureBox picbox, bool cutimageflag)
        {
            DetectFaceFeature getdetectfacefeature = new DetectFaceFeature();

            Console.WriteLine("/********************* GetDetectFaceFeature Start*********************/");
            Console.WriteLine("  StartTime:{0}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff"));
            //检测人脸
            //Console.WriteLine("/********************* Face Delect Start *********************/");
            int    width   = 0;
            int    height  = 0;
            int    pitch   = 0;
            Bitmap bitmapx = new Bitmap(imageParam);

            byte[] imageData    = ArcSoft_FACE_API.FSDK_FACE_SHARE.ReadBmpToByte(bitmapx, ref width, ref height, ref pitch);
            IntPtr imageDataPtr = Marshal.AllocHGlobal(imageData.Length);

            Marshal.Copy(imageData, 0, imageDataPtr, imageData.Length);
            ArcSoft_FACE_API.FSDK_FACE_SHARE.ASVLOFFSCREEN offInput = new ArcSoft_FACE_API.FSDK_FACE_SHARE.ASVLOFFSCREEN();
            offInput.u32PixelArrayFormat = 513;
            offInput.ppu8Plane           = new IntPtr[4];
            offInput.ppu8Plane[0]        = imageDataPtr;
            offInput.i32Width            = width;
            offInput.i32Height           = height;
            offInput.pi32Pitch           = new int[4];
            offInput.pi32Pitch[0]        = pitch;
            IntPtr offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput));

            Marshal.StructureToPtr(offInput, offInputPtr, false);
            ArcSoft_FACE_API.FSDK_FACE_DETECTION.AFD_FSDK_FACERES detectface = new ArcSoft_FACE_API.FSDK_FACE_DETECTION.AFD_FSDK_FACERES();
            IntPtr    faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(detectface));
            Stopwatch watchTime  = new Stopwatch();

            watchTime.Start();
            //检测结果
            int detectResult = ArcSoft_FACE_API.FSDK_FACE_DETECTION.AFD_FSDK_StillImageFaceDetection(
                ArcSoft_FACE_API.FSDK_FACE_SHARE.detectEngine,
                offInputPtr,
                ref faceResPtr
                );

            watchTime.Stop();
            //Console.WriteLine(String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds));
            // Console.WriteLine("/********************* Face Delect End *********************/");
            detectface = (ArcSoft_FACE_API.FSDK_FACE_DETECTION.AFD_FSDK_FACERES)Marshal.PtrToStructure(faceResPtr, typeof(ArcSoft_FACE_API.FSDK_FACE_DETECTION.AFD_FSDK_FACERES));
            ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT[] rectArr = new ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT[detectface.nFace];
            if (detectface.nFace > 0)
            {
                getdetectfacefeature.faceflag = true;

                //人脸画框
                for (int j = 0; j < rectArr.Length; j++)
                {
                    rectArr[j] = (ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT)Marshal.PtrToStructure(detectface.rcFace + Marshal.SizeOf(typeof(ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT)) * j, typeof(ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT));
                }
                imageParam = ArcSoft_FACE_API.FSDK_FACE_SHARE.DrawRectangleInPicture2(imageParam, rectArr, Color.Red, 3, DashStyle.Dash);

                //截取人脸
                ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT rect = rectArr[0];
                if (cutimageflag)
                {
                    for (int j = 0; j < rectArr.Length; j++)
                    {
                        Image imgResult = ArcSoft_FACE_API.FSDK_FACE_SHARE.CutFace((Bitmap)imageParam, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
                        picbox.Image = imgResult;
                    }
                }

                //获取人脸特征值
                // Console.WriteLine("/********************* Face Recognition Start *********************/");
                ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_FACEINPUT faceResult = new ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_FACEINPUT();
                int orient = (int)Marshal.PtrToStructure(detectface.lfaceOrient, typeof(int));
                faceResult.lfaceOrient = orient;

                faceResult.rcFace = new ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT();
                rect = (ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT)Marshal.PtrToStructure(detectface.rcFace, typeof(ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT));
                faceResult.rcFace = rect;

                IntPtr faceResultPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceResult));
                Marshal.StructureToPtr(faceResult, faceResultPtr, false);
                ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_FACEMODEL localFaceModels = new ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_FACEMODEL();
                IntPtr localFaceModelsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(localFaceModels));

                watchTime.Start();
                int extractResult = ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_ExtractFRFeature(ArcSoft_FACE_API.FSDK_FACE_SHARE.recogitionEngine, offInputPtr, faceResultPtr, localFaceModelsPtr);
                watchTime.Stop();
                //Console.WriteLine(String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds));
                localFaceModels = (ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_FACEMODEL)Marshal.PtrToStructure(localFaceModelsPtr, typeof(ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_FACEMODEL));
                //Console.WriteLine("" + localFaceModels.lFeatureSize);

                if (localFaceModels.lFeatureSize > 0)
                {
                    getdetectfacefeature.face_Feature = new byte[localFaceModels.lFeatureSize];
                    Marshal.Copy(localFaceModels.pbFeature, getdetectfacefeature.face_Feature, 0, localFaceModels.lFeatureSize);
                }
                else
                {
                    Console.WriteLine("获取人脸特征值失败");
                }
                Console.WriteLine("/********************* Face Recognition End  *********************/");
                faceResult = new ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_FACEINPUT();
                Marshal.FreeHGlobal(faceResultPtr);
                rect            = new ArcSoft_FACE_API.FSDK_FACE_SHARE.MRECT();
                localFaceModels = new ArcSoft_FACE_API.FSDK_FACE_RECOGNITION.AFR_FSDK_FACEMODEL();
                Marshal.FreeHGlobal(localFaceModelsPtr);
            }
            else
            {
                picbox.Image = null;
            }
            bitmapx.Dispose();
            rectArr  = null;
            offInput = new ArcSoft_FACE_API.FSDK_FACE_SHARE.ASVLOFFSCREEN();
            Marshal.FreeHGlobal(offInputPtr);
            imageData = null;
            Marshal.FreeHGlobal(imageDataPtr);

            Console.WriteLine("    EndTime:{0}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff"));
            Console.WriteLine("/********************* GetDetectFaceFeature End *********************/");
            return(getdetectfacefeature);
        }