1
z8018
2025-06-06 ee5e3a086a5dc145e7f5df3de32e0e072c183949
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//using OpenCvSharp;
//using System;
//using System.Runtime.InteropServices;
///**
// *  备注(人脸数据库管理说明):
// *  人脸数据库为采用sqlite3的数据库,会自动生成一个db目录夹,下面有数据库face.db文件保存数据库
// *  可用sqliteExpert之类的可视化工具打开查看,亦可用命令行,方法请自行百度。
// *  该数据库仅仅可适应于5w人左右的人脸库,且设计表格等属于小型通用化。
// *  若不能满足客户个性化需求,客户可自行设计数据库保存数据。宗旨就是每个人脸图片提取一个特征值保存。
// *  人脸1:1,1:N比对及识别实际就是特征值的比对。1:1只要提取2张不同的图片特征值调用compare_feature比对。
// *  1:N是提取一个特征值和数据库中已保存的N个特征值一一比对(比对速度很快,不用担心效率问题),
// *  最终取分数高的值为最高相似度。
// *  相似度识别的分数可自行测试根据实验结果拟定,一般推荐相似度大于80分为同一人。
// * 
// */
//namespace FaceAI
//{
//    class FaceManager
//    {
//        // 人脸注册(传特征值,特征值可参考FaceFeature.cs提取,亦可参考FaceCompare.cs查看特征值的比对)
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "user_add", CharSet = CharSet.Ansi
//            , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr user_add(ref BDFaceFeature f1, string user_id, string group_id, 
//            string user_info="");
 
//        // 人脸注册(传opencv图片帧,特征值可参考FaceFeature.cs提取,亦可参考FaceCompare.cs查看特征值的比对)
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "user_add_by_mat", CharSet = CharSet.Ansi
//            , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr user_add_by_mat(IntPtr mat, string user_id, string group_id,
//            string user_info = "");
 
//        // 人脸更新(传图片帧)
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "user_update", CharSet = CharSet.Ansi
//            , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr user_update(IntPtr mat, string user_id, string group_id,
//            string user_info = "");
      
//        // 用户删除
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "user_delete", CharSet = CharSet.Ansi
//           , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr user_delete(string user_id, string group_id);
//        // 组添加
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "group_add", CharSet = CharSet.Ansi
//           , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr group_add(string group_id);
//        // 组删除
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "group_delete", CharSet = CharSet.Ansi
//           , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr group_delete(string group_id);
//        // 查询用户信息
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "get_user_info", CharSet = CharSet.Ansi
//           , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr get_user_info(string user_id, string group_id);
 
//        // 查询用户图片
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "get_user_image", CharSet = CharSet.Ansi
//           , CallingConvention = CallingConvention.Cdecl)]
//        public static extern int get_user_image(IntPtr out_mat, string user_id, string group_id);
 
//        // 用户组列表查询
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "get_user_list", CharSet = CharSet.Ansi
//           , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr get_user_list(string group_id, int start = 0, int length = 100);
//        // 组列表查询
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "get_group_list", CharSet = CharSet.Ansi
//           , CallingConvention = CallingConvention.Cdecl)]
//        public static extern IntPtr get_group_list(int start = 0, int length = 100);
 
//        // 数据库人脸数量查询
//        [DllImport("BaiduFaceApi.dll", EntryPoint = "db_face_count", CharSet = CharSet.Ansi
//           , CallingConvention = CallingConvention.Cdecl)]
//        public static extern int db_face_count(string group_id);
 
 
//        // 测试人脸注册
//        public void test_user_add()
//        {
//            // 人脸注册
//            string user_id = "test_user";
//            string group_id = "test_group";
//            string file_name = "../images/1.jpg";
           
