linux版本中间件

face.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**
  2. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. *
  13. * @author baidu aip
  14. */
  15. #ifndef __AIP_FACE_H__
  16. #define __AIP_FACE_H__
  17. #include "base/base.h"
  18. #include <vector>
  19. namespace aip {
  20. class Face: public AipBase
  21. {
  22. public:
  23. std::string _detect =
  24. "https://aip.baidubce.com/rest/2.0/face/v2/detect";
  25. std::string _match =
  26. "https://aip.baidubce.com/rest/2.0/face/v2/match";
  27. std::string _identify =
  28. "https://aip.baidubce.com/rest/2.0/face/v2/identify";
  29. std::string _verify =
  30. "https://aip.baidubce.com/rest/2.0/face/v2/verify";
  31. std::string _user_add =
  32. "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/add";
  33. std::string _user_update =
  34. "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/update";
  35. std::string _user_delete =
  36. "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/delete";
  37. std::string _user_get =
  38. "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/get";
  39. std::string _group_getlist =
  40. "https://aip.baidubce.com/rest/2.0/face/v2/faceset/group/getlist";
  41. std::string _group_getusers =
  42. "https://aip.baidubce.com/rest/2.0/face/v2/faceset/group/getusers";
  43. std::string _group_adduser =
  44. "https://aip.baidubce.com/rest/2.0/face/v2/faceset/group/adduser";
  45. std::string _group_deleteuser =
  46. "https://aip.baidubce.com/rest/2.0/face/v2/faceset/group/deleteuser";
  47. Face(const std::string & app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
  48. {
  49. }
  50. std::string vector_join_base64(const std::vector<std::string> & v_images) {
  51. std::string images;
  52. size_t count = v_images.size();
  53. for (size_t i = 0; i < count;i++)
  54. {
  55. std::string image = v_images[i];
  56. images += base64_encode(image.c_str(), (int) image.size());
  57. if (i != count) {
  58. images += ",";
  59. }
  60. }
  61. return images;
  62. }
  63. /**
  64. * detect
  65. * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
  66. * options 可选参数:
  67. * max_face_num 最多处理人脸数目,默认值1
  68. * face_fields 包括age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities信息,逗号分隔,默认只返回人脸框、概率和旋转角度
  69. */
  70. Json::Value detect(
  71. std::string const & image,
  72. const std::map<std::string, std::string> & options)
  73. {
  74. std::map<std::string, std::string> data;
  75. data["image"] = base64_encode(image.c_str(), (int) image.size());
  76. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  77. Json::Value result =
  78. this->request(_detect, null, data, null);
  79. return result;
  80. }
  81. /**
  82. * match
  83. * @param images vector多图图像文件二进制内容,vector中每一项可以使用aip::get_file_content函数获取
  84. * options 可选参数:
  85. * ext_fields 返回质量信息,取值固定:目前支持qualities(质量检测)。(对所有图片都会做改处理)
  86. * image_liveness 返回的活体信息,“faceliveness,faceliveness” 表示对比对的两张图片都做活体检测;“,faceliveness” 表示对第一张图片不做活体检测、第二张图做活体检测;“faceliveness,” 表示对第一张图片做活体检测、第二张图不做活体检测;<br>**注:需要用于判断活体的图片,图片中的人脸像素面积需要不小于100px\*100px,人脸长宽与图片长宽比例,不小于1/3**
  87. * types 请求对比的两张图片的类型,示例:“7,13”<br>**12**表示带水印证件照:一般为带水印的小图,如公安网小图<br>**7**表示生活照:通常为手机、相机拍摄的人像图片、或从网络获取的人像图片等<br>**13**表示证件照片:如拍摄的身份证、工卡、护照、学生证等证件图片,**注**:需要确保人脸部分不可太小,通常为100px\*100px
  88. */
  89. Json::Value match(
  90. const std::vector<std::string> & images,
  91. const std::map<std::string, std::string> & options)
  92. {
  93. std::map<std::string, std::string> data;
  94. data["images"] = vector_join_base64(images);
  95. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  96. Json::Value result =
  97. this->request(_match, null, data, null);
  98. return result;
  99. }
  100. /**
  101. * identify
  102. * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B,多个用户组id,用逗号分隔
  103. * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
  104. * options 可选参数:
  105. * ext_fields 特殊返回信息,多个用逗号分隔,取值固定: 目前支持faceliveness(活体检测)。**注:需要用于判断活体的图片,图片中的人脸像素面积需要不小于100px\*100px,人脸长宽与图片长宽比例,不小于1/3**
  106. * user_top_num 返回用户top数,默认为1,最多返回5个
  107. */
  108. Json::Value identify(
  109. std::string const & group_id,
  110. std::string const & image,
  111. const std::map<std::string, std::string> & options)
  112. {
  113. std::map<std::string, std::string> data;
  114. data["group_id"] = group_id;
  115. data["image"] = base64_encode(image.c_str(), (int) image.size());
  116. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  117. Json::Value result =
  118. this->request(_identify, null, data, null);
  119. return result;
  120. }
  121. /**
  122. * verify
  123. * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
  124. * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
  125. * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B,多个用户组id,用逗号分隔
  126. * options 可选参数:
  127. * top_num 返回用户top数,默认为1
  128. * ext_fields 特殊返回信息,多个用逗号分隔,取值固定: 目前支持faceliveness(活体检测)。**注:需要用于判断活体的图片,图片中的人脸像素面积需要不小于100px\*100px,人脸长宽与图片长宽比例,不小于1/3**
  129. */
  130. Json::Value verify(
  131. std::string const & uid,
  132. std::string const & image,
  133. std::string const & group_id,
  134. const std::map<std::string, std::string> & options)
  135. {
  136. std::map<std::string, std::string> data;
  137. data["uid"] = uid;
  138. data["image"] = base64_encode(image.c_str(), (int) image.size());
  139. data["group_id"] = group_id;
  140. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  141. Json::Value result =
  142. this->request(_verify, null, data, null);
  143. return result;
  144. }
  145. /**
  146. * user_add
  147. * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
  148. * @param user_info 用户资料,长度限制256B
  149. * @param group_id 用户组id,标识一组用户(由数字、字母、下划线组成),长度限制128B。如果需要将一个uid注册到多个group下,group\_id需要用多个逗号分隔,每个group_id长度限制为48个英文字符。**注:group无需单独创建,注册用户时则会自动创建group。**<br>**产品建议**:根据您的业务需求,可以将需要注册的用户,按照业务划分,分配到不同的group下,例如按照会员手机尾号作为groupid,用于刷脸支付、会员计费消费等,这样可以尽可能控制每个group下的用户数与人脸数,提升检索的准确率
  150. * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
  151. * options 可选参数:
  152. * action_type 参数包含append、replace。**如果为“replace”,则每次注册时进行替换replace(新增或更新)操作,默认为append操作**。例如:uid在库中已经存在时,对此uid重复注册时,新注册的图片默认会**追加**到该uid下,如果手动选择`action_type:replace`,则会用新图替换库中该uid下所有图片。
  153. */
  154. Json::Value user_add(
  155. std::string const & uid,
  156. std::string const & user_info,
  157. std::string const & group_id,
  158. std::string const & image,
  159. const std::map<std::string, std::string> & options)
  160. {
  161. std::map<std::string, std::string> data;
  162. data["uid"] = uid;
  163. data["user_info"] = user_info;
  164. data["group_id"] = group_id;
  165. data["image"] = base64_encode(image.c_str(), (int) image.size());
  166. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  167. Json::Value result =
  168. this->request(_user_add, null, data, null);
  169. return result;
  170. }
  171. /**
  172. * user_update
  173. * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
  174. * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
  175. * @param user_info 用户资料,长度限制256B
  176. * @param group_id 更新指定groupid下uid对应的信息
  177. * options 可选参数:
  178. * action_type 目前仅支持replace,uid不存在时,不报错,会自动变为注册操作;未选择该参数时,如果uid不存在会提示错误
  179. */
  180. Json::Value user_update(
  181. std::string const & uid,
  182. std::string const & image,
  183. std::string const & user_info,
  184. std::string const & group_id,
  185. const std::map<std::string, std::string> & options)
  186. {
  187. std::map<std::string, std::string> data;
  188. data["uid"] = uid;
  189. data["image"] = base64_encode(image.c_str(), (int) image.size());
  190. data["user_info"] = user_info;
  191. data["group_id"] = group_id;
  192. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  193. Json::Value result =
  194. this->request(_user_update, null, data, null);
  195. return result;
  196. }
  197. /**
  198. * user_delete
  199. * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
  200. * @param group_id 删除指定groupid下uid对应的信息
  201. * options 可选参数:
  202. */
  203. Json::Value user_delete(
  204. std::string const & uid,
  205. std::string const & group_id,
  206. const std::map<std::string, std::string> & options)
  207. {
  208. std::map<std::string, std::string> data;
  209. data["uid"] = uid;
  210. data["group_id"] = group_id;
  211. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  212. Json::Value result =
  213. this->request(_user_delete, null, data, null);
  214. return result;
  215. }
  216. /**
  217. * user_get
  218. * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
  219. * options 可选参数:
  220. * group_id 选择指定group_id则只查找group列表下的uid内容,如果不指定则查找所有group下对应uid的信息
  221. */
  222. Json::Value user_get(
  223. std::string const & uid,
  224. const std::map<std::string, std::string> & options)
  225. {
  226. std::map<std::string, std::string> data;
  227. data["uid"] = uid;
  228. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  229. Json::Value result =
  230. this->request(_user_get, null, data, null);
  231. return result;
  232. }
  233. /**
  234. * group_getlist
  235. * options 可选参数:
  236. * start 默认值0,起始序号
  237. * end 返回数量,默认值100,最大值1000
  238. */
  239. Json::Value group_getlist(
  240. const std::map<std::string, std::string> & options)
  241. {
  242. std::map<std::string, std::string> data;
  243. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  244. Json::Value result =
  245. this->request(_group_getlist, null, data, null);
  246. return result;
  247. }
  248. /**
  249. * group_getusers
  250. * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B
  251. * options 可选参数:
  252. * start 默认值0,起始序号
  253. * end 返回数量,默认值100,最大值1000
  254. */
  255. Json::Value group_getusers(
  256. std::string const & group_id,
  257. const std::map<std::string, std::string> & options)
  258. {
  259. std::map<std::string, std::string> data;
  260. data["group_id"] = group_id;
  261. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  262. Json::Value result =
  263. this->request(_group_getusers, null, data, null);
  264. return result;
  265. }
  266. /**
  267. * group_adduser
  268. * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B,多个用户组id,用逗号分隔
  269. * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
  270. * @param src_group_id 从指定group里复制信息
  271. * options 可选参数:
  272. */
  273. Json::Value group_adduser(
  274. std::string const & group_id,
  275. std::string const & uid,
  276. std::string const & src_group_id,
  277. const std::map<std::string, std::string> & options)
  278. {
  279. std::map<std::string, std::string> data;
  280. data["group_id"] = group_id;
  281. data["uid"] = uid;
  282. data["src_group_id"] = src_group_id;
  283. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  284. Json::Value result =
  285. this->request(_group_adduser, null, data, null);
  286. return result;
  287. }
  288. /**
  289. * group_deleteuser
  290. * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B,多个用户组id,用逗号分隔
  291. * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
  292. * options 可选参数:
  293. */
  294. Json::Value group_deleteuser(
  295. std::string const & group_id,
  296. std::string const & uid,
  297. const std::map<std::string, std::string> & options)
  298. {
  299. std::map<std::string, std::string> data;
  300. data["group_id"] = group_id;
  301. data["uid"] = uid;
  302. std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
  303. Json::Value result =
  304. this->request(_group_deleteuser, null, data, null);
  305. return result;
  306. }
  307. };
  308. }
  309. #endif