Example #1
0
        /// <summary>
        /// 提取人脸特征
        /// </summary>
        /// <param name="pEngine">引擎Handle</param>
        /// <param name="imageInfo">图像数据</param>
        /// <param name="multiFaceInfo">人脸检测结果</param>
        /// <returns>保存人脸特征结构体指针</returns>
        public static IntPtr ExtractFeature(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out ASF_SingleFaceInfo singleFaceInfo)
        {
            singleFaceInfo            = new ASF_SingleFaceInfo();
            singleFaceInfo.faceRect   = MemoryUtil.PtrToStructure <MRECT>(multiFaceInfo.faceRects);
            singleFaceInfo.faceOrient = MemoryUtil.PtrToStructure <int>(multiFaceInfo.faceOrients);
            IntPtr pSingleFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_SingleFaceInfo>());                                                                                     // 申请内存,单人脸检测结构体

            MemoryUtil.StructureToPtr(singleFaceInfo, pSingleFaceInfo);                                                                                                               // 结构体进入内存

            IntPtr pFaceFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>());                                                                                           // 申请内存,人脸特征结构体
            int    retCode      = ASFFunctions.ASFFaceFeatureExtract(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pSingleFaceInfo, pFaceFeature); // 单人脸特征提取

            Console.WriteLine("FR Extract Feature result:" + retCode);                                                                                                                // 直接打印结果

            if (retCode != 0)                                                                                                                                                         // 返回结果非0,即匹配到人脸,释放指针
            {
                // 释放指针
                MemoryUtil.Free(pSingleFaceInfo);                                                         // 释放单人脸检测结构体指针
                MemoryUtil.Free(pFaceFeature);                                                            // 释放人脸特征结构体指针
                ASF_FaceFeature emptyFeature  = new ASF_FaceFeature();                                    // 声明空人脸特征结构体
                IntPtr          pEmptyFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); // 声明空人脸特征结构体指针
                MemoryUtil.StructureToPtr(emptyFeature, pEmptyFeature);                                   // 将数据从托管对象封送到非托管内存块
                return(pEmptyFeature);                                                                    // 匹配成功,返回空的人脸特征结构体指针
            }

            // 人脸特征feature过滤
            ASF_FaceFeature faceFeature = MemoryUtil.PtrToStructure <ASF_FaceFeature>(pFaceFeature);// 将ptr托管的内存转化为结构体对象
            int             cc          = faceFeature.featureSize;
            var             ff          = faceFeature.feature;

            byte[] feature = new byte[faceFeature.featureSize];

            MemoryUtil.Copy(faceFeature.feature, feature, 0, faceFeature.featureSize);// source, destination, startIndex, length

            ASF_FaceFeature localFeature = new ASF_FaceFeature();

            localFeature.feature = MemoryUtil.Malloc(feature.Length);                        // 申请本地人脸特征指针
            MemoryUtil.Copy(feature, 0, localFeature.feature, feature.Length);               // source, startIndex, destination, length
            localFeature.featureSize = feature.Length;                                       // 设置本地特征值长度
            IntPtr pLocalFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>()); // 申请本地特征值指针空间

            MemoryUtil.StructureToPtr(localFeature, pLocalFeature);                          // T t,IntPtr ptr

            // 释放指针
            MemoryUtil.Free(pSingleFaceInfo);
            MemoryUtil.Free(pFaceFeature);


            /******** 特征值feature存入数据库 start **********/
            Users users = new Users();

            users.Id      = SidUtils.sid();
            users.Feature = feature;
            MysqlUtils mysqlUtils = new MysqlUtils();

            mysqlUtils.InsertUserFaceFeature(users);
            /******** 特征值feature存入数据库 end **********/
            return(pLocalFeature);// 返回人脸特征
        }