//            string user_info = "user_info";
//            // 提取人脸特征值数组(多人会提取多个人的特征值)
///*
//            FaceFeature feature = new FaceFeature();
//            BDFaceFeature[] feaList1 = feature.test_get_face_feature_by_path(file_name
//            if (feaList1 == null)
//            {
//                Console.WriteLine("get feature fail");
//                return;
//            }
//            // 假设测试的图片是1个人,
//            BDFaceFeature f1 = feaList1[0];
//            // 人脸注册 (传特征值人脸注册,该方法注册不保存人脸图片入库)
//            IntPtr ptr = user_add(ref f1, user_id, group_id, user_info);
//            string buf = Marshal.PtrToStringAnsi(ptr);
//            Console.WriteLine("user_add res is:" + buf);
//*/
//            // 通过图片帧人脸注册(只有该方法进行的人脸注册,人脸库才会保存人脸图片)
//            Mat mat = Cv2.ImRead(file_name);
//            IntPtr mptr = user_add_by_mat(mat.CvPtr, user_id, group_id, user_info);
//            string mbuf = Marshal.PtrToStringAnsi(mptr);
//            Console.WriteLine("user_add_by_mat res is:" + mbuf);
//        }
      
//        // 测试人脸更新
//        public void test_user_update()
//        {
//            string user_id = "test_user";
//            string group_id = "test_group";
//            string file_name = "../images/1.jpg";
//            Mat mat = Cv2.ImRead(file_name);
//            string user_info = "user_info";
//            // 人脸更新   
//            IntPtr ptr = user_update(mat.CvPtr, user_id, group_id, user_info);
//            string buf = Marshal.PtrToStringAnsi(ptr);
//            Console.WriteLine("user_update res is:" + buf);
//        }     
 
//        // 测试用户删除 (用户删除后,人脸数据也被删除)
//        public void test_user_delete()
//        {
//            string user_id = "test_user";
//            string group_id = "test_group";
//            IntPtr ptr = user_delete(user_id, group_id);
//            string buf = Marshal.PtrToStringAnsi(ptr);
//            Console.WriteLine("user_delete res is:" + buf);
//        }
 
//        // 组添加
//        public void test_group_add()
//        {
//            string group_id = "test_group2";
//            IntPtr ptr = group_add(group_id);
//            string buf = Marshal.PtrToStringAnsi(ptr);
//            Console.WriteLine("group_add res is:" + buf);
//        }
 
//        // 组删除
//        public void test_group_delete()
//        {
//            string group_id = "test_group2";
//            IntPtr ptr = group_delete(group_id);
//            string buf = Marshal.PtrToStringAnsi(ptr);
//            Console.WriteLine("group_delete res is:" + buf);
//        }
 
//        // 查询用户信息
//        public void test_get_user_info()
//        {
//            string user_id = "test_user";
//            string group_id = "test_group";
//            IntPtr ptr = get_user_info(user_id , group_id);
//            string buf = Marshal.PtrToStringAnsi(ptr);
//            Console.WriteLine("get_user_info res is:" + buf);
//        }
 
//        // 查询用户图片
//        public void test_get_user_image()
//        {
//            string user_id = "test_user";
//            string group_id = "test_group";
//            Mat out_mat = new Mat();
//            int res = get_user_image(out_mat.CvPtr, user_id, group_id);
//            if (res == 0)
//            {
//                Console.WriteLine("get_user_image success");
//                // 图片保存到本地
//                out_mat.ImWrite("user.jpg");
//            }
//            else
//            {
//                Console.WriteLine("get_user_image error{0}:", res);
//            }
          
//        }
 
//        // 用户组列表查询
//        public void test_get_user_list()
//        {
//            string group_id = "test_group";
//            IntPtr ptr = get_user_list(group_id);
//            string buf = Marshal.PtrToStringAnsi(ptr);
//            Console.WriteLine("get_user_list res is:" + buf);
//        }
 
//        // 组列表查询
//        public void test_get_group_list()
//        {
//            IntPtr ptr = get_group_list();
//            string buf = Marshal.PtrToStringAnsi(ptr);
//            Console.WriteLine("get_group_list res is:" + buf);
//        }
 
//        // 人脸库数量查询
//        public void test_db_face_count()
//        {
//            string group_id = "test_group";
//            // 参数传组id表示查该组都人脸数量
//            int count = db_face_count(group_id);
//            Console.WriteLine("count  is:" + count);
//            string group_id2 = null;
//            // 参数传null表示查整个库
//            int count2 = db_face_count(group_id2);
//            Console.WriteLine("all count is:" + count2);
//        }
//    }
//}