Exemple #1
0
        //检测人脸、提取特征
        private byte[] detectAndExtractFeature(Image imageParam, int firstSecondFlg)
        {
            byte[] feature = null;

            try
            {
                Console.WriteLine();

                Console.WriteLine("############### Face Detect Start #########################");

                int width = 0;

                int height = 0;

                int pitch = 0;

                Bitmap bitmap = new Bitmap(imageParam);

                byte[] imageData = getBGR(bitmap, ref width, ref height, ref pitch);

                //GCHandle hObject = GCHandle.Alloc(imageData, GCHandleType.Pinned);

                //IntPtr imageDataPtr = hObject.AddrOfPinnedObject();

                IntPtr imageDataPtr = Marshal.AllocHGlobal(imageData.Length);

                Marshal.Copy(imageData, 0, imageDataPtr, imageData.Length);

                ASVLOFFSCREEN offInput = new 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;

                AFD_FSDK_FACERES faceRes = new AFD_FSDK_FACERES();

                IntPtr offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput));

                Marshal.StructureToPtr(offInput, offInputPtr, false);

                IntPtr faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceRes));

                //Marshal.StructureToPtr(faceRes, faceResPtr, false);

                Console.WriteLine("StartTime:{0}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff"));

                Stopwatch watchTime = new Stopwatch();

                watchTime.Start();
                //人脸检测
                int detectResult = AmFaceVerify.AFD_FSDK_StillImageFaceDetection(detectEngine, offInputPtr, ref faceResPtr);

                watchTime.Stop();

                if (firstSecondFlg == 1)
                {
                    setControlText(this.label5, String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds));

                    //this.label5.Text = String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds);
                }
                else if (firstSecondFlg == 2)
                {
                    setControlText(this.label2, String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds));

                    //this.label2.Text = String.Format("检测耗时:{0}ms", watchTime.ElapsedMilliseconds);
                }

                object obj = Marshal.PtrToStructure(faceResPtr, typeof(AFD_FSDK_FACERES));

                faceRes = (AFD_FSDK_FACERES)obj;

                Console.WriteLine("  Face Count:{0}", faceRes.nFace);

                for (int i = 0; i < faceRes.nFace; i++)
                {
                    MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace + Marshal.SizeOf(typeof(MRECT)) * i, typeof(MRECT));

                    int orient = (int)Marshal.PtrToStructure(faceRes.lfaceOrient + Marshal.SizeOf(typeof(int)) * i, typeof(int));

                    if (i == 0)
                    {
                        Image image = CutFace(bitmap, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);

                        if (firstSecondFlg == 1)
                        {
                            this.pictureBox3.Image = image;
                        }
                        else if (firstSecondFlg == 2)
                        {
                            this.pictureBox4.Image = image;
                        }
                    }

                    Console.WriteLine("    left:{0} top:{1} right:{2} bottom:{3} orient:{4}", rect.left, rect.top, rect.right, rect.bottom, orient);
                }

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

                Console.WriteLine("############### Face Detect End   #########################");

                if (faceRes.nFace > 0)
                {
                    Console.WriteLine();

                    Console.WriteLine("############### Face Recognition Start #########################");

                    AFR_FSDK_FACEINPUT faceResult = new AFR_FSDK_FACEINPUT();

                    int orient = (int)Marshal.PtrToStructure(faceRes.lfaceOrient, typeof(int));

                    faceResult.lfaceOrient = orient;

                    faceResult.rcFace = new MRECT();

                    MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace, typeof(MRECT));

                    faceResult.rcFace = rect;

                    IntPtr faceResultPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceResult));

                    Marshal.StructureToPtr(faceResult, faceResultPtr, false);

                    AFR_FSDK_FACEMODEL localFaceModels = new AFR_FSDK_FACEMODEL();

                    IntPtr localFaceModelsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(localFaceModels));

                    //Marshal.StructureToPtr(localFaceModels, localFaceModelsPtr, false);

                    watchTime.Start();

                    int extractResult = AmFaceVerify.AFR_FSDK_ExtractFRFeature(regcognizeEngine, offInputPtr, faceResultPtr, localFaceModelsPtr);

                    Marshal.FreeHGlobal(faceResultPtr);

                    Marshal.FreeHGlobal(offInputPtr);

                    watchTime.Stop();

                    if (firstSecondFlg == 1)
                    {
                        setControlText(this.label3, String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds));

                        //this.label3.Text = String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds);
                    }
                    else if (firstSecondFlg == 2)
                    {
                        setControlText(this.label4, String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds));

                        //this.label4.Text = String.Format("抽取特征耗时:{0}ms", watchTime.ElapsedMilliseconds);
                    }

                    object objFeature = Marshal.PtrToStructure(localFaceModelsPtr, typeof(AFR_FSDK_FACEMODEL));

                    Marshal.FreeHGlobal(localFaceModelsPtr);

                    localFaceModels = (AFR_FSDK_FACEMODEL)objFeature;

                    feature = new byte[localFaceModels.lFeatureSize];

                    Marshal.Copy(localFaceModels.pbFeature, feature, 0, localFaceModels.lFeatureSize);

                    localFaceModels = new AFR_FSDK_FACEMODEL();

                    Console.WriteLine("############### Face Recognition End   #########################");
                }

                bitmap.Dispose();

                imageData = null;

                Marshal.FreeHGlobal(imageDataPtr);

                offInput = new ASVLOFFSCREEN();

                faceRes = new AFD_FSDK_FACERES();

                //Marshal.FreeHGlobal(faceResPtr);
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog("detect", e.Message + "\n" + e.StackTrace);
            }
            return(feature);
        }
