Esempio n. 1
0
        /// <summary>
        /// 人脸检测(PS:检测RGB图像的人脸时,必须保证图像的宽度能被4整除,否则会失败)
        /// </summary>
        /// <param name="pEngine">引擎Handle</param>
        /// <param name="imageInfo">图像数据</param>
        /// <returns>人脸检测结果</returns>
        public static ASF_MultiFaceInfo DetectFace(IntPtr pEngine, ImageInfo imageInfo)
        {
            ASF_MultiFaceInfo multiFaceInfo  = new ASF_MultiFaceInfo();
            IntPtr            pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>());
            int retCode = ASFFunctions.ASFDetectFaces(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo);

            multiFaceInfo = MemoryUtil.PtrToStructure <ASF_MultiFaceInfo>(pMultiFaceInfo);
            return(multiFaceInfo);
        }
        /// <summary>
        /// 人脸检测/人脸追踪
        /// </summary>
        /// <param name="imageFormat">图片格式</param>
        /// <param name="image">图片</param>
        /// <param name="multiFaceInfo">多人脸对象</param>
        /// <param name="detectModel">检测模式</param>
        /// <returns>返回0表示正常;返回负数请根据ErrorCodeUtil类注释查看;其他值请在官网-帮助中心查询</returns>
        public int ASFDetectFaces(Image image, out MultiFaceInfo multiFaceInfo, ASF_ImagePixelFormat imageFormat = ASF_ImagePixelFormat.ASVL_PAF_RGB24_B8G8R8, ASF_DetectModel detectModel = ASF_DetectModel.ASF_DETECT_MODEL_RGB)
        {
            int retCode = -1;

            multiFaceInfo = new MultiFaceInfo();
            //判断图像是否为空
            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);
            }

            ASF_MultiFaceInfo multiFaceInfoStruct = new ASF_MultiFaceInfo();
            IntPtr            pMultiFaceInfo      = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>());

            //调用SDK接口
            retCode = ASFFunctions.ASFDetectFaces(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo);
            if (retCode != 0)
            {
                MemoryUtil.FreeArray(imageInfo.imgData, pMultiFaceInfo);
                return(retCode);
            }
            multiFaceInfoStruct = MemoryUtil.PtrToStructure <ASF_MultiFaceInfo>(pMultiFaceInfo);
            MemoryUtil.FreeArray(imageInfo.imgData, pMultiFaceInfo);

            //转化非托管内存到托管内存
            multiFaceInfo.faceNum = multiFaceInfoStruct.faceNum;
            if (multiFaceInfo.faceNum > 0)
            {
                if (multiFaceInfoStruct.faceID != IntPtr.Zero)
                {
                    multiFaceInfo.faceID = new int[multiFaceInfo.faceNum];
                    Marshal.Copy(multiFaceInfoStruct.faceID, multiFaceInfo.faceID, 0, multiFaceInfo.faceNum);
                }
                multiFaceInfo.faceOrients = new int[multiFaceInfo.faceNum];
                Marshal.Copy(multiFaceInfoStruct.faceOrients, multiFaceInfo.faceOrients, 0, multiFaceInfo.faceNum);
                multiFaceInfo.faceRects = new MRECT[multiFaceInfo.faceNum];
                for (int i = 0; i < multiFaceInfo.faceNum; i++)
                {
                    multiFaceInfo.faceRects[i] = MemoryUtil.PtrToStructure <MRECT>(multiFaceInfoStruct.faceRects + MemoryUtil.SizeOf <MRECT>() * i);
                }
            }
            return(retCode);
        }