华为云AI:轻松实现图像识别调用

文章目录


前言

基于华为云AI服务和java使用SDK实现图像识别,主要以媒资图像标签和名人识别为例。


一、环境配置

  • Maven(没有直接下载华为的SDK包,而是使用Maven安装依赖)
  • JDK19(官方的SDK包要求JDK版本必须高于JDK8版本,大家根据自己只要满足版本要求即可)
  • 开发工具:IDEA 2023.3(其他版本也可)
    • 能创建Maven项目即可
  • 开通图像识别服务(目前是免费体验):这里我开通的是图像标签/媒资图像标签和名人识别服务。
  • 设置访问密钥
  • 服务区域:我开通的服务区域是华北-北京四

关键步骤

Maven项目的创建和Java环境变量的配置我就不再赘诉,这是大家学习java早已熟练掌握的,这里只讲诉易错的。

开通图像识别服务

华为云首页就有云产品体验区(找不到就在搜索栏检索),勾选AI:

点击"立即体验"后,找到服务列表,开通你想要的服务(点击开通):

设置访问密钥

在控制台找到"我的凭证":

找到"访问密钥",如果没有就新增,新增后一定要下载密钥的CSV文件,他会有提示让你下载,防止你忘记:

下载完csv文件后用记事本打开即可看到AK和SK:

Maven引入依赖配置

版本可以自己切换

xml 复制代码
		<dependency>
			<groupId>com.huaweicloud.sdk</groupId>
			<artifactId>huaweicloud-sdk-image</artifactId>
			<version>3.1.8</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.70</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.16</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.16.0</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.13.0</version>
		</dependency>

二、图像识别实例

媒资图像标签

功能介绍:对用户传入的图像可以返回图像中的物体名称、所属类别及置信度信息。

使用图片是网上的,仅作学习使用:

代码如下:

java 复制代码
/**
 * @Version: 1.0.0
 * @Author: Dragon_王
 * @ClassName: RunImageMediaTaggingSolution
 * @Description: 媒资图像标签
 * @Date: 2024/1/8 11:51
 */

/**
 * 此demo仅供测试使用,强烈建议使用SDK
 * 使用前需配置依赖jar包。jar包可通过下载SDK获取
 */

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;
import com.huaweicloud.sdk.image.v2.region.ImageRegion;
import com.huaweicloud.sdk.image.v2.*;
import com.huaweicloud.sdk.image.v2.model.*;

public class RunImageMediaTaggingSolution {

    public static void main(String[] args) {
        //此处需要输入您的AK/SK信息
        String ak = "你的AK";
        String sk = "你的SK";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        ImageClient client = ImageClient.newBuilder()
                .withCredential(auth)
                .withRegion(ImageRegion.valueOf("cn-north-4"))  //此处替换为您开通服务的区域
                .build();
        RunImageMediaTaggingRequest request = new RunImageMediaTaggingRequest();
        ImageMediaTaggingReq body = new ImageMediaTaggingReq();
        body.withThreshold(10f);
        body.withLanguage("zh");
        body.withUrl("https://tse2-mm.cn.bing.net/th/id/OIP-C.SIuEnb1-arhtDNqfdICVqAHaE7?rs=1&pid=ImgDetMain");  //此处替换为公网可以访问的图片地址
        request.withBody(body);
        try {
            RunImageMediaTaggingResponse response = client.runImageMediaTagging(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());
        }
    }
}

运行结果:

java 复制代码
class RunImageMediaTaggingResponse {
    result: class ImageMediaTaggingResponseResult {
        tags: [class ImageMediaTaggingItemBody {
            confidence: 83.63
            type: 动物
            tag: 金毛犬
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 金毛犬
                en: Golden retriever
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 81.78
            type: 动物
            tag: 金毛
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 金毛
                en: Golden hair
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 77.00
            type: 动物
            tag: 金毛寻猎犬
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 金毛寻猎犬
                en: Golden Retriever
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 62.60
            type: 动物
            tag: 贵妇犬
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 贵妇犬
                en: Poodle
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 59.02
            type: 生活
            tag: 狗链
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 狗链
                en: Dog chain
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 生活
                en: Life
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 53.84
            type: 动物
            tag: 宠物狗
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 宠物狗
                en: Pet dog
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 48.01
            type: 动物
            tag: 狗狗
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 狗狗
                en: Dog
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 44.02
            type: 动物
            tag: 犬
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 犬
                en: Dog
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 42.11
            type: 动物
            tag: 纯种犬
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 纯种犬
                en: Purebred dog
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }, class ImageMediaTaggingItemBody {
            confidence: 38.65
            type: 动物
            tag: 中华田园犬
            i18nTag: class ImageMediaTaggingItemBodyI18nTag {
                zh: 中华田园犬
                en: Chinese pastoral dog
            }
            i18nType: class ImageMediaTaggingItemBodyI18nType {
                zh: 动物
                en: Animal
            }
            instances: []
        }]
    }
}

Process finished with exit code 0

名人识别

功能介绍:分析并识别图片中包含的敏感人物、明星及网红人物,返回人物信息及人脸坐标。

使用照片是网上的照片,仅作学习使用:

代码如下:

java 复制代码
/**
 * @Version: 1.0.0
 * @Author: Dragon_王
 * @ClassName: RunCelebrityRecognitionSolution
 * @Description: 媒资标签
 * @Date: 2024/1/9 16:23
 */

import com.alibaba.fastjson.JSON;

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;
import com.huaweicloud.sdk.image.v2.ImageClient;
import com.huaweicloud.sdk.image.v2.model.RunCelebrityRecognitionRequest;
import com.huaweicloud.sdk.image.v2.region.ImageRegion;
import com.huaweicloud.sdk.image.v2.model.CelebrityRecognitionReq;
import com.huaweicloud.sdk.image.v2.model.RunCelebrityRecognitionResponse;


public class RunCelebrityRecognitionSolution {

    public static void main(String[] args) {
        // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全
        // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK
        String ak = "你的AK";
        String sk = "你的SK";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        ImageClient client = ImageClient.newBuilder()
                .withCredential(auth)
                .withRegion(ImageRegion.valueOf("cn-north-4"))  //此处替换为您开通服务的区域
                .build();
        RunCelebrityRecognitionRequest request = new RunCelebrityRecognitionRequest();
        CelebrityRecognitionReq body = new CelebrityRecognitionReq();
        body.withThreshold(0f);
        body.withUrl("https://tse1-mm.cn.bing.net/th/id/OIP-C.tM6jifW1xaCDP7Kia9QiYwHaKD?rs=1&pid=ImgDetMain");   //此处替换为公网可以访问的图片地址
        request.withBody(body);
        try {
            RunCelebrityRecognitionResponse response = client.runCelebrityRecognition(request);
            System.out.println(response.getHttpStatusCode());
            System.out.println(JSON.toJSONString(response));
        } 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());
        }
    }
}

运行结果:

java 复制代码
200
{"httpStatusCode":200,"result":[{"confidence":0.9985551,"faceDetail":{"w":132,"h":186,"x":197,"y":79},"label":"成龙"}]}

Process finished with exit code 0

总结

以上就是华为云的AI图像识别服务调用,这里提供官方文档

相关推荐
innutritious20 分钟前
车辆重识别(2020NIPS去噪扩散概率模型)论文阅读2024/9/27
人工智能·深度学习·计算机视觉
橙子小哥的代码世界1 小时前
【深度学习】05-RNN循环神经网络-02- RNN循环神经网络的发展历史与演化趋势/LSTM/GRU/Transformer
人工智能·pytorch·rnn·深度学习·神经网络·lstm·transformer
快乐就好ya1 小时前
Java多线程
java·开发语言
IT学长编程1 小时前
计算机毕业设计 二手图书交易系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·二手图书交易系统
CS_GaoMing2 小时前
Centos7 JDK 多版本管理与 Maven 构建问题和注意!
java·开发语言·maven·centos7·java多版本
艾伦~耶格尔2 小时前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构
985小水博一枚呀2 小时前
【深度学习基础模型】神经图灵机(Neural Turing Machines, NTM)详细理解并附实现代码。
人工智能·python·rnn·深度学习·lstm·ntm
man20172 小时前
基于spring boot的篮球论坛系统
java·spring boot·后端
2401_858120533 小时前
Spring Boot框架下的大学生就业招聘平台
java·开发语言
S hh3 小时前
【Linux】进程地址空间
java·linux·运维·服务器·学习