版本说明

本示例基于华为云SDK V3.0版本开发

功能介绍

用户传入图片通过后台算法判断图片主体,并返回主体坐标。

前置条件

申请服务

申请主体识别服务的具体操作步骤请参见《图像识别API参考》的“如何调用API > 申请服务”章节。

获取AK/SK

开发者在使用前需先获取账号的ak、sk、endpoint。

您需要拥有华为云账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的 AK/SK。更多信息请查看访问密钥。 endpoint 华为云各服务应用区域和各服务的终端节点,详情请查看地区和终端节点。

运行环境

Java JDK 1.8 及其以上版本,推荐通过Maven 安装依赖的方式使用JAVA版本SDK。

SDK获取和安装

在Maven 项目的 pom.xml 文件加入相应版本的依赖项即可。

以引入3.1.2版本的内容审核SDK为例:

<dependency>
    <groupId>com.huaweicloud.sdk</groupId>
    <artifactId>huaweicloud-sdk-image</artifactId>
    <version>3.1.2</version>
</dependency>

示例代码

主体识别示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用ImageMainObjectDetectionReq中的withUrl或withImage方法配置图像信息(image和url参数二选一),示例中以url为例,配置完成后运行即可。

package com.huawei.image.demo

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.image.v2.ImageClient;
import com.huaweicloud.sdk.image.v2.model.ImageMainObjectDetectionReq;
import com.huaweicloud.sdk.image.v2.model.RunImageMainObjectDetectionRequest;
import com.huaweicloud.sdk.image.v2.model.RunImageMainObjectDetectionResponse;
import com.huaweicloud.sdk.image.v2.region.ImageRegion;


public class ImageMainObjectDetectionDemo {
    
    public static void main(String[] args) {
        // 设置AK和SK
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
    
        ICredential auth = new BasicCredentials()
            .withAk(ak)
            .withSk(sk);
    
        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);
        ImageClient client = ImageClient.newBuilder()
            .withCredential(auth)
            .withRegion(ImageRegion.valueOf("cn-north-4")) //示例中区域为北京四,根据实际调用区域替换
            .withHttpConfig(config)
            .build();
        RunImageMainObjectDetectionRequest request = new RunImageMainObjectDetectionRequest();
        ImageMainObjectDetectionReq body = new ImageMainObjectDetectionReq();
        // 替换示例中的图像地址
        body.setUrl("https://sandbox-expriment-files.obs.cn-north-1.myhuaweicloud.com/20220510/image_reco_test.jpg");
        request.withBody(body);
        try {
            RunImageMainObjectDetectionResponse response = client.runImageMainObjectDetection(request);
            System.out.println(response.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

参考

更多信息请参考主体识别API

修订记录

发布日期 文档版本 修订说明
2022-09-28 1.0 文档首次发布