| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- /**
- * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
- * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- *
- * @author baidu aip
- */
- #ifndef __AIP_FACE_H__
- #define __AIP_FACE_H__
- #include "base/base.h"
- #include <vector>
- namespace aip {
- class Face: public AipBase
- {
- public:
-
- std::string _detect =
- "https://aip.baidubce.com/rest/2.0/face/v2/detect";
-
- std::string _match =
- "https://aip.baidubce.com/rest/2.0/face/v2/match";
-
- std::string _identify =
- "https://aip.baidubce.com/rest/2.0/face/v2/identify";
-
- std::string _verify =
- "https://aip.baidubce.com/rest/2.0/face/v2/verify";
-
- std::string _user_add =
- "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/add";
-
- std::string _user_update =
- "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/update";
-
- std::string _user_delete =
- "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/delete";
-
- std::string _user_get =
- "https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/get";
-
- std::string _group_getlist =
- "https://aip.baidubce.com/rest/2.0/face/v2/faceset/group/getlist";
-
- std::string _group_getusers =
- "https://aip.baidubce.com/rest/2.0/face/v2/faceset/group/getusers";
-
- std::string _group_adduser =
- "https://aip.baidubce.com/rest/2.0/face/v2/faceset/group/adduser";
-
- std::string _group_deleteuser =
- "https://aip.baidubce.com/rest/2.0/face/v2/faceset/group/deleteuser";
-
- Face(const std::string & app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
- {
- }
- std::string vector_join_base64(const std::vector<std::string> & v_images) {
- std::string images;
- size_t count = v_images.size();
- for (size_t i = 0; i < count;i++)
- {
- std::string image = v_images[i];
- images += base64_encode(image.c_str(), (int) image.size());
- if (i != count) {
- images += ",";
- }
- }
- return images;
- }
-
- /**
- * detect
- * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
- * options 可选参数:
- * max_face_num 最多处理人脸数目,默认值1
- * face_fields 包括age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities信息,逗号分隔,默认只返回人脸框、概率和旋转角度
- */
- Json::Value detect(
- std::string const & image,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["image"] = base64_encode(image.c_str(), (int) image.size());
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_detect, null, data, null);
- return result;
- }
-
- /**
- * match
- * @param images vector多图图像文件二进制内容,vector中每一项可以使用aip::get_file_content函数获取
- * options 可选参数:
- * ext_fields 返回质量信息,取值固定:目前支持qualities(质量检测)。(对所有图片都会做改处理)
- * image_liveness 返回的活体信息,“faceliveness,faceliveness” 表示对比对的两张图片都做活体检测;“,faceliveness” 表示对第一张图片不做活体检测、第二张图做活体检测;“faceliveness,” 表示对第一张图片做活体检测、第二张图不做活体检测;<br>**注:需要用于判断活体的图片,图片中的人脸像素面积需要不小于100px\*100px,人脸长宽与图片长宽比例,不小于1/3**
- * types 请求对比的两张图片的类型,示例:“7,13”<br>**12**表示带水印证件照:一般为带水印的小图,如公安网小图<br>**7**表示生活照:通常为手机、相机拍摄的人像图片、或从网络获取的人像图片等<br>**13**表示证件照片:如拍摄的身份证、工卡、护照、学生证等证件图片,**注**:需要确保人脸部分不可太小,通常为100px\*100px
- */
- Json::Value match(
- const std::vector<std::string> & images,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["images"] = vector_join_base64(images);
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_match, null, data, null);
- return result;
- }
-
- /**
- * identify
- * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B,多个用户组id,用逗号分隔
- * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
- * options 可选参数:
- * ext_fields 特殊返回信息,多个用逗号分隔,取值固定: 目前支持faceliveness(活体检测)。**注:需要用于判断活体的图片,图片中的人脸像素面积需要不小于100px\*100px,人脸长宽与图片长宽比例,不小于1/3**
- * user_top_num 返回用户top数,默认为1,最多返回5个
- */
- Json::Value identify(
- std::string const & group_id,
- std::string const & image,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["group_id"] = group_id;
- data["image"] = base64_encode(image.c_str(), (int) image.size());
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_identify, null, data, null);
- return result;
- }
-
- /**
- * verify
- * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
- * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
- * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B,多个用户组id,用逗号分隔
- * options 可选参数:
- * top_num 返回用户top数,默认为1
- * ext_fields 特殊返回信息,多个用逗号分隔,取值固定: 目前支持faceliveness(活体检测)。**注:需要用于判断活体的图片,图片中的人脸像素面积需要不小于100px\*100px,人脸长宽与图片长宽比例,不小于1/3**
- */
- Json::Value verify(
- std::string const & uid,
- std::string const & image,
- std::string const & group_id,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["uid"] = uid;
- data["image"] = base64_encode(image.c_str(), (int) image.size());
- data["group_id"] = group_id;
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_verify, null, data, null);
- return result;
- }
-
- /**
- * user_add
- * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
- * @param user_info 用户资料,长度限制256B
- * @param group_id 用户组id,标识一组用户(由数字、字母、下划线组成),长度限制128B。如果需要将一个uid注册到多个group下,group\_id需要用多个逗号分隔,每个group_id长度限制为48个英文字符。**注:group无需单独创建,注册用户时则会自动创建group。**<br>**产品建议**:根据您的业务需求,可以将需要注册的用户,按照业务划分,分配到不同的group下,例如按照会员手机尾号作为groupid,用于刷脸支付、会员计费消费等,这样可以尽可能控制每个group下的用户数与人脸数,提升检索的准确率
- * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
- * options 可选参数:
- * action_type 参数包含append、replace。**如果为“replace”,则每次注册时进行替换replace(新增或更新)操作,默认为append操作**。例如:uid在库中已经存在时,对此uid重复注册时,新注册的图片默认会**追加**到该uid下,如果手动选择`action_type:replace`,则会用新图替换库中该uid下所有图片。
- */
- Json::Value user_add(
- std::string const & uid,
- std::string const & user_info,
- std::string const & group_id,
- std::string const & image,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["uid"] = uid;
- data["user_info"] = user_info;
- data["group_id"] = group_id;
- data["image"] = base64_encode(image.c_str(), (int) image.size());
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_user_add, null, data, null);
- return result;
- }
-
- /**
- * user_update
- * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
- * @param image 图像文件二进制内容,可以使用aip::get_file_content函数获取
- * @param user_info 用户资料,长度限制256B
- * @param group_id 更新指定groupid下uid对应的信息
- * options 可选参数:
- * action_type 目前仅支持replace,uid不存在时,不报错,会自动变为注册操作;未选择该参数时,如果uid不存在会提示错误
- */
- Json::Value user_update(
- std::string const & uid,
- std::string const & image,
- std::string const & user_info,
- std::string const & group_id,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["uid"] = uid;
- data["image"] = base64_encode(image.c_str(), (int) image.size());
- data["user_info"] = user_info;
- data["group_id"] = group_id;
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_user_update, null, data, null);
- return result;
- }
-
- /**
- * user_delete
- * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
- * @param group_id 删除指定groupid下uid对应的信息
- * options 可选参数:
- */
- Json::Value user_delete(
- std::string const & uid,
- std::string const & group_id,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["uid"] = uid;
- data["group_id"] = group_id;
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_user_delete, null, data, null);
- return result;
- }
-
- /**
- * user_get
- * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
- * options 可选参数:
- * group_id 选择指定group_id则只查找group列表下的uid内容,如果不指定则查找所有group下对应uid的信息
- */
- Json::Value user_get(
- std::string const & uid,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["uid"] = uid;
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_user_get, null, data, null);
- return result;
- }
-
- /**
- * group_getlist
- * options 可选参数:
- * start 默认值0,起始序号
- * end 返回数量,默认值100,最大值1000
- */
- Json::Value group_getlist(
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_group_getlist, null, data, null);
- return result;
- }
-
- /**
- * group_getusers
- * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B
- * options 可选参数:
- * start 默认值0,起始序号
- * end 返回数量,默认值100,最大值1000
- */
- Json::Value group_getusers(
- std::string const & group_id,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["group_id"] = group_id;
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_group_getusers, null, data, null);
- return result;
- }
-
- /**
- * group_adduser
- * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B,多个用户组id,用逗号分隔
- * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
- * @param src_group_id 从指定group里复制信息
- * options 可选参数:
- */
- Json::Value group_adduser(
- std::string const & group_id,
- std::string const & uid,
- std::string const & src_group_id,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["group_id"] = group_id;
- data["uid"] = uid;
- data["src_group_id"] = src_group_id;
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_group_adduser, null, data, null);
- return result;
- }
-
- /**
- * group_deleteuser
- * @param group_id 用户组id(由数字、字母、下划线组成),长度限制128B,多个用户组id,用逗号分隔
- * @param uid 用户id(由数字、字母、下划线组成),长度限制128B
- * options 可选参数:
- */
- Json::Value group_deleteuser(
- std::string const & group_id,
- std::string const & uid,
- const std::map<std::string, std::string> & options)
- {
- std::map<std::string, std::string> data;
-
- data["group_id"] = group_id;
- data["uid"] = uid;
- std::copy(options.begin(), options.end(), std::inserter(data, data.end()));
- Json::Value result =
- this->request(_group_deleteuser, null, data, null);
- return result;
- }
-
- };
- }
- #endif
|