图像搜索 SDK 文档
文档版本 01
发布日期 2021-12-20
版权所有 © 华为技术有限公司 2021。 保留一切权利。
非经本公司书面许可,任何单位和个人不得擅自摘抄、复制本文档内容的部分或全部,并不得以任何形式传 播。
商标声明
和其他华为商标均为华为技术有限公司的商标。
本文档提及的其他所有商标或注册商标,由各自的所有人拥有。
注意
您购买的产品、服务或特性等应受华为公司商业合同和条款的约束,本文档中描述的全部或部分产品、服务或 特性可能不在您的购买或使用范围之内。除非合同另有约定,华为公司对本文档内容不做任何明示或暗示的声 明或保证。
由于产品版本升级或其他原因,本文档内容会不定期进行更新。除非另有约定,本文档仅作为使用指导,本文 档中的所有陈述、信息和建议不构成任何明示或暗示的担保。
华为技术有限公司
地址: 深圳市龙岗区坂田华为总部办公楼 邮编:518129
网址:
https://www.huawei.com
客户服务邮箱:
[email protected]
客户服务电话:4008302118目 录
1 图像搜索 SDK 简介...1
2 获取认证信息... 3
3 准备环境...4
4 SDK 列表及指导文档(通用版)... 5
5 使用 SDK(Java)... 6
5.1 JAVA 开发环境配置... 6
5.2 JAVA 语言 SDK 获取和安装... 6
5.3 创建实例示例... 7
5.4 添加图片示例... 8
5.5 搜索图片示例... 9
5.6 查询图片示例...10
5.7 删除图片示例...11
5.8 修改图片信息示例... 12
5.9 删除实例示例...14
5.10 查询用户实例示例... 15
6 使用 SDK(Python)... 17
6.1 Python 开发环境配置...17
6.2 Python 语言 SDK 获取和安装... 18
6.3 创建实例示例...19
6.4 添加图片示例...20
6.5 搜索图片示例...21
6.6 查询图片示例...21
6.7 删除图片示例...22
6.8 修改图片信息示例... 23
6.9 删除实例示例...24
6.10 查询用户实例示例... 24
7 使用 SDK(.NET)... 26
7.1 .NET 开发环境配置... 26
7.2 .Net 语言 SDK 获取和安装...26
7.3 创建实例示例...27
7.4 添加图片示例...28
7.5 搜索图片示例...29
7.6 查询图片示例...31
7.7 删除图片示例...32
7.8 修改图片信息示例... 33
7.9 删除实例示例...34
7.10 查询用户实例示例... 35
8 使用 SDK(GO)... 38
8.1 GO 开发环境配置... 38
8.2 GO 语言 SDK 获取和安装... 38
8.3 创建实例示例...38
8.4 添加图片示例...39
8.5 搜索图片示例...40
8.6 查询图片示例...41
8.7 删除图片示例...42
8.8 修改图片信息示例... 42
8.9 删除实例示例...43
8.10 查询用户实例示例... 44
1 图像搜索 SDK 简介
图像搜索概述
图像搜索(Image Search)基于深度学习与图像识别技术,结合不同应用业务和行业 场景,利用特征向量化与搜索能力,帮助您从指定图库中搜索相同或相似的图片。
图像搜索服务以开放API(Application Programming Interface,应用程序编程接口)
的方式提供给用户,用户通过实时访问和调用API获取图像搜索结果,帮助用户在图像 库中进行相同或相似图像搜索。
SDK 概述
图像搜索软件开发工具包(Image Search Software Development Kit)是对图像搜索 提供的REST API进行的封装,以简化用户的开发工作。用户直接调用图像搜索SDK提 供的接口函数即可实现使用图像搜索业务能力的目的。
说明
图像搜索服务提供的SDK暂不支持访问重试,如果访问异常,SDK会直接返回本次的请求结果,
所以需要客户端自行建立重试机制。
接口与 API 对应关系
图像搜索接口与API对应关系请参见表1-1。
表1-1 接口与 API 对应关系
接口 API
创建实例 POST /v1/{projectId}/service
添加图片 POST /v1/{projectId}/{instanceName}/image
搜索图片 POST /v1/{projectId}/{instanceName}/image/search 查询图片 POST /v1/{projectId}/{instanceName}/image/check 删除图片 DELETE /v1/{projectId}/{instanceName}/image 修改图片信息 PUT /v1/{projectId}/{instanceName}/image
接口 API
删除实例 DELETE /v1/{projectId}/service/{instanceName}
查询用户实例信息 GET /v1/{projectId}/service/{instanceName}
2 获取认证信息
使用服务API需要进行认证,推荐用户使用AK/SK方式。步骤如下:
步骤1 注册并登录华为云管理控制台。
步骤2 鼠标移动至用户名处,在下拉列表中单击“我的凭证”。
步骤3 选择“访问密钥”页签,单击“新增访问秘钥”按钮。
步骤4 通过邮箱或者手机进行验证,输入对应的验证码。
步骤5 单击“确定”,下载认证账号的AK/SK,AK/SK数据会以本地文件的形式保存,请妥善 保管。
----结束
3 准备环境
在使用图像搜索SDK时,各语言需要准备的环境,参考表3-1。
表3-1 开发环境 开发语
言
准备项 说明
JAVA 安装JDK JAVA环境开发配置,支持 Java JDK 1.8 及其以上版本,推荐 通过Maven 安装依赖的方式使用JAVA版本SDK。
PYTHO
N 安装
python python版本sdk支持python3.3及以上版本。
.NET 安装.NET .NET Standard 2.0 及其以上版本或C# 4.0 及其以上版本。
GO 安装go 支持 go 1.14 及以上版本。
4 SDK 列表及指导文档(通用版)
API Explorer能根据需要动态生成SDK代码功能,降低您使用SDK的难度,推荐使用。
SDK中心提供服务SDK依赖引入的方式。
表4-1提供了各个云服务支持的SDK列表,您可以在GitHub仓库查看SDK更新历史、获
取安装包以及查看指导文档,参照进行进阶配置。表4-1 SDK 列表
编程语言 github地址 指导文档 视频指导
JAVA
huaweicloud-
sdk-java-v3 Java SDK使用指导 Java SDK视频指导
PYTHONhuaweicloud-
sdk-python-v3 Python SDK使用 指导
Python SDK视频 指导
.NET
huaweicloud-
sdk-net-v3 .Net SDK使用指导
-GO
huaweicloud-
sdk-go-v3 Go SDK使用指导
-5 使用 SDK(Java)
5.1 JAVA 开发环境配置
SDK包要求JDK版本必须高于JDK8,如下操作以win7环境配置JDK8 64位为例,若已经 下载JDK并配置好环境请忽略本章节。
步骤1 下载JDK文件。
步骤2 下载完成后按照提示安装,位置自选,比如安装到本地C:\Program Files\Java
\jdk1.8.0_131。
步骤3 配置Java环境变量:右键“计算机>属性>高级系统设置>环境变量”,进行如下操作:
● 新建系统变量JAVA_HOME,变量值为实际JDK安装位置。
● 在Path中添加%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin (注意用英文分号 分隔)。
● 新建系统变量CLASSPATH,变量值为%JAVA_HOME%\lib\dt.jar;%JAVA_HOME
%\lib\tools.jar 。
步骤4 打开命令行窗口,输入“java -version”,显示如图1 Java版本信息表示配置成功。
图5-1 Java 版本信息
----结束
5.2 JAVA 语言 SDK 获取和安装
JAVA版本图像搜索的SDK推荐您通过 Maven 安装依赖的方式使用华为云 Java SDK:
步骤1 首先您需要在您的操作系统中下载 并 安装Maven 。
步骤2 安装配置完成后,输入命令“mvn -v”,显示如下图 Maven版本信息 表示成功。
步骤3 Maven安装完成后,可根据开发需要,直接引入依赖到已有的Maven工程或先用开发 工具创建Maven工程。
创建项目以idea开发工具为例(已有Maven 工程可跳过此步骤):
1. 打开IntelliJ IDEA 开发工具。
2. 点击File - New - project...
3. 在New Project弹窗点击-Maven-点击Next。
4. 输入GroupId和ArtifactId,点击Next。
5. 输入Project name 和 Project location,点击Finish。
步骤4 在Maven 项目的 pom.xml 文件加入相应的依赖项即可。
以引入3.0.55版本的图像搜索SDK为例:
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-imagesearch</artifactId>
<version>3.0.67</version>
</dependency>
示例代码因需要输出结果更直观,所以新增fastjson的依赖(如接入不涉及此项,可忽 略并在示例代码中删除相关内容),依赖如下:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
----结束
5.3 创建实例示例
本章节对创建实例AK/SK方式使用SDK进行示例说明。
图像搜索示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用
CreateInstanceReq类的setName和setModel方法配置实例名称和模型,配置完成后运 行即可。
1. 创建实例调用示例代码如下:
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.imagesearch.v1.ImageSearchClient;
import com.huaweicloud.sdk.imagesearch.v1.model.RunCreateInstanceRequest;
import com.huaweicloud.sdk.imagesearch.v1.model.CreateInstanceReq;
import com.huaweicloud.sdk.imagesearch.v1.model.RunCreateInstanceResponse;
import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
import java.util.Arrays;
import java.util.List;
/** * 创建实例,实例中会生成图片索引库,用来存放图片特征。
*/public class RunCreateInstanceSolution { public static void main(String[] args) { //设置获取的AK和SK
String ak = "<YOUR AK>";
String sk = "<YOUR SK>";
ICredential auth = new BasicCredentials() .withAk(ak)
.withSk(sk);
ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth)
//设置region, 示例中为北京四
.withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build();
RunCreateInstanceRequest request = new RunCreateInstanceRequest();
CreateInstanceReq body = new CreateInstanceReq();
//设置实例名称
body.setName("instance-name");
//设置模型
body.setModel("common-search");
List<String> tags = Arrays.asList("animal", "plant");
body.setTags(tags);
request.setBody(body);
try {
RunCreateInstanceResponse response = client.runCreateInstance(request);
System.out.println(JSON.toJSONString(response));
} catch (ConnectionException e) { e.printStackTrace();
} catch (RequestTimeoutException e) { e.printStackTrace();
} catch (ServiceResponseException e) { System.out.println(e.getHttpStatusCode());
System.out.println(e.getErrorCode());
System.out.println(e.getErrorMsg());
} } }
2. 执行示例代码,控制台返回200即表示程序执行成功。创建实例结果输出到控制 台。SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
{"desc":"","domain":"通用图片搜索","expiredDate":-1,"httpStatusCode":200,"instanceName":"instance- name","level":30000000,"registerDate":1636700045229,"status":{"value":"NORMAL"}},"tags":
["animal","plant"]}
Process finished with exit code 0
5.4 添加图片示例
本章节对名人识别AK/SK方式使用SDK进行示例说明。
添加图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用
AddPictureRequestReq类的setPath方法配置图像的url,配置完成后运行即可。
1. 添加图片调用示例代码如下:
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.imagesearch.v1.*;
import com.huaweicloud.sdk.imagesearch.v1.model.*;
import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
/** * 添加图片
*/public class RunAddPictureSolution { public static void main(String[] args) { //设置获取的AK和SK
String ak = "<YOUR AK>";
String sk = "<YOUR SK>";
ICredential auth = new BasicCredentials() .withAk(ak)
.withSk(sk);
ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth)
//设置region, 示例中region为北京四
.withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build();
RunAddPictureRequest request = new RunAddPictureRequest();
request.setInstanceName("instance-name");
AddPictureRequestReq req = new AddPictureRequestReq();
//添加图片,setPath设置图像的URL
req.setPath("https://bucketname.obs.cn-north-4.myhuaweicloud.com/image/test1.jpg");
request.setBody(req);
try {
RunAddPictureResponse response = client.runAddPicture(request);
System.out.println(JSON.toJSONString(response));
} catch (ConnectionException e) { e.printStackTrace();
} catch (RequestTimeoutException e) { e.printStackTrace();
} catch (ServiceResponseException e) { System.out.println(e.getHttpStatusCode());
System.out.println(e.getErrorCode());
System.out.println(e.getErrorMsg());
} } }
2. 执行示例代码文件,控制台输出添加图片结果即表示程序执行成功。添加图片结
果输出到控制台。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
{"httpStatusCode":200,"result":"Success."}
Process finished with exit code 0
5.5 搜索图片示例
本章节对搜索图片AK/SK方式使用SDK进行示例说明。
搜图图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用 SearchPictureReq类的setPath设置图像的URL,配置完成后运行即可。
1. 搜索图片调用示例代码如下:
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.imagesearch.v1.*;
import com.huaweicloud.sdk.imagesearch.v1.model.*;
import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
/** * 从图片索引库中搜索相似图片
*/public class RunSearchPictureSolution { public static void main(String[] args) { //设置获取的AK和SK
String ak = "<YOUR AK>";
String sk = "<YOUR SK>";
ICredential auth = new BasicCredentials() .withAk(ak)
.withSk(sk);
ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth)
//设置region, 示例中region为北京四
.withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build();
RunSearchPictureRequest request = new RunSearchPictureRequest();
//设置实例名称
request.setInstanceName("instance-name");
SearchPictureReq body = new SearchPictureReq();
//设置图像URL路径
body.setPath("https://bucketname.obs.cn-north-4.myhuaweicloud.com/search-test.jpg");
request.setBody(body);
try {
RunSearchPictureResponse response = client.runSearchPicture(request);
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());
} } }
2. 执行示例代码文件,控制台输出搜索图片结果即表示程序执行成功。搜索图片结
果输出到控制台。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
{"count":1,"httpStatusCode":200,"result":[{"path":"https://bucketName.obs.cn- north-4.myhuaweicloud.com/search-test.jpg","sim":1.0,"tags":{}}]}
Process finished with exit code 0
5.6 查询图片示例
本章节对查询图片AK/SK方式使用SDK进行示例说明。
查询图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用 DeletePictureReq类的setPath设置图像URL,配置完成后运行即可。
1. 查询图片调用示例代码如下:
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.imagesearch.v1.*;
import com.huaweicloud.sdk.imagesearch.v1.model.*;
import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
/** * 通过图片路径查询索引库中对应图片是否存在
*/public class RunCheckPictureSolution { public static void main(String[] args) { //设置获取的AK和SK
String ak = "<YOUR AK>";
String sk = "<YOUR SK>";
ICredential auth = new BasicCredentials() .withAk(ak)
.withSk(sk);
ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth)
//示例region为北京四,需更改为实际的region
.withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build();
RunCheckPictureRequest request = new RunCheckPictureRequest();
//设置实例名称
request.setInstanceName("instance-name");
DeletePictureReq body = new DeletePictureReq();
//图片URL路径,作为图片库中索引图片的ID
body.setPath("https://bucketname.obs.cn-north4.myhuaweicloud.com/searchtest.jpg");
request.setBody(body);
try {
RunCheckPictureResponse response = client.runCheckPicture(request);
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());
} } }
2. 执行示例代码文件,控制台输出查询图片结果即表示程序执行成功。查询图片结
果输出到控制台。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
{"exist":"true","httpStatusCode":200}
Process finished with exit code 0
5.7 删除图片示例
本章节对删除图片AK/SK方式使用SDK进行示例说明。
删除图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用 DeletePictureReq类的setPath设置图像的URL,配置完成后运行即可。
1. 删除图片调用示例代码如下:
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.imagesearch.v1.*;
import com.huaweicloud.sdk.imagesearch.v1.model.*;
import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
/** * 通过图片路径删除索引库中对应图片
*/public class RunDeletePictureSolution { public static void main(String[] args) { //设置获取的AK和SK
String ak = "<YOUR AK>";
String sk = "<YOUR SK>";
ICredential auth = new BasicCredentials() .withAk(ak)
.withSk(sk);
ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth)
//设置region,示例中region为北京四
.withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build();
RunDeletePictureRequest request = new RunDeletePictureRequest();
//设置实例名称
request.setInstanceName("instance-name");
DeletePictureReq body= new DeletePictureReq();
//图片URL路径,作为图片库中索引图片的ID。
body.setPath("https://bucketname.obs.cn-north-4.myhuaweicloud.com/search-test.jpg");
request.setBody(body);
try {
RunDeletePictureResponse response = client.runDeletePicture(request);
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());
} } }
2. 执行示例代码文件,控制台输出删除图片结果即表示程序执行成功。删除图片结
果输出到控制台。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
{"httpStatusCode":200,"result":"Success."}
Process finished with exit code 0
5.8 修改图片信息示例
本章节对修改图片信息AK/SK方式使用SDK进行示例说明。
修改图片信息示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用
RunModifyPictureReq类的setPath和setTags方法配置图像的路径和标签,配置完成后 运行即可。
1. 修改图片调用示例代码如下:
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.imagesearch.v1.*;
import com.huaweicloud.sdk.imagesearch.v1.model.*;
import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
import java.util.HashMap;
import java.util.Map;
/** * 修改图像索引库中已存在的图片信息。
*/public class RunModifyPictureSolution { public static void main(String[] args) { //设置获取的AK和SK
String ak = "<YOUR AK>";
String sk = "<YOUR SK>";
ICredential auth = new BasicCredentials() .withAk(ak)
.withSk(sk);
ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth)
//设置region,示例中region为北京四
.withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build();
RunModifyPictureRequest request = new RunModifyPictureRequest();
//设置实例名称
request.setInstanceName("instance-name");
RunModifyPictureReq body = new RunModifyPictureReq();
//图片URL路径,作为图片库中索引图片的ID。
body.setPath("https://bucketname.obs.cn-north-4.myhuaweicloud.com/search-test.jpg");
//自定义标签,格式为key:value对
Map<String, String> tags = new HashMap<>();
tags.put("animal", "dog");
body.setTags(tags);
request.setBody(body);
try {
RunModifyPictureResponse response = client.runModifyPicture(request);
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());
} } }
2. 执行示例代码文件,控制台输出修改图片信息结果即表示程序执行成功。修改图
片信息结果输出到控制台。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
{"httpStatusCode":200,"result":"Success."}
Process finished with exit code 0
5.9 删除实例示例
本章节对删除实例AK/SK方式使用SDK进行示例说明。
删除实例示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用
RunDeleteInstanceRequest类的setInstanceName配置实例名称,配置完成后运行即 可。
1. 删除实例调用示例代码如下:
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.imagesearch.v1.ImageSearchClient;
import com.huaweicloud.sdk.imagesearch.v1.model.RunDeleteInstanceRequest;
import com.huaweicloud.sdk.imagesearch.v1.model.RunDeleteInstanceResponse;
import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
/** * 删除已存在的实例。
*/public class RunDeleteInstanceDemo { public static void main(String[] args) { //设置获取的AK和SK
String ak = "<YOUR AK>";
String sk = "<YOUR SK>";
ICredential auth = new BasicCredentials() .withAk(ak)
.withSk(sk);
ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth)
//设置region, 示例中region为北京四
.withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build();
RunDeleteInstanceRequest request = new RunDeleteInstanceRequest();
//设置实例名称
request.setInstanceName("instance-name");
try {
RunDeleteInstanceResponse response = client.runDeleteInstance(request);
System.out.println(JSON.toJSONString(response));
} catch (ConnectionException e) { System.out.println(e.getMessage());
} catch (RequestTimeoutException e) { System.out.println(e.getMessage());
} catch (ServiceResponseException e) { System.out.println(e.getHttpStatusCode());
System.out.println(e.getErrorCode());
System.out.println(e.getErrorMsg());
} } }
2. 执行示例代码文件,控制台输出删除实例结果即表示程序执行成功。删除实例结
果输出到控制台。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
{"httpStatusCode":200,"result":"success."}
Process finished with exit code 0
5.10 查询用户实例示例
本章节对查询用户实例AK/SK方式使用SDK进行示例说明。
查询用户实例示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用
RunQueryInstanceRequest类的setInstanceName配置实例名称,配置完成后运行即 可。
1. 查询用户实例调用示例代码如下:
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.imagesearch.v1.*;
import com.huaweicloud.sdk.imagesearch.v1.model.*;
import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
/** * 查看用户指定实例详细信息。
*/public class RunQueryInstanceSolution { public static void main(String[] args) { //设置获取的AK和SK
String ak = "<YOUR AK>";
String sk = "<YOUR SK>";
ICredential auth = new BasicCredentials() .withAk(ak)
.withSk(sk);
ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth)
//设置region, 示例中region为北京四
.withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build();
RunQueryInstanceRequest request = new RunQueryInstanceRequest();
//设置实例名称
request.setInstanceName("instance-name");
try {
RunQueryInstanceResponse response = client.runQueryInstance(request);
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());
} } }
2. 执行示例代码文件,控制台输出查询用户实例结果即表示程序执行成功。查询用
户实例结果输出到控制台。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
{"desc":"","domain":"通用图片搜索","expiredDate":-1,"httpStatusCode":200,"instanceName":"instance- name","level":30000000,"registerDate":1637230766199,"status":{"value":"NORMAL"},"tags":
["animal","plant"]}
Process finished with exit code 0
6 使用 SDK(Python)
6.1 Python 开发环境配置
使用图像搜索Python版本SDK包,需要您配置Python开发环境,步骤如下:
步骤1 从Python官网下载并安装合适的Python版本。请使用Python3.3以上版本,如下以 Python3.7 版本为例进行说明。
步骤2 从PyCharm官网下载并安装最新版本。
步骤3 在PyCharm开发工具中配置Python环境,在菜单依次选择“File > Settings > Project Interpreter”。
步骤4 在页面上方选择您的Python安装路径,如图6-1所示。选择好目标Python之后单击页 面下方“Apply”完成配置。
图6-1 PyCharm 配置 python 环境
----结束
6.2 Python 语言 SDK 获取和安装
python语言SDK语言获取和安装推荐使用pip命令进行安装或使用pycharm进行安装,
需要安装huaweicloudsdkcore包以及huaweicloudsdkimagesearch,参考方法如下:
pip 安装:
# 安装核心库
pip install huaweicloudsdkcore
# 安装图像搜索服务库
pip install huaweicloudsdkimagesearch 使用pycharm安装,步骤如下:
1. 打开pycharm,点击File -> Settings...
2. 点击Python Interpreter -> 点击+
3. 点击+,分别搜索huaweicloudsdkcore及huaweicloudsdkimagesearch,搜索到包内容点击左下角Install Package完成安装
图6-2 pycharm 安装图像搜索 python 版本 sdk 包
6.3 创建实例示例
本章节对创建实例AK/SK方式使用SDK进行示例说明。
创建实例示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化 CreateInstanceReq中的name和model配置实例名称和模型,配置完成后运行即可。
1. 创建实例调用示例代码如下:
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkimagesearch.v1 import *
from huaweicloudsdkimagesearch.v1.region.imagesearch_region import ImageSearchRegion if __name__ == "__main__":
# 设置获取的AK和SK ak = "<YOUR AK>"
sk = "<YOUR SK>"
credentials = BasicCredentials(ak, sk) client = ImageSearchClient.new_builder() \ .with_credentials(credentials) \
.with_region(ImageSearchRegion.value_of("cn-north-4")) \ .build()
try:
request = RunCreateInstanceRequest() # 设置创建实例参数
request.body = CreateInstanceReq(
name="instance-name", # 实例名称 model="common-search", # 模型 tags=["animal", "plant"] # 标签
)
response = client.run_create_instance(request) print(response.status_code)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
2. 执行示例代码,控制台返回200即表示程序执行成功。创建实例结果输出到控制 台。200
{"domain": "通用图片搜索", "desc": "", "registerDate": 1637548249353, "expiredDate": -1, "level":
30000000, "tags": ["animal", "plant"], "status": "NORMAL", "instanceName": "instance-name"}
Process finished with exit code 0
6.4 添加图片示例
本章节对添加图片AK/SK方式使用SDK进行示例说明。
添加图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化 AddPictureRequestReq中的setPath配置图像URL,配置完成后运行即可。
1. 添加图片调用示例代码如下:
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkimagesearch.v1 import *
from huaweicloudsdkimagesearch.v1.region.imagesearch_region import ImageSearchRegion if __name__ == "__main__":
# 设置获取的AK和SK ak = "<YOUR AK>"
sk = "<YOUR SK>"
credentials = BasicCredentials(ak, sk) client = ImageSearchClient.new_builder() \ .with_credentials(credentials) \
.with_region(ImageSearchRegion.value_of("cn-north-4")) \ .build()
try:
request = RunAddPictureRequest() # 设置实例名称
request.instance_name="instance-name"
# 设置图片地址
request.body = AddPictureRequestReq(
path="https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch-test.jpg"
)
response = client.run_add_picture(request) print(response.status_code)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
2. 执行示例代码文件,控制台输出添加图片结果即表示程序执行成功。添加图片结
果输出到控制台。
200 {"result": "Success."}
6.5 搜索图片示例
本章节对搜索图片AK/SK方式使用SDK进行示例说明。
搜索图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化 SearchPictureReq中的path配置图像的URL,配置完成后运行即可。
1. 搜过图片调用示例代码如下:
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkimagesearch.v1 import *
from huaweicloudsdkimagesearch.v1.region.imagesearch_region import ImageSearchRegion if __name__ == "__main__":
# 设置获取的AK和SK ak = "<YOUR AK>"
sk = "<YOUR SK>"
credentials = BasicCredentials(ak, sk) client = ImageSearchClient.new_builder() \ .with_credentials(credentials) \
.with_region(ImageSearchRegion.value_of("cn-north-4")) \ .build()
try:
request = RunSearchPictureRequest() # 设置实例名称
request.instance_name="instance-name"
# 设置图片路径
request.body=SearchPictureReq(
path="https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch-test.jpg"
)
response = client.run_search_picture(request) print(response.status_code)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
2. 执行示例代码文件,控制台输出搜索图片结果即表示程序执行成功。搜索图片结
果输出到控制台。
200 {"count": 1, "result": [{"path": "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch- test.jpg", "sim": 1.0, "tags": {}}]}
6.6 查询图片示例
本章节对查询图片AK/SK方式使用SDK进行示例说明。
查询图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化 DeletePictureReq中的path配置图像的URL,配置完成后运行即可。
1. 查询图片调用示例代码如下:
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkimagesearch.v1 import *
from huaweicloudsdkimagesearch.v1.region.imagesearch_region import ImageSearchRegion if __name__ == "__main__":
# 设置获取的AK和SK ak = "<YOUR AK>"
sk = "<YOUR SK>"
credentials = BasicCredentials(ak, sk) client = ImageSearchClient.new_builder() \ .with_credentials(credentials) \
.with_region(ImageSearchRegion.value_of("cn-north-4")) \ .build()
try:
request = RunCheckPictureRequest() # 设置实例名称
request.instance_name="instance-name"
# 设置图片路径
request.body=DeletePictureReq(
path="https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch-test.jpg"
)
response = client.run_check_picture(request) print(response.status_code)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
2. 执行示例代码文件,控制台输出查询图片结果即表示程序执行成功。查询图片结
果输出到控制台。
200 {
"exist": "true"
}
6.7 删除图片示例
本章节对删除图片AK/SK方式使用SDK进行示例说明。
删除图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化 CelebrityRecognitionReq中的url或image配置图像信息(image和url参数二选一),
示例中以url为例,配置完成后运行即可。
1. 删除图片调用示例代码如下:
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkimagesearch.v1 import *
from huaweicloudsdkimagesearch.v1.region.imagesearch_region import ImageSearchRegion if __name__ == "__main__":
ak = "<YOUR AK>"
sk = "<YOUR SK>"
credentials = BasicCredentials(ak, sk) client = ImageSearchClient.new_builder() \ .with_credentials(credentials) \
.with_region(ImageSearchRegion.value_of("cn-north-4")) \ .build()
try:
request = RunDeletePictureRequest() # 设置实例名称
request.instance_name="instance-name"
# 设置图片地址
request.body = DeletePictureReq(
path="https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch-test.jpg"
)
response = client.run_delete_picture(request) print(response.status_code)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
2. 执行示例代码文件,控制台输出删除图片结果即表示程序执行成功。删除图片结
果输出到控制台。
200 {
"result": "success."
}
6.8 修改图片信息示例
本章节对修改图片信息AK/SK方式使用SDK进行示例说明。
修改图片信息示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化 RunModifyPictureReq中的path和tags配置图像的路径和标签,配置完成后运行即可。
1. 修改图片信息调用示例代码如下:
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkimagesearch.v1 import *
from huaweicloudsdkimagesearch.v1.region.imagesearch_region import ImageSearchRegion if __name__ == "__main__":
# 设置获取的AK和SK ak = "<YOUR AK>"
sk = "<YOUR SK>"
credentials = BasicCredentials(ak, sk) client = ImageSearchClient.new_builder() \ .with_credentials(credentials) \
.with_region(ImageSearchRegion.value_of("cn-north-4")) \ .build()
try:
request = RunModifyPictureRequest() # 设置实例名称
request.instance_name="instance-name"
#设置图片url和标签
request.body=RunModifyPictureReq(
path="https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch-test.jpg", tags={"animal": "dog"}
)
response = client.run_modify_picture(request) print(response.status_code)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
2. 执行示例代码文件,控制台输出修改图片信息结果即表示程序执行成功。修改图
片信息结果输出到控制台。
200 {
"result": "Success."
}
6.9 删除实例示例
本章节删除实例AK/SK方式使用SDK进行示例说明。
删除实例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化
RunDeleteInstanceRequest中的instance_name配置实例名称,配置完成后运行即 可。
1. 删除实例调用示例代码如下:
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkimagesearch.v1 import *
from huaweicloudsdkimagesearch.v1.region.imagesearch_region import ImageSearchRegion if __name__ == "__main__":
# 设置获取的AK和SK ak = "<YOUR AK>"
sk = "<YOUR SK>"
credentials = BasicCredentials(ak, sk) client = ImageSearchClient.new_builder() \ .with_credentials(credentials) \
.with_region(ImageSearchRegion.value_of("cn-north-4")) \ .build()
try:
request = RunDeleteInstanceRequest() #设置实例名称
request.instance_name = "instance-name"
response = client.run_delete_instance(request) print(response.status_code)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
2. 执行示例代码文件,控制台输出删除实例结果即表示程序执行成功。删除实例结
果输出到控制台。
200 {
"result": "Success."
}
6.10 查询用户实例示例
本章节对查询用户实例AK/SK方式使用SDK进行示例说明。
查询用户实例示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化 RunQueryInstanceRequest中的instance_name配置实例名称,配置完成后运行即可。
1. 查询用户实例调用示例代码如下:
# coding: utf-8
from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkimagesearch.v1 import *
from huaweicloudsdkimagesearch.v1.region.imagesearch_region import ImageSearchRegion if __name__ == "__main__":
#设置获取的AK和SK ak = "<YOUR AK>"
sk = "<YOUR SK>"
credentials = BasicCredentials(ak, sk) client = ImageSearchClient.new_builder() \ .with_credentials(credentials) \
.with_region(ImageSearchRegion.value_of("cn-north-4")) \ .build()
try:
request = RunQueryInstanceRequest() #设置实例名称
request.instance_name = "instance-name"
response = client.run_query_instance(request) print(response.status_code)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
2. 执行示例代码文件,控制台输出查询用户实例结果即表示程序执行成功。查询用
户实例结果输出到控制台。
200 {"domain": "通用图片搜索", "desc": "", "registerDate": 1637548249353, "expiredDate": -1, "level":
30000000, "tags": ["animal", "plant"], "status": "NORMAL", "instanceName": "instance-name"}
7 使用 SDK(.NET)
7.1 .NET 开发环境配置
使用图像搜索.NET版本SDK包,需要先配置.Net开发环境。
1. 从Visual Studio官网,下载最新版Visual Studio。
2. 下载完毕后,点击exe文件,开始安装。
3. 安装过程中选择“工作负荷 > ASP.NET和Web开发”,如图7-1所示,安装完成后 启动即可。
图7-1 选择安装.NET 环境依赖
7.2 .Net 语言 SDK 获取和安装
.NET语言SDK的获取和安装可以使用可以通过命令方式安装或使用Visual Studio开发 工具安装第三方引用,参考方法如下:
使用 .NET CLI 工具
dotnet add package HuaweiCloud.SDK.Core dotnet add package HuaweiCloud.SDK.ImageSearch 使用 Package Manager
Install-Package HuaweiCloud.SDK.Core Install-Package HuaweiCloud.SDK.ImageSearch
使用Visual Studio开发工具安装第三方引用:点击工具->选择NuGet包管理器->点击 管理解决方案的NuGet程序包,选择浏览中搜索并安装HuaweiCloud.SDK.Core及 HuaweiCloud.SDK.ImageSearch,如下图所示:
图7-2 安装图像搜索.NET 版本 sdk 包
7.3 创建实例示例
本章节对创建实例AK/SK方式使用SDK进行示例说明。
创建实例示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化 CreateInstanceReq中的Name和Model配置实例的名称和模型,配置完成后运行即 可。
1. 创建实例调用示例代码如下:
using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.ImageSearch;
using HuaweiCloud.SDK.ImageSearch.V1;
using HuaweiCloud.SDK.ImageSearch.V1.Model;
using Newtonsoft.Json;
namespace RunCreateInstanceSolution { class Program
{
static void Main(string[] args) {
const string ak = "<YOUR AK>";
const string sk = "<YOUR SK>";
var config = HttpConfig.GetDefaultConfig();
config.IgnoreSslVerification = true;
var auth = new BasicCredentials(ak, sk);
var client = ImageSearchClient.NewBuilder() .WithCredential(auth)
.WithRegion(ImageSearchRegion.ValueOf("cn-north-4")) .WithHttpConfig(config)
.Build();
var req = new RunCreateInstanceRequest {
};
req.Body = new CreateInstanceReq() {
Name = "instance-name", Model = "common-search",
Tags = new List<string>{"animal", "plant"}
};
try {
var resp = client.RunCreateInstance(req);
var respStatusCode = resp.HttpStatusCode;
Console.WriteLine(respStatusCode);
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
Console.WriteLine(clientRequestException.ErrorMsg);
}
catch (ConnectionException connectionException) {
Console.WriteLine(connectionException.ErrorMessage);
} } } }
2. 执行示例代码,控制台返回200即表示程序执行成功。创建实例结果输出到控制 台。200
{ "instanceName": "instance-name", "level": 30000000,
"expiredDate": -1, "domain": "通用图片搜索", "desc": "",
"registerDate": 1638939677331, "tags"[
"animal", "plant"
], "status": "NORMAL"
}
7.4 添加图片示例
本章节对添加图片呢AK/SK方式使用SDK进行示例说明。
添加图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用 AddPictureRequestReq类的Path配置图片的URL,配置完成后运行即可。
1. 添加图片调用示例代码如下:
using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.ImageSearch;
using HuaweiCloud.SDK.ImageSearch.V1;
using HuaweiCloud.SDK.ImageSearch.V1.Model;
using Newtonsoft.Json;
namespace RunCreateInstanceSolution { class Program
{
static void Main(string[] args) {
const string ak = "<YOUR AK>";
const string sk = "<YOUR SK>";
var config = HttpConfig.GetDefaultConfig();
config.IgnoreSslVerification = true;
var auth = new BasicCredentials(ak, sk);
var client = ImageSearchClient.NewBuilder() .WithCredential(auth)
.WithRegion(ImageSearchRegion.ValueOf("cn-north-4")) .WithHttpConfig(config)
.Build();
var req = new RunAddPictureRequest {
InstanceName = "instance-name"
};
req.Body = new AddPictureRequestReq() {
Path = "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
};
try {
var resp = client.RunAddPicture(req);
var respStatusCode = resp.HttpStatusCode;
Console.WriteLine(respStatusCode);
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
Console.WriteLine(clientRequestException.ErrorMsg);
}
catch (ConnectionException connectionException) {
Console.WriteLine(connectionException.ErrorMessage);
} } } }
2. 执行示例代码文件,控制台输出添加图片结果即表示程序执行成功。添加图片结
果输出到控制台。
200 {
"result": "Success."
}
7.5 搜索图片示例
本章节对搜索图片AK/SK方式使用SDK进行示例说明。
搜索图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用 SearchPictureReq类的Path配置图像URL,配置完成后运行即可。
1. 添加图片调用示例代码如下:
using System;
using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.ImageSearch;
using HuaweiCloud.SDK.ImageSearch.V1;
using HuaweiCloud.SDK.ImageSearch.V1.Model;
using Newtonsoft.Json;
namespace RunSearchPictureSolution { class Program
{
static void Main(string[] args) {
const string ak = "<YOUR AK>";
const string sk = "<YOUR SK>";
var config = HttpConfig.GetDefaultConfig();
config.IgnoreSslVerification = true;
var auth = new BasicCredentials(ak, sk);
var client = ImageSearchClient.NewBuilder() .WithCredential(auth)
.WithRegion(ImageSearchRegion.ValueOf("cn-north-4")) .WithHttpConfig(config)
.Build();
var req = new RunSearchPictureRequest {
InstanceName = "instance-name"
};
req.Body = new SearchPictureReq() {
Path = "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
};
try {
var resp = client.RunSearchPicture(req);
var respStatusCode = resp.HttpStatusCode;
Console.WriteLine(respStatusCode);
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
Console.WriteLine(clientRequestException.ErrorMsg);
}
catch (ConnectionException connectionException) {
Console.WriteLine(connectionException.ErrorMessage);
} } } }
2. 执行示例代码文件,控制台输出搜索图片结果即表示程序执行成功。搜索图片结
果输出到控制台。
200 { "result": [ {
"path": "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg", "sim": 1.0,
"tags": {}
} ], "count":1 }
7.6 查询图片示例
本章节对查询图片AK/SK方式使用SDK进行示例说明。
查询图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用 DeletePictureReq类的Path配置图片的URL,配置完成后运行即可。
1. 查询图片调用示例代码如下:
using System;
using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.ImageSearch;
using HuaweiCloud.SDK.ImageSearch.V1;
using HuaweiCloud.SDK.ImageSearch.V1.Model;
using Newtonsoft.Json;
namespace RunCheckPictureSolution { class Program
{
static void Main(string[] args) {
const string ak = "<YOUR AK>";
const string sk = "<YOUR SK>";
var config = HttpConfig.GetDefaultConfig();
config.IgnoreSslVerification = true;
var auth = new BasicCredentials(ak, sk);
var client = ImageSearchClient.NewBuilder() .WithCredential(auth)
.WithRegion(ImageSearchRegion.ValueOf("cn-north-4")) .WithHttpConfig(config)
.Build();
var req = new RunCheckPictureRequest {
InstanceName = "instance-name"
};
req.Body = new DeletePictureReq() {
Path = "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
};
try {
var resp = client.RunCheckPicture(req);
var respStatusCode = resp.HttpStatusCode;
Console.WriteLine(respStatusCode);
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
Console.WriteLine(clientRequestException.ErrorMsg);
}
catch (ConnectionException connectionException) {
Console.WriteLine(connectionException.ErrorMessage);
} } } }
2. 执行示例代码文件,控制台输出查询图片结果即表示程序执行成功。查询图片结
果输出到控制台。
200 {
"exist": "true"
}
7.7 删除图片示例
本章节对删除图片AK/SK方式使用SDK进行示例说明。
删除图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用 DeletePictureReq类的Path配置图片的URL,配置完成后运行即可。
1. 删除图片调用示例代码如下:
using System;
using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.ImageSearch;
using HuaweiCloud.SDK.ImageSearch.V1;
using HuaweiCloud.SDK.ImageSearch.V1.Model;
using Newtonsoft.Json;
namespace RunDeletePictureSolution { class Program
{
static void Main(string[] args) {
const string ak = "<YOUR AK>";
const string sk = "<YOUR SK>";
var config = HttpConfig.GetDefaultConfig();
config.IgnoreSslVerification = true;
var auth = new BasicCredentials(ak, sk);
var client = ImageSearchClient.NewBuilder() .WithCredential(auth)
.WithRegion(ImageSearchRegion.ValueOf("cn-north-4")) .WithHttpConfig(config)
.Build();
var req = new RunDeletePictureRequest {
InstanceName = "instance-name"
};
req.Body = new DeletePictureReq() {
Path = "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
};
try {
var resp = client.RunDeletePicture(req);
var respStatusCode = resp.HttpStatusCode;
Console.WriteLine(respStatusCode);
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
Console.WriteLine(clientRequestException.ErrorMsg);
}
catch (ConnectionException connectionException) {
Console.WriteLine(connectionException.ErrorMessage);
} } } }
2. 执行示例代码文件,控制台输出删除图片结果即表示程序执行成功。删除图片结
果输出到控制台。
200 {
"result": "Success."
}
7.8 修改图片信息示例
本章节对修改图片信息AK/SK方式使用SDK进行示例说明。
修改图片信息示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用
RunModifyPictureReq类的Path和Tags配置图片的URL和标签,配置完成后运行即可。
1. 修改图片信息调用示例代码如下:
using System;
using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.ImageSearch;
using HuaweiCloud.SDK.ImageSearch.V1;
using HuaweiCloud.SDK.ImageSearch.V1.Model;
using Newtonsoft.Json;
namespace RunModifyPictureSolution { class Program
{
static void Main(string[] args) {
const string ak = "<YOUR AK>";
const string sk = "<YOUR SK>";
var config = HttpConfig.GetDefaultConfig();
config.IgnoreSslVerification = true;
var auth = new BasicCredentials(ak, sk);
var client = ImageSearchClient.NewBuilder() .WithCredential(auth)
.WithRegion(ImageSearchRegion.ValueOf("cn-north-4")) .WithHttpConfig(config)
.Build();
var req = new RunModifyPictureRequest {
InstanceName = "instance-name"
};
req.Body = new RunModifyPictureReq() {
Path = https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg, Tags = new Dictionary<string, string>{{"animal", "dog"}}
};
try {
var resp = client.RunModifyPicture(req);
var respStatusCode = resp.HttpStatusCode;
Console.WriteLine(respStatusCode);
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
Console.WriteLine(clientRequestException.ErrorMsg);
}
catch (ConnectionException connectionException) {
Console.WriteLine(connectionException.ErrorMessage);
} } } }
2. 执行示例代码文件,控制台输出删除实例结果即表示程序执行成功。修改图片信
息结果输出到控制台。
200 {
"result": "Success."
}
7.9 删除实例示例
本章节对删除实例AK/SK方式使用SDK进行示例说明。
删除实例示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用
RunDeleteInstanceRequest类的InstanceName配置实例名称,配置完成后运行即可。
1. 删除实例调用示例代码如下:
using System;
using System;
using System.Collections.Generic;
using HuaweiCloud.SDK.Core;
using HuaweiCloud.SDK.Core.Auth;
using HuaweiCloud.SDK.ImageSearch;
using HuaweiCloud.SDK.ImageSearch.V1;
using HuaweiCloud.SDK.ImageSearch.V1.Model;
using Newtonsoft.Json;
namespace RunDeleteInstanceSolution { class Program