Ejemplo n.º 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);// 返回人脸特征
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 提取人脸特征
        /// </summary>
        /// <param name="pEngine">引擎Handle</param>
        /// <param name="imageInfo">图像数据</param>
        /// <param name="multiFaceInfo">人脸检测结果</param>
        /// <returns>保存人脸特征结构体指针</returns>
        public static IntPtr ExtractFaceFeature(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out ASF_SingleFaceInfo singleFaceInfo)
        {
            singleFaceInfo = new ASF_SingleFaceInfo();
            if (multiFaceInfo.faceRects == null)
            {
                ASF_FaceFeature emptyFeature  = new ASF_FaceFeature();
                IntPtr          pEmptyFeature = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_FaceFeature>());
                UtilToolMemory.StructureToPtr(emptyFeature, pEmptyFeature);
                return(pEmptyFeature);
            }
            singleFaceInfo.faceRect   = UtilToolMemory.PtrToStructure <MRECT>(multiFaceInfo.faceRects);
            singleFaceInfo.faceOrient = UtilToolMemory.PtrToStructure <int>(multiFaceInfo.faceOrients);
            IntPtr pSingleFaceInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_SingleFaceInfo>());

            UtilToolMemory.StructureToPtr(singleFaceInfo, pSingleFaceInfo);

            IntPtr pFaceFeature = UtilToolMemory.Malloc(UtilToolMemory.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)
            {
                //释放指针
                UtilToolMemory.Free(pSingleFaceInfo);
                UtilToolMemory.Free(pFaceFeature);
                ASF_FaceFeature emptyFeature  = new ASF_FaceFeature();
                IntPtr          pEmptyFeature = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_FaceFeature>());
                UtilToolMemory.StructureToPtr(emptyFeature, pEmptyFeature);
                return(pEmptyFeature);
            }

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

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

            ASF_FaceFeature localFeature = new ASF_FaceFeature();

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

            UtilToolMemory.StructureToPtr(localFeature, pLocalFeature);

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

            return(pLocalFeature);
        }
Ejemplo n.º 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);
        }
        /// <summary>
        /// 单人脸特征提取
        /// </summary>
        /// <param name="imageFormat">图片格式</param>
        /// <param name="image">图片</param>
        /// <param name="multiFaceInfo">人脸框对象</param>
        /// <param name="faceIndex">人脸索引</param>
        /// <param name="faceFeature">[out]特征结果</param>
        /// <returns>返回0表示正常;返回负数请根据ErrorCodeUtil类注释查看;其他值请在官网-帮助中心查询</returns>
        public int ASFFaceFeatureExtract(Image image, MultiFaceInfo multiFaceInfo, out FaceFeature faceFeature, int faceIndex = 0, ASF_ImagePixelFormat imageFormat = ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8)
        {
            int retCode = -1;

            faceFeature = new FaceFeature();
            if (multiFaceInfo == null)
            {
                return(ErrorCodeUtil.MULPTIFACEINFO_IS_NULL);
            }
            if (faceIndex >= multiFaceInfo.faceNum)
            {
                return(ErrorCodeUtil.FACEINDEX_INVALID);
            }
            if (image == null)
            {
                return(ErrorCodeUtil.IMAGE_IS_NULL);
            }
            ImageInfo imageInfo = new ImageInfo();

            imageInfo = ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8.Equals(imageFormat) ? ImageUtil.ReadBMP(image) : ImageUtil.ReadBMP_IR(image);
            if (imageInfo == null)
            {
                return(ErrorCodeUtil.IMAGE_DATA_READ_FAIL);
            }
            //获取某个单人脸信息
            SingleFaceInfo singleFaceInfo  = new SingleFaceInfo();
            IntPtr         pSIngleFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <SingleFaceInfo>());

            singleFaceInfo.faceRect   = multiFaceInfo.faceRects[faceIndex];
            singleFaceInfo.faceOrient = multiFaceInfo.faceOrients[faceIndex];
            MemoryUtil.StructureToPtr(singleFaceInfo, pSIngleFaceInfo);
            IntPtr pAsfFaceFeature = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>());

            //调用SDK接口
            retCode = ASFFunctions.ASFFaceFeatureExtract(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pSIngleFaceInfo, pAsfFaceFeature);
            if (retCode != 0)
            {
                MemoryUtil.FreeArray(pSIngleFaceInfo, pAsfFaceFeature, imageInfo.imgData);
                return(retCode);
            }
            //获取特征结构体,并转化
            ASF_FaceFeature asfFeature = MemoryUtil.PtrToStructure <ASF_FaceFeature>(pAsfFaceFeature);

            byte[] feature = new byte[asfFeature.featureSize];
            MemoryUtil.Copy(asfFeature.feature, feature, 0, asfFeature.featureSize);
            faceFeature.featureSize = asfFeature.featureSize;
            faceFeature.feature     = feature;
            MemoryUtil.FreeArray(pSIngleFaceInfo, pAsfFaceFeature, imageInfo.imgData);
            return(retCode);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 提取单人脸特征
        /// </summary>
        /// <param name="pEngine">人脸识别引擎</param>
        /// <param name="image">图片</param>
        /// <param name="singleFaceInfo">单人脸信息</param>
        /// <returns>单人脸特征</returns>
        public static IntPtr ExtractFaceFeature(IntPtr pEngine, Image image, ASF_SingleFaceInfo singleFaceInfo)
        {
            ImageInfo imageInfo = UtilTool.ReadImage(image);

            if (imageInfo == null)
            {
                ASF_FaceFeature emptyFeature  = new ASF_FaceFeature();
                IntPtr          pEmptyFeature = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_FaceFeature>());
                UtilToolMemory.StructureToPtr(emptyFeature, pEmptyFeature);
                return(pEmptyFeature);
            }
            IntPtr pSingleFaceInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_SingleFaceInfo>());

            UtilToolMemory.StructureToPtr(singleFaceInfo, pSingleFaceInfo);

            IntPtr pFaceFeature = UtilToolMemory.Malloc(UtilToolMemory.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)
            {
                //释放指针
                UtilToolMemory.Free(pSingleFaceInfo);
                UtilToolMemory.Free(pFaceFeature);
                UtilToolMemory.Free(imageInfo.imgData);

                ASF_FaceFeature emptyFeature  = new ASF_FaceFeature();
                IntPtr          pEmptyFeature = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_FaceFeature>());
                UtilToolMemory.StructureToPtr(emptyFeature, pEmptyFeature);
                return(pEmptyFeature);
            }

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

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

            ASF_FaceFeature localFeature = new ASF_FaceFeature();

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

            UtilToolMemory.StructureToPtr(localFeature, pLocalFeature);

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

            return(pLocalFeature);
        }