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)
{"domain": "通用图片搜索", "desc": "", "registerDate": 1637548249353, "expiredDate": -1, "level":
30000000, "tags": ["animal", "plant"], "status": "NORMAL", "instanceName": "instance-name"}
Process finished with exit code 0
6.4 添加图片示例
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)
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)
"exist": "true"
}
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)
"result": "success."
}
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
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
{ "instanceName": "instance-name", "level": 30000000,
"expiredDate": -1, "domain": "通用图片搜索", "desc": "",
"registerDate": 1638939677331, "tags"[
using HuaweiCloud.SDK.ImageSearch.V1.Model;
Path = "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
};
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
"result": "Success."
}
7.5 搜索图片示例
本章节对搜索图片AK/SK方式使用SDK进行示例说明。
搜索图片示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用
Path = "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
};
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
200 { "result": [ {
"path": "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg", "sim": 1.0,
Path = "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
};
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
"exist": "true"
}
Path = "https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg"
};
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
"result": "Success."
}
.WithRegion(ImageSearchRegion.ValueOf("cn-north-4"))
Path = https://bucketname.obs.cn-north-4.myhuaweicloud.com/imagesearch/test.jpg, Tags = new Dictionary<string, string>{{"animal", "dog"}}
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
"result": "Success."
}
{
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
"result": "Success."
}
using HuaweiCloud.SDK.ImageSearch.V1;
Console.WriteLine(JsonConvert.DeserializeObject(resp.HttpBody));
}
catch (RequestTimeoutException requestTimeoutException) {
Console.WriteLine(requestTimeoutException.ErrorMessage);
}
catch (ServiceResponseException clientRequestException) {
Console.WriteLine(clientRequestException.HttpStatusCode);
Console.WriteLine(clientRequestException.ErrorCode);
"instanceName": "instance-name", "level": 30000000,
"expiredDate": -1, "domain": "通用图片搜索",
"domainEn": "General image search", "desc": "",
"registerDate": 1638235929989, "tags":[
"animal",
"plant"
],
"status": "NORMAL"
}