Exemple #2
0
        //加载摄像头图像
        public void DisplayAddProcess()
        {
            try
            {
                while (!this._stop)
                {
                    Image <Bgr, byte> frame = this.capture.QueryFrame();

                    if (frame != null)
                    {
                        using (Bitmap im = frame.Clone().ToBitmap())
                        {
                            im.RotateFlip(RotateFlipType.RotateNoneFlipX);

                            Bitmap tempBitmap = im.Clone(new RectangleF(0, 0, im.Width, im.Height), PixelFormat.Format24bppRgb);

                            //this.setPictureBoxControlImage(this.pictureBox1, new Bitmap(tempBitmap));

                            byte[] imageData = getBGR(tempBitmap, ref width, ref height, ref pitch);

                            IntPtr imageDataPtr = Marshal.AllocHGlobal(imageData.Length);

                            Marshal.Copy(imageData, 0, imageDataPtr, imageData.Length);

                            ASVLOFFSCREEN offInput = new 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;

                            AFT_FSDK_FACERES faceRes = new AFT_FSDK_FACERES();

                            IntPtr offInputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(offInput));

                            Marshal.StructureToPtr(offInput, offInputPtr, false);

                            IntPtr faceResPtr = Marshal.AllocHGlobal(Marshal.SizeOf(faceRes));

                            //人脸检测
                            int detectResult = AmFaceVerify.AFT_FSDK_FaceFeatureDetect(traceEngine, offInputPtr, ref faceResPtr);

                            if (detectResult != 0)
                            {
                                this.setPictureBoxControlImage(this.pictureBox1, new Bitmap(tempBitmap));

                                continue;
                            }

                            //Console.WriteLine("  detect Result:" + detectResult);

                            object obj = Marshal.PtrToStructure(faceResPtr, typeof(AFT_FSDK_FACERES));

                            faceRes = (AFT_FSDK_FACERES)obj;
                            //Console.WriteLine("  Face Count:{0}", faceRes.nFace);

                            if (faceRes.nFace > maxFaceCount)
                            {
                                this.setPictureBoxControlImage(this.pictureBox1, new Bitmap(tempBitmap));

                                continue;
                            }

                            Rectangle[] faces = new Rectangle[faceRes.nFace];

                            for (int i = 0; i < faceRes.nFace; i++)
                            {
                                MRECT rect = (MRECT)Marshal.PtrToStructure(faceRes.rcFace + Marshal.SizeOf(typeof(MRECT)) * i, typeof(MRECT));

                                //Console.WriteLine("    left:{0} top:{1} right:{2} bottom:{3}", rect.left, rect.top, rect.right, rect.bottom);

                                faces[i] = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
                            }

                            if (faceRes.nFace > 0)
                            {
                                this.setPictureBoxControlImage(this.pictureBox1, drawFaces(tempBitmap, faces));

                                //  this.setPictureBoxControlImage(this.pictureBox2, drawFaces(tempBitmap, faces));
                                #region xhz

                                if (IsPhoto)
                                {
                                    Task task1 = Task.Run(() =>
                                    {
                                        pictureBox2.Image = drawFaces((Bitmap)tempBitmap.Clone(), (Rectangle[])faces.Clone());

                                        UploadPicture();
                                    });
                                    // task1.Wait();

                                    // Bitmap jpg=new Bitmap((Image)pictureBox2.Image.Clone());
                                    //pictureBox2.Image = CutFace(tempBitmap, faces[0].X,faces[0].Y,faces[0].Width,faces[0].Height);


                                    IsPhoto = false;
                                }
                                //  Image image = CutFace(bitmap, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);

                                #endregion
                            }
                            else
                            {
                                this.setPictureBoxControlImage(this.pictureBox1, new Bitmap(tempBitmap));
                            }

                            Marshal.FreeHGlobal(offInputPtr);

                            imageData = null;

                            Marshal.FreeHGlobal(imageDataPtr);

                            offInput = new ASVLOFFSCREEN();

                            faceRes = new AFT_FSDK_FACERES();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                LogHelper.WriteErrorLog("DisplayAddProcess", ex.Message + "\n" + ex.StackTrace);
            }
        }