更新时间:2022-03-31 GMT+08:00
分享

Java SDK

本章节介绍人脸识别服务Java SDK,您可以参考本章节进行快速集成开发。

准备工作

  • 已注册华为云帐号,并完成实名认证,帐号不能处于欠费或冻结状态。
  • 已开通人脸识别服务。如未开通,请登录人脸识别管理控制台开通所需服务。
  • 已具备开发环境,支持Java JDK 1.8 及其以上版本。
  • 登录“我的凭证 > 访问秘钥”页面,获取Access Key(AK)和Secret Access Key(SK)。
  • 登录“我的凭证”页面,获取“IAM用户名”“、帐号名”以及待使用区域的“项目ID”。调用服务时会用到这些信息,请提前保存。

    本样例以“华北-北京四”区域为例,获取对应的项目ID(project_id)。

    图1 我的凭证

安装SDK

推荐您通过Maven方式获取和安装SDK,首先需要在您的操作系统中下载安装Maven,安装完成后您只需要在Java项目的pom.xml文件中加入相应的依赖项即可。

使用SDK前,需要安装“huaweicloud-sdk-core”和“huaweicloud-sdk-frs”。SDK的版本信息请参见SDK中心

<dependency>
    <groupId>com.huaweicloud.sdk</groupId>
    <artifactId>huaweicloud-sdk-core</artifactId>
    <version>3.0.56</version>
</dependency>
<dependency>
    <groupId>com.huaweicloud.sdk</groupId>
    <artifactId>huaweicloud-sdk-frs</artifactId>
    <version>3.0.56</version>
</dependency>

开始使用

详细的SDK介绍,使用异步客户端,配置日志请参见SDK中心Java SDK使用指导Java SDK使用视频

  1. 导入依赖模块
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    
    //导入v2版本sdk
    import com.huaweicloud.sdk.frs.v2.region.FrsRegion;
    import com.huaweicloud.sdk.frs.v2.*;
    import com.huaweicloud.sdk.frs.v2.model.*;
  2. 配置认证信息
    配置AK、SK信息。华为云通过AK识别用户的身份,通过SK对请求数据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。AK、SK获取方法请参见准备工作
    public static ICredential getCredential(String ak, String sk) {
        return new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
    }
  3. 初始化客户端
    指定region方式
    public static FrsClient getClient(Region region, ICredential auth) {
        // 初始化人脸识别服务的客户端
        return FrsClient.newBuilder()
                .withCredential(auth)
                .withRegion(region) // 选择服务所在区域 FrsRegion.valueOf("cn-north-4")
                .build();
    }

    服务部署区域请参见终端节点

  4. 发送请求并查看响应
    // 以调用人脸检测 DetectFaceByBase64 接口为例
    DetectFaceByBase64Request request = new DetectFaceByBase64Request();
    FaceDetectBase64Req body = new FaceDetectBase64Req();
    body.withImageBase64("/9j/4AAQSkZJRgABAQAAAQABAAD...");
    request.withBody(body);
    DetectFaceByBase64Response response = client.detectFaceByBase64(request);
    System.out.println(response.toString());

    使用人脸比对SDK时,image1、image2参数需为相同类型,即同为url、base64或file。

  5. 异常处理
    表1 异常处理

    一级分类

    一级分类说明

    二级分类

    二级分类说明

    ConnectionException

    连接类异常

    HostUnreachableException

    网络不可达、被拒绝。

    SslHandShakeException

    SSL认证异常。

    RequestTimeoutException

    响应超时异常

    CallTimeoutException

    单次请求,服务器处理超时未返回。

    RetryOutageException

    在重试策略消耗完成后,仍无有效的响应。

    ServiceResponseException

    服务器响应异常

    ServerResponseException

    服务端内部错误,Http响应码:[500,]。

    ClientRequestException

    请求参数不合法,Http响应码:[400, 500)

    DetectFaceByBase64Request request = new DetectFaceByBase64Request();
    try {
        DetectFaceByBase64Response response = client.detectFaceByBase64(request);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }

SDK demo 代码解析

  • 人脸检测
    DetectFaceByBase64Request detectRequest = new DetectFaceByBase64Request();
    FaceDetectBase64Req faceDetectBase64Req = new FaceDetectBase64Req();
    faceDetectBase64Req.withImageBase64("/9j/4AAQSkZJRgABAQAAAQABAAD...");
    faceDetectBase64Req.withAttributes("2");
    detectRequest.setBody(faceDetectBase64Req);
    try {
        DetectFaceByBase64Response detectResponse = client.detectFaceByBase64(detectRequest);
        System.out.println(detectResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 人脸比对
    CompareFaceByBase64Request compareRequest = new CompareFaceByBase64Request();
    FaceCompareBase64Req faceCompareBase64Req = new FaceCompareBase64Req();
    faceCompareBase64Req.withImage1Base64("/9j/4AAQSkZJRgABAQAAAQABAAD...");
    faceCompareBase64Req.withImage2Base64("/9j/4AAQSkZJRgABAQAAAQABAAD...");
    compareRequest.withBody(faceCompareBase64Req);
    try {
        CompareFaceByBase64Response compareResponse = client.compareFaceByBase64(compareRequest);
        System.out.println(compareResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 人脸搜索
    SearchFaceByBase64Request searchRequest = new SearchFaceByBase64Request();
    searchRequest.withFaceSetName("face_set_name");
    FaceSearchBase64Req faceSearchBase64Req = new FaceSearchBase64Req();
    List<Map<String, String>> listbodySort = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
    map.put("timestamp","asc");
    listbodySort.add(map);
    List<String> listbodyReturnFields = new ArrayList<>();
    listbodyReturnFields.add("timestamp");
    listbodyReturnFields.add("id");
    faceSearchBase64Req.withSort(listbodySort);
    faceSearchBase64Req.withReturnFields(listbodyReturnFields);
    faceSearchBase64Req.withImageBase64("/9j/4AAQSkZJRgABAQAAAQABAAD...");
    searchRequest.withBody(faceSearchBase64Req);
    try {
        SearchFaceByBase64Response searchResponse = client.searchFaceByBase64(searchRequest);
        System.out.println(searchResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 创建人脸库
    CreateFaceSetRequest createFaceSetRequest = new CreateFaceSetRequest();
    CreateFaceSetReq createFaceSetReq = new CreateFaceSetReq();
    createFaceSetReq.withFaceSetName("face_set_name");
    Map<String, TypeInfo> stringTypeInfoMap = new HashMap<>();
    TypeInfo typeInfo = new TypeInfo();
    typeInfo.withType("long");
    stringTypeInfoMap.put("timestamp", typeInfo);
    createFaceSetReq.withExternalFields(stringTypeInfoMap);
    createFaceSetRequest.withBody(createFaceSetReq);
    try {
        CreateFaceSetResponse createFaceSetResponse = client.createFaceSet(createFaceSetRequest);
        System.out.println(createFaceSetResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 查询人脸库
    ShowFaceSetRequest showFaceSetRequest = new ShowFaceSetRequest();
    showFaceSetRequest.withFaceSetName("face_set_name");
    try {
        ShowFaceSetResponse showFaceSetResponse = client.showFaceSet(showFaceSetRequest);
        System.out.println(showFaceSetResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 查询所有人脸库
    ShowAllFaceSetsRequest showAllFaceSetsRequest = new ShowAllFaceSetsRequest();
    try {
        ShowAllFaceSetsResponse showAllFaceSetsResponse = client.showAllFaceSets(showAllFaceSetsRequest);
        System.out.println(showAllFaceSetsResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 删除人脸库
    DeleteFaceSetRequest deleteFaceSetRequest = new DeleteFaceSetRequest();
    deleteFaceSetRequest.withFaceSetName("face_set_name");
    try {
        DeleteFaceSetResponse deleteFaceSetResponse = client.deleteFaceSet(deleteFaceSetRequest);
        System.out.println(deleteFaceSetResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 添加人脸
    //add face
    AddFacesByBase64Request addFacesByBase64Request = new AddFacesByBase64Request();
    addFacesByBase64Request.withFaceSetName("face_set_name");
    AddFacesBase64Req addFacesBase64Req = new AddFacesBase64Req();
    addFacesBase64Req.withExternalFields("{\"timestamp\":12}");
    addFacesBase64Req.withImageBase64("9j/4AAQSkZJRgABAQAAAQABAAD...");
    addFacesByBase64Request.withBody(addFacesBase64Req);
    try {
        AddFacesByBase64Response addFacesByBase64Response = client.addFacesByBase64(addFacesByBase64Request);
        System.out.println(addFacesByBase64Response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 删除人脸
    //delete face by faceId
    DeleteFaceByFaceIdRequest deleteFaceByFaceIdRequest = new DeleteFaceByFaceIdRequest();
    deleteFaceByFaceIdRequest.withFaceSetName("face_set_name");
    deleteFaceByFaceIdRequest.withFaceId("iexEBb6t");
    try {
        DeleteFaceByFaceIdResponse deleteFaceByFaceIdResponse = client.deleteFaceByFaceId(deleteFaceByFaceIdRequest);
        System.out.println(deleteFaceByFaceIdResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
    
    //delete face by externalImageId
    DeleteFaceByExternalImageIdRequest deleteFaceRequest = new DeleteFaceByExternalImageIdRequest();
    deleteFaceRequest.withFaceSetName("face_set_name");
    deleteFaceRequest.withExternalImageId("iexEBb6t");
    try {
        DeleteFaceByExternalImageIdResponse response = client.deleteFaceByExternalImageId(deleteFaceRequest);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 批量删除人脸
    BatchDeleteFacesRequest batchDeleteFacesRequest = new BatchDeleteFacesRequest();
    batchDeleteFacesRequest.withFaceSetName("face_set_name");
    DeleteFacesBatchReq deleteFacesBatchReq = new DeleteFacesBatchReq();
    deleteFacesBatchReq.withFilter("age:[20 TO 30]");
    batchDeleteFacesRequest.withBody(deleteFacesBatchReq);
    try {
        BatchDeleteFacesResponse batchDeleteFacesResponse = client.batchDeleteFaces(batchDeleteFacesRequest);
        System.out.println(batchDeleteFacesResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 更新人脸
    UpdateFaceRequest updateFaceRequest = new UpdateFaceRequest();
    updateFaceRequest.withFaceSetName("face_set_name");
    UpdateFaceReq updateFaceReq = new UpdateFaceReq();
    updateFaceReq.withFaceId("iexEBb6t");
    updateFaceRequest.withBody(updateFaceReq);
    try {
        UpdateFaceResponse response = client.updateFace(updateFaceRequest);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 查询人脸
    //show faces by faceId
    ShowFacesByFaceIdRequest showFacesByFaceIdRequest = new ShowFacesByFaceIdRequest();
    showFacesByFaceIdRequest.withFaceSetName("face_set_name");
    showFacesByFaceIdRequest.withFaceId("iexEBb6t");
    try {
        ShowFacesByFaceIdResponse response = client.showFacesByFaceId(showFacesByFaceIdRequest);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
    
    //show faces by limit
    ShowFacesByLimitRequest showFacesByLimitRequest = new ShowFacesByLimitRequest();
    showFacesByLimitRequest.withFaceSetName("face_set_name");
    showFacesByLimitRequest.withOffset(0);
    showFacesByLimitRequest.withLimit(10);
    try {
        ShowFacesByLimitResponse showFacesByLimitResponse = client.showFacesByLimit(showFacesByLimitRequest);
        System.out.println(showFacesByLimitResponse.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 动作活体检测
    //detect live by base64
    DetectLiveByBase64Request request = new DetectLiveByBase64Request();
    LiveDetectBase64Req body = new LiveDetectBase64Req();
    body.withActions("1,2,3,4");
    body.withVideoBase64("/9j/4AAQSkZJRgABAQAAAQABAAD...");
    request.withBody(body);
    try {
        DetectLiveByBase64Response response = client.detectLiveByBase64(request);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }
  • 静默活体检测
    //detect live face by base64
    DetectLiveFaceByBase64Request request = new DetectLiveFaceByBase64Request();
    LiveDetectFaceBase64Req body = new LiveDetectFaceBase64Req();
    body.withImageBase64("/9j/4AAQSkZJRgABAQAAAQABAAD...");
    request.withBody(body);
    try {
        DetectLiveFaceByBase64Response response = client.detectLiveFaceByBase64(request);
        System.out.println(response.toString());
    } catch (ConnectionException e) {
        e.printStackTrace();
    } catch (RequestTimeoutException e) {
        e.printStackTrace();
    } catch (ServiceResponseException e) {
        e.printStackTrace();
        System.out.println(e.getHttpStatusCode());
        System.out.println(e.getErrorCode());
        System.out.println(e.getErrorMsg());
    }

SDK代码自动生成

API Explorer提供API检索及平台调试,支持全量快速检索、可视化调试、帮助文档查看、在线咨询。

您只需要在API Explorer中修改接口参数,即可自动生成对应的代码示例。同时,可在集成开发环境CloudIDE中完成代码的构建、调试、运行等操作。

图2 API Explorer
分享:

    相关文档

    相关产品

close