Example #2
0
        /// <summary>
        /// 获取图片信息
        /// </summary>
        /// <param name="image">图片</param>
        /// <returns>成功或失败</returns>
        public static ImageInfo ReadBMP(Image image)
        {
            ImageInfo imageInfo = new ImageInfo();

            //将Image转换为Format24bppRgb格式的BMP
            Bitmap     bm   = new Bitmap(image);
            BitmapData data = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            try
            {
                //位图中第一个像素数据的地址。它也可以看成是位图中的第一个扫描行
                IntPtr ptr = data.Scan0;

                //定义数组长度
                int    soureBitArrayLength = data.Height * Math.Abs(data.Stride);
                byte[] sourceBitArray      = new byte[soureBitArrayLength];

                //将bitmap中的内容拷贝到ptr_bgr数组中
                MemoryUtil.Copy(ptr, sourceBitArray, 0, soureBitArrayLength);

                //填充引用对象字段值
                imageInfo.width  = data.Width;
                imageInfo.height = data.Height;
                imageInfo.format = ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8;

                //获取去除对齐位后度图像数据
                int    line         = imageInfo.width * 3;
                int    pitch        = Math.Abs(data.Stride);
                int    bgr_len      = line * imageInfo.height;
                byte[] destBitArray = new byte[bgr_len];

                /*
                 * 图片像素数据在内存中是按行存储,一般图像库都会有一个内存对齐,在每行像素的末尾位置
                 * 每行的对齐位会使每行多出一个像素空间(三通道如RGB会多出3个字节,四通道RGBA会多出4个字节)
                 * 以下循环目的是去除每行末尾的对齐位,将有效的像素拷贝到新的数组
                 */
                for (int i = 0; i < imageInfo.height; ++i)
                {
                    Array.Copy(sourceBitArray, i * pitch, destBitArray, i * line, line);
                }

                imageInfo.imgData = MemoryUtil.Malloc(destBitArray.Length);
                MemoryUtil.Copy(destBitArray, 0, imageInfo.imgData, destBitArray.Length);

                return(imageInfo);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                bm.UnlockBits(data);
            }

            return(null);
        }
Example #3
0
        /// <summary>
        /// 提取单人脸特征
        /// </summary>
        /// <param name="pEngine">人脸识别引擎</param>
        /// <param name="image">图片</param>
        /// <param name="singleFaceInfo">单人脸信息</param>
        /// <returns>单人脸特征</returns>
        public static IntPtr ExtractFeature(IntPtr pEngine, Image image, ASF_SingleFaceInfo singleFaceInfo)
        {
            ImageInfo imageInfo       = ImageUtil.ReadBMP(image);                                    // 读取图片
            IntPtr    pSingleFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_SingleFaceInfo>()); // 申请单人脸结构体指针空间

            MemoryUtil.StructureToPtr(singleFaceInfo, pSingleFaceInfo);                              // 指针化

            IntPtr pFaceFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>());          // 申请人脸特征指针空间
            int    retCode      = -1;

            try
            {
                retCode = ASFFunctions.ASFFaceFeatureExtract(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pSingleFaceInfo, pFaceFeature);// 单人脸特征提取
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("FR Extract Feature result:" + retCode);

            if (retCode != 0)
            {
                //释放指针
                MemoryUtil.Free(pSingleFaceInfo);
                MemoryUtil.Free(pFaceFeature);
                ASF_FaceFeature emptyFeature  = new ASF_FaceFeature();
                IntPtr          pEmptyFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>());
                MemoryUtil.StructureToPtr(emptyFeature, pEmptyFeature);
                return(pEmptyFeature);
            }

            //人脸特征feature过滤
            ASF_FaceFeature faceFeature = MemoryUtil.PtrToStructure <ASF_FaceFeature>(pFaceFeature);

            byte[] feature = new byte[faceFeature.featureSize];
            MemoryUtil.Copy(faceFeature.feature, feature, 0, faceFeature.featureSize);

            ASF_FaceFeature localFeature = new ASF_FaceFeature();

            localFeature.feature = MemoryUtil.Malloc(feature.Length);
            MemoryUtil.Copy(feature, 0, localFeature.feature, feature.Length);
            localFeature.featureSize = feature.Length;
            IntPtr pLocalFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>());

            MemoryUtil.StructureToPtr(localFeature, pLocalFeature);

            //释放指针
            MemoryUtil.Free(pSingleFaceInfo);
            MemoryUtil.Free(pFaceFeature);
            MemoryUtil.Free(imageInfo.imgData);

            return(pLocalFeature);
        }