yanjinhui
2025-06-12 95ac5296182b763d12125c1d47f53c00632ffc41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Runtime.InteropServices;
//using System.IO;
////using OpenCvSharp;
 
//// 提取人脸特征值
//namespace FaceAI
//{
//    // 人脸特征值结构体
//    [StructLayout(LayoutKind.Sequential, Pack = 1)]
//    public struct BDFaceFeature
//    {
//        public int size;
//        // 人脸的特征值,提取出来后是128个float的浮点值
//        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
//        public float[] data;// = new float[128];
//    }
//    // 人脸特征值结构体
//    [StructLayout(LayoutKind.Sequential, Pack = 1)]
//    public struct BDDepthFeature
//    {
//        public int size;
//        // 人脸的深度特征值,提取出来后是256个float的浮点值
//        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
//        public float[] data;// = new float[256];
//    }
//    class FaceFeature
//    {
//        // 提取人脸特征值
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "face_feature", CharSet = CharSet.Ansi
//            , CallingConvention = CallingConvention.Cdecl)]
//        // 返回num为特征值的人数,type为0表示提取生活照的特征值,1表示nir的特征值
//        public static extern int face_feature(IntPtr feature, IntPtr box, IntPtr mat, int type);
       
 
 
//        public void test_face_feature()
//        {
//            long t_begin = TimeUtil.get_time_stamp();
//            BDFaceFeature[] fea1 = test_get_face_feature_by_path("../images/2.jpg");
//            long t_end = TimeUtil.get_time_stamp();
//            Console.WriteLine("get feature time cost is:" + (t_end - t_begin));
//        }
 
//        // 获取特征值,传入图片路径
//        public BDFaceFeature[] test_get_face_feature_by_path(string img_path)
//        {
//            // 特征值的长度,128个float值
//            int dim_count = 128;
//            Mat mat = Cv2.ImRead(img_path);
//            // 假设提取的人数,需要比实际的人数多,因为需要提取分配内存
//            int faceNum = 5;
//            BDFaceFeature[] feaList = new BDFaceFeature[faceNum];
//            for(int i = 0; i < faceNum; i++)
//            {
//                feaList[i].data = new float[dim_count];
//                feaList[i].size = 0;
//            }
           
//            if (mat.Empty())
//            {
//                return null;
//            }
 
//            int sizeFeature = Marshal.SizeOf(typeof(BDFaceFeature));
//            IntPtr ptFea = Marshal.AllocHGlobal(sizeFeature * faceNum);
 
//            // 构造返回的人脸框数据
//            BDFaceBBox[] info = new BDFaceBBox[faceNum];
//            int sizeBox = Marshal.SizeOf(typeof(BDFaceBBox));
//            IntPtr ptBox = Marshal.AllocHGlobal(sizeBox * faceNum);
//            // 返回num为特征值的人数,type为0表示提取生活照的特征值,1表示证件照的特征值,2表示nir的特征值
//            int type = 0;
//            int num = face_feature(ptFea, ptBox, mat.CvPtr, type);
//            if (num <= 0)
//            {
//                return null;
//            }
//            Console.WriteLine("face num is:{0}", num);
//            // 请确保faceNum大于num, faceNum为c#期望的检测人数,需要预先分配内存,否则无法获取
//            if (num > faceNum)
//            {
//                num = faceNum;
//            }
//            for (int index = 0; index < num; index++)
//            {
//                IntPtr ptrF = new IntPtr();
//                IntPtr ptrB = new IntPtr();
//                if (8 == IntPtr.Size)
//                {
//                    ptrF = (IntPtr)(ptFea.ToInt64() + sizeFeature * index);
//                    ptrB = (IntPtr)(ptBox.ToInt64() + sizeBox * index);
//                }
//                else if (4 == IntPtr.Size)
//                {
//                    ptrF = (IntPtr)(ptFea.ToInt32() + sizeFeature * index);
//                    ptrB = (IntPtr)(ptBox.ToInt32() + sizeBox * index);
//                }
//                feaList[index] = (BDFaceFeature)Marshal.PtrToStructure(ptrF, typeof(BDFaceFeature));
//                Console.WriteLine("feaList[index].size is:{0}", feaList[index].size);
                
 
//                for (int k = 0; k < feaList[index].size; k++)
//                {
//                    Console.WriteLine("feature is {0}:", feaList[index].data[k]);
//                }
 
//                info[index] = (BDFaceBBox)Marshal.PtrToStructure(ptrB, typeof(BDFaceBBox));
 
//                // 索引值
//                Console.WriteLine("detect score is:{0}", info[index].index);
//                // 置信度
//                Console.WriteLine("detect score is:{0}", info[index].score);             
//                // 人脸宽度
//                Console.WriteLine("detect mWidth is:{0}", info[index].width);
//                // 中心点X,Y坐标
//                Console.WriteLine("detect mCenter_x is:{0}", info[index].center_x);
//                Console.WriteLine("detect mCenter_y is:{0}", info[index].center_y);
//            }
//            // 绘制人脸框
//            FaceDraw.draw_rects(ref mat, faceNum, info);
 
//            //mat.ImWrite("detect.jpg");
//            mat.Release();
//            return feaList;
//        }
       
//    }
//}