腾讯人脸识别

public class IaiClientTest {

  private static final String iaiSecretId = "xxx";
  private static final String iaiSecretKey = "ccc";
  private static final String REGIONID = "ap-beijing";


  private static final String groupPre = "cccy_";
  private static final String groupSuf = "_0001";
  private static final String noticeGroupPre = "cc-cc";

  /**
   * 初始化CosClient相关配置, appid、accessKey、secretKey、region.
   * @return COSClient.
   */
  public static IaiClient getIaiClient() {
    // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    Credential cred = new Credential(iaiSecretId, iaiSecretKey);

    ClientProfile profile = new ClientProfile("TC3-HMAC-SHA256");

    // 实例化要请求产品的client对象
    IaiClient client = new IaiClient(cred, REGIONID, profile);


    log.info("connect iaiclient success.");
    return  client;
  }


  /**
   *拼接groupid.
   * @param noticeFlag notice_flag.
   * @param bkey bkey.
   * @return groupId.
   */
  private static String getGroupId(boolean noticeFlag, int bkey) {

    if (noticeFlag) {
      return String.format("%s%s%s", noticeGroupPre, bkey, groupSuf);
    } else {
      return String.format("%s%s%s", groupPre, bkey, groupSuf);
    }
  }

  /**
   * 创建groupID.
   * @return boolean.
   */
  public static Boolean createGroup() {
    var client = getIaiClient();
    var groupId = getGroupId(true, 40840);
    try {
      CreateGroupRequest request = new CreateGroupRequest();
      request.setGroupId(groupId);
      request.setGroupName(groupId);
      request.setFaceModelVersion("3.0");
      client.CreateGroup(request);
      log.debug("Create group. GroupId: %s", groupId);
      return false;
    } catch (TencentCloudSDKException e) {
      return true;
    } catch (Exception e) {
      var msg =String.format("Create group id: %s failed. Ex: %s", groupId);
      throw new AppResourceConflictException(msg);
    }
  }

  /**
   * 创建person.
   * @param groupId groupId.
   * @param personId personId.
   * @param personName personName.
   * @param b64file b64file.
   */
  public static CreatePersonResponse createPerson(
      String groupId, String personId, String personName, String b64file) {

    var client = getIaiClient();
    try {
      var req = new CreatePersonRequest();
      req.setGroupId(groupId);
      req.setPersonId(personId);
      req.setPersonName(personName);
      if (b64file != null) {
        req.setImage(b64file);
      }
      var res = client.CreatePerson(req);

      log.debug("CreatePerson result. GroupId: %s. PersonId: %s. res: %s",
          groupId, personId, res);
      return res;
      //{"FaceId": "3139479249070616485", "RequestId": "b8437bb4-d81a-412b-9f4c-8136fa3a578b"}
    } catch (TencentCloudSDKException e) {
      var msg = String.format("Create person failed. GroupId: %s. PersonId: %s. Ex: %s",
          groupId, personId, e);
      throw new AppResourceConflictException(msg);
    }
  }


  /**
   * iai请求删除人脸数据 根据groupID.
   * @param personid b64file.
   * @return DetectFaceResponse.
   */
  public static DeletePersonFromGroupResponse deletePersonFromGroup(
      String personid, boolean noticeFlag, int bkey) {
    var client = getIaiClient();
    try {
      String groupId = getGroupId(noticeFlag, bkey);
      var isCrea = createGroup();
      if (!isCrea) {
        return null;
      }
      DeletePersonFromGroupRequest req = new DeletePersonFromGroupRequest();
      // set attribute
      req.setPersonId(personid);
      req.setGroupId(groupId);
      return client.DeletePersonFromGroup(req);
    } catch (TencentCloudSDKException e) {
      throw new AppResourceConflictException(String.format("IAI connection failed! %s", e));
    }
  }

  /**
   * iai请求人脸识别.
   * @param b64file b64file.
   * @return DetectFaceResponse.
   */
  public DetectFaceResponse detectFace(String b64file) {
    var client = getIaiClient();
    try {
      DetectFaceRequest req = new DetectFaceRequest();
      // set attribute
      req.setImage(b64file);
      req.setNeedFaceAttributes(1L);
      req.setNeedQualityDetection(1L);
      req.setFaceModelVersion("3.0");
      return client.DetectFace(req);
    } catch (TencentCloudSDKException e) {
      throw new AppResourceConflictException(String.format("IAI connection failed! %s", e));
    }
  }


  public static void main(String[] args) {
    //createGroup();
    //createPerson("wipro-may_50038_0001",
        //"person_050038_047602_5df9cb5dec41364e842d2ppp",
        //"person_040840_021544_5df9cb5dec41364e842d2ppp",
       // "");
    //deletePersonFromGroup("notice_040840_021544_5e05a2e3ec4136568471e0fa",
      //  true, 40840);
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## 使用前准备​ 1. 前往注册: [腾讯云账号注册](https://cloud.tencent.com/register) (详细指引见 [注册腾讯云](https://cloud.tencent.com/document/product/378/9603)) 2. 取得存储桶名称 **BucketName**: 请前往 [创建存储桶](https://cloud.tencent.com/document/product/460/10637) 3. 取得 **APPID**、**SecretId**、**SecretKey**:请前往 [云API密钥](https://console.cloud.tencent.com/cam/capi) ,点击“新建密钥” ## 快速体验 1. 修改文件 src/main/java/com/qcloud/image/demo/Demo.java 的 main() 方法,填入上述申请到的 **APPID**、**SecretId**、**SecretKey**、**BucketName** 2. 导入到 IDE:工程用 Maven 构建,以 Intellij IDEA 为例,导入方式为:Import Project -> 选择工程目录 -> Import project from external model -> Maven 3. 运行:Demo.java 右键,Run Demo.main() ## 使用简介 ### 初始化 ```java ImageClient imageClient = new ImageClient(APPID, SecretId, SecretKey); ``` ### 设置代理 根据实际网络环境,可能要设置代理,例如: ```java Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); imageClient.setProxy(proxy); ``` ### 使用 SDK 提供功能如下: **图像识别**:鉴黄,标签 **文字识别(OCR)**:身份证,名片,通用,驾驶证行驶证,营业执照,银行卡,车牌号 **人脸识别**:人脸检测,五官定位,个体信息管理,人脸验证,人脸对比及人脸检索 **人脸核身**:照片核身(通过照片和身份证信息),获取唇语验证码(用于活体核身),活体核身(通过视频和照片),活体核身(通过视频和身份证信息) ```java // 调用车牌识别API示例 String imageUrl = "http://youtu.qq.com/app/img/experience/char_general/icon_ocr_license_3.jpg"; String result = imageClient.ocrPlate(new OcrPlateRequest("bucketName", imageUrl)); System.out.println(result); ``` 更多例子详情可参见 [Demo.java](https://github.com/tencentyun/image-java-sdk-v2.0/blob/master/src/main/java/com/qcloud/image/demo/Demo.java) 的代码。 ## 集成到你的项目中 ### 获得 SDK jar 文件 1. 直接使用 release/*-with-dependencies.jar 2. 自行编译:在工程根目录下执行命令 `mvn assembly:assembly`,编译结果见 target/*-with-dependencies.jar ### 导入 jar 文件 根据项目具体情况导入 *-with-dependencies.jar

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值