文章目录
前言
基于华为云AI服务和java使用SDK实现图像识别,主要以媒资图像标签和名人识别为例。
一、环境配置
- Maven(没有直接下载华为的SDK包,而是使用Maven安装依赖)
- JDK19(官方的SDK包要求JDK版本必须高于JDK8版本,大家根据自己只要满足版本要求即可)
- 开发工具:IDEA 2023.3(其他版本也可) - 能创建Maven项目即可
- 开通图像识别服务(目前是免费体验):这里我开通的是图像标签/媒资图像标签和名人识别服务。
- 设置访问密钥
- 服务区域:我开通的服务区域是华北-北京四
关键步骤
Maven项目的创建和Java环境变量的配置我就不再赘诉,这是大家学习java早已熟练掌握的,这里只讲诉易错的。
开通图像识别服务
华为云首页就有云产品体验区(找不到就在搜索栏检索),勾选AI:
点击“立即体验”后,找到服务列表,开通你想要的服务(点击开通):
设置访问密钥
在控制台找到“我的凭证”:
找到“访问密钥”,如果没有就新增,新增后一定要下载密钥的CSV文件,他会有提示让你下载,防止你忘记:
下载完csv文件后用记事本打开即可看到AK和SK:
Maven引入依赖配置
版本可以自己切换
<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>
二、图像识别实例
媒资图像标签
功能介绍:对用户传入的图像可以返回图像中的物体名称、所属类别及置信度信息。
使用图片是网上的,仅作学习使用:
代码如下:
/**
* @Version: 1.0.0
* @Author: Dragon_王
* @ClassName: RunImageMediaTaggingSolution
* @Description: 媒资图像标签
* @Date: 2024/1/8 11:51
*//**
* 此demo仅供测试使用,强烈建议使用SDK
* 使用前需配置依赖jar包。jar包可通过下载SDK获取
*/importcom.huaweicloud.sdk.core.auth.ICredential;importcom.huaweicloud.sdk.core.auth.BasicCredentials;importcom.huaweicloud.sdk.core.exception.ConnectionException;importcom.huaweicloud.sdk.core.exception.RequestTimeoutException;importcom.huaweicloud.sdk.core.exception.ServiceResponseException;importcom.huaweicloud.sdk.image.v2.region.ImageRegion;importcom.huaweicloud.sdk.image.v2.*;importcom.huaweicloud.sdk.image.v2.model.*;publicclassRunImageMediaTaggingSolution{publicstaticvoidmain(String[] args){//此处需要输入您的AK/SK信息String ak ="你的AK";String sk ="你的SK";ICredential auth =newBasicCredentials().withAk(ak).withSk(sk);ImageClient client =ImageClient.newBuilder().withCredential(auth).withRegion(ImageRegion.valueOf("cn-north-4"))//此处替换为您开通服务的区域.build();RunImageMediaTaggingRequest request =newRunImageMediaTaggingRequest();ImageMediaTaggingReq body =newImageMediaTaggingReq();
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());}}}
运行结果:
classRunImageMediaTaggingResponse{
result:classImageMediaTaggingResponseResult{
tags:[classImageMediaTaggingItemBody{
confidence:83.63
type: 动物
tag: 金毛犬
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 金毛犬
en:Golden retriever
}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]},classImageMediaTaggingItemBody{
confidence:81.78
type: 动物
tag: 金毛
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 金毛
en:Golden hair
}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]},classImageMediaTaggingItemBody{
confidence:77.00
type: 动物
tag: 金毛寻猎犬
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 金毛寻猎犬
en:GoldenRetriever}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]},classImageMediaTaggingItemBody{
confidence:62.60
type: 动物
tag: 贵妇犬
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 贵妇犬
en:Poodle}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]},classImageMediaTaggingItemBody{
confidence:59.02
type: 生活
tag: 狗链
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 狗链
en:Dog chain
}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 生活
en:Life}
instances:[]},classImageMediaTaggingItemBody{
confidence:53.84
type: 动物
tag: 宠物狗
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 宠物狗
en:Pet dog
}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]},classImageMediaTaggingItemBody{
confidence:48.01
type: 动物
tag: 狗狗
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 狗狗
en:Dog}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]},classImageMediaTaggingItemBody{
confidence:44.02
type: 动物
tag: 犬
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 犬
en:Dog}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]},classImageMediaTaggingItemBody{
confidence:42.11
type: 动物
tag: 纯种犬
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 纯种犬
en:Purebred dog
}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]},classImageMediaTaggingItemBody{
confidence:38.65
type: 动物
tag: 中华田园犬
i18nTag:classImageMediaTaggingItemBodyI18nTag{
zh: 中华田园犬
en:Chinese pastoral dog
}
i18nType:classImageMediaTaggingItemBodyI18nType{
zh: 动物
en:Animal}
instances:[]}]}}Process finished withexit code 0
名人识别
功能介绍:分析并识别图片中包含的敏感人物、明星及网红人物,返回人物信息及人脸坐标。
使用照片是网上的照片,仅作学习使用:
代码如下:
/**
* @Version: 1.0.0
* @Author: Dragon_王
* @ClassName: RunCelebrityRecognitionSolution
* @Description: 媒资标签
* @Date: 2024/1/9 16:23
*/importcom.alibaba.fastjson.JSON;importcom.huaweicloud.sdk.core.auth.ICredential;importcom.huaweicloud.sdk.core.auth.BasicCredentials;importcom.huaweicloud.sdk.core.exception.ConnectionException;importcom.huaweicloud.sdk.core.exception.RequestTimeoutException;importcom.huaweicloud.sdk.core.exception.ServiceResponseException;importcom.huaweicloud.sdk.image.v2.ImageClient;importcom.huaweicloud.sdk.image.v2.model.RunCelebrityRecognitionRequest;importcom.huaweicloud.sdk.image.v2.region.ImageRegion;importcom.huaweicloud.sdk.image.v2.model.CelebrityRecognitionReq;importcom.huaweicloud.sdk.image.v2.model.RunCelebrityRecognitionResponse;publicclassRunCelebrityRecognitionSolution{publicstaticvoidmain(String[] args){// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全// 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SKString ak ="你的AK";String sk ="你的SK";ICredential auth =newBasicCredentials().withAk(ak).withSk(sk);ImageClient client =ImageClient.newBuilder().withCredential(auth).withRegion(ImageRegion.valueOf("cn-north-4"))//此处替换为您开通服务的区域.build();RunCelebrityRecognitionRequest request =newRunCelebrityRecognitionRequest();CelebrityRecognitionReq body =newCelebrityRecognitionReq();
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());}}}
运行结果:
200{"httpStatusCode":200,"result":[{"confidence":0.9985551,"faceDetail":{"w":132,"h":186,"x":197,"y":79},"label":"成龙"}]}Process finished withexit code 0
总结
以上就是华为云的AI图像识别服务调用,这里提供官方文档
版权归原作者 浩泽学编程 所有, 如有侵权,请联系我们删除。