写点什么

【案例教学】华为云 API 图像搜索 ImageSearch 的快捷性—AI 帮助您快速归类图片

  • 2023-09-14
    广东
  • 本文字数:11351 字

    阅读完需:约 37 分钟

云服务、API、SDK,调试,查看,我都行


阅读短文您可以学习到:人工智能 AI 同类型的相片合并归类

1 IntelliJ IDEA 之 API 插件介绍

API 插件支持 VS Code IDE、IntelliJ IDEA 等平台、以及华为云自研 CodeArts IDE,基于华为云服务提供的能力,帮助开发者更高效、便捷的搭建应用。API 插件关联华为云服务下的 API Explorer、DevStar、CodeLabs、SDK 中心和 CLI 中心产品,致力于为开发者提供更稳定、快速、安全的编程体验。


在本插件中,我们提供了但不局限于如下的功能:


对接华为云 API 开放平台,支持用户检索 API、查看 API 文档、调试 API、以及提供 SDK 示例代码供用户学习如何使用 API。


提供华为云 SDK 代码片段补全功能,SDK 依赖包自动引入,加速用户集成华为云 API。


对接华为云开发体验馆 Codelabs,提供 500+云服务代码示例,向导式教程帮助用户快速学习。


说明:


在 IntelliJ IDEA 等系列平台和 VS Code IDE,华为云 API 插件的名称是 Huawei Cloud API。而在 CodeArts IDE,API 插件是 IDE 原生内置的,名称是华为云 API 开发套件。


API 插件在 IntelliJ IDEA 等系列平台和 VS Code IDE 的使用依赖底座插件,请提前安装底座插件。

2  API 插件安装--IntelliJ IDEA

2.1 IntelliJ IDEA 等平台

安装准备: 下载并安装 JDK1.8 或更高版本。下载并安装 IntelliJ IDEA 2020.2 或更高版本。


须知: IntellIj 平台同时支撑包括 Goland、Pycharm 等在内的 IDE,若在其它相关 IDE 上开发,请下载配置好对应语言的编译器或者解释器。这里以 IDEA 为例介绍 IntelliJ 平台插件的安装流程,其他 IntelliJ 系列的 IDE 请参考 IDEA。https://developer.huaweicloud.com/develop/toolkit.html


开始安装:


您可以在直接在 IDE 插件市场或者直接在 JetBrains 插件市场下载离线包安装。


IDE 安装


  1. 在 IntelliJ IDEA 顶部菜单栏中选择 File > Settings,在 Settings 对话框的左侧导航栏中单击 Plugins。

  2. Plugins 区域单击 Marketplace,在搜索栏中输入 Huawei Cloud API。

  3. Search Results 区域会出现 Huawei Cloud API,单击 Install,完成后重启 IDE。



离线包安装:


  1. 进入插件市场搜索 Huawei Cloud API,进入插件详情页,在 Versions 页签下选择想要版本的 API 插件,点击 Download 下载离线的插件压缩包保存到本地。。

  2. 在 IntelliJ IDEA 顶部菜单栏中选择 File > Settings,在 Settings 对话框的左侧导航栏中单击 Plugins。

  3. 在 Plugins 区域单击 ,再单击 Install Plugin from Disk...。

  4. 在 Choose Plugin File 对话框中选择离线安装包(不用解压),并按照 IntelliJ IDEA 安装页面的提示,完成后续安装步骤。



说明: 若当前您想要安装插件的 IntelliJ IDE 已经在桌面打开,则进入插件市场搜索 Huawei Cloud API,进入插件详情页,在右上角会识别到本地已经打开的 IDE,点击相应按钮,在弹出的 IDE 窗口中点击 ok,则 IDE 后台会开始安装相应版本的 API 插件。


安装验证: 在 IntelliJ 系列平台上安装插件成功后在左侧的导航栏中可以看到 Huawei Cloud Toolkit 图标,点击后面板会出现 Huawei Cloud API 的字样,则说明安装成功。

2.2 API 列表


左侧展示 API 列表,可以查询所有 API,目前云服务 206,APIs9213


https://developer.huaweicloud.com/develop/toolkit.html


版本说明


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


功能介绍


创建实例,实例中会生成图片索引库,用来存放图片特征。


该示例展示了如何通过 java 版 SDK 实现创建实例。


前置条件


获取 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.0.67 版本的内容审核 SDK 为例:


<dependency>


<groupId>com.huaweicloud.sdk</groupId>


<artifactId>huaweicloud-sdk-imagesearch</artifactId>


<version>3.0.67</version>


</dependency>

3 创建图像搜索实例

图像内容审核示例代码只需将 AK/SK 信息替换为实际 AK/SK,代码中可以使用 CreateInstanceReq 类的 setName 或 setModel 方法配置实例名称和模型名称,配置完成后运行即可。


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;
public class ImageSearchCreateInstanceDemo { public static void main(String[] args) { String ak = "<YOUR AK>"; String sk = "<YOUR SK>"; ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth) .withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build(); RunCreateInstanceRequest request = new RunCreateInstanceRequest(); CreateInstanceReq body = new CreateInstanceReq(); body.setName("instance-name"); body.setModel("common-search"); request.setBody(body); try { RunCreateInstanceResponse response = client.runCreateInstance(request); System.out.println(response.toString()); } 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()); } }}
复制代码

4 删除图像搜索实例

删除实例示例代码只需将 AK/SK 信息替换为实际 AK/SK,代码中可以使用 RunDeleteInstanceRequest 类的 setInstanceName 方法配置实例名称,配置完成后运行即可。


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 ImageSearchDeleteInstanceDemo { public static void main(String[] args) { String ak = "<YOUR AK>"; String sk = "<YOUR SK>"; ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); ImageSearchClient client = ImageSearchClient.newBuilder() .withCredential(auth) .withRegion(ImageSearchRegion.valueOf("cn-north-4")) .build(); RunDeleteInstanceRequest request = new RunDeleteInstanceRequest(); request.setInstanceName("instance-name"); try { RunDeleteInstanceResponse response = client.runDeleteInstance(request); System.out.println(response.toString()); } 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()); } }}
复制代码

5 相册中筛选同类型的相片合并归类

使用如下代码(来自 ImageSearchService.py)同步查询特定 Region 下的图像搜索服务(IS),调用前请根据实际情况替换如下变量 {your ak string}、{your sk string}、{your region string}、{your endpoint string} 、{your projectId string} 以及 {your file path string}


class ImageSearchService(object):    def __init__(self, service_name, region, ak, sk, end_point_url, project_id):        self.end_point_url = end_point_url        self.project_id = project_id        self.sig = signer.Signer()        self.sig.Key = ak        self.sig.Secret = sk        self.name = service_name        self.region = region
def create_instance(self, project_id, model, name, description="", tags=["test-tags"]): uri = "https://imagesearch.cn-north-4.myhuaweicloud.com/v1/%s/service"%project_id data = {"project_id": project_id, "model": model, "name": name, "description": description, "tags": tags } json_data = json.dumps(data) r = signer.HttpRequest("POST", uri) r.body = json_data r.headers = {"content-type": "application/json"} self.sig.Sign(r) resp = requests.request(r.method, r.scheme+"://"+r.host+r.uri, headers=r.headers, data=r.body) # print(resp.status_code, resp.reason) # print(resp.content) return resp
def delete_instance(self, project_id, instance_name): uri = "https://imagesearch.cn-north-4.myhuaweicloud.com/v1/%s/service/%s"%(project_id, instance_name) data = {} json_data = json.dumps(data) r = signer.HttpRequest("DELETE", uri) r.body = json_data r.headers = {"content-type": "application/json"} self.sig.Sign(r) resp = requests.request(r.method, r.scheme+"://"+r.host+r.uri, headers=r.headers, data=r.body) # print(resp.status_code, resp.reason) # print(resp.content) return resp def check_user_instance(self, end_point_url, project_id, instance_name): uri = "%s/v1/%s/service/%s"%(end_point_url, project_id, instance_name) print(uri) data = {} json_data = json.dumps(data) r = signer.HttpRequest("GET", uri) r.body = json_data r.headers = {"content-type": "application/json"} self.sig.Sign(r) resp = requests.request(r.method, r.scheme+"://"+r.host+r.uri, headers=r.headers, data=r.body) # print(resp.status_code, resp.reason) # print(resp.content) return resp
def add_picture(self, project_id, instance_name, path, file="", is_crop=False, tags={"test-tags": "test-default"}): print("call add_picture: %s, length of file path %d"%(path, len(path))) uri = "https://imagesearch.cn-north-4.myhuaweicloud.com/v1/%s/%s/image"%(project_id, instance_name) # print(uri) data = {} data["path"] = path data["file"] = file data["tags"] = tags json_data = json.dumps(data) r = signer.HttpRequest("POST", uri) r.body = json_data r.headers = {"content-type": "application/json"} self.sig.Sign(r) resp = requests.request(r.method, r.scheme+"://"+r.host+r.uri, headers=r.headers, data=r.body) # print(resp.status_code, resp.reason) # print(resp.content) return resp
def search_picture(self, project_id, instance_name, path="", box={"height": 0, "width": 0, "x": 0, "y": 0}, file="", is_crop=False, limit=0, offset=0): uri = "https://imagesearch.cn-north-4.myhuaweicloud.com/v1/%s/%s/image/search"%(project_id, instance_name) data = {} data["path"] = path data["file"] = file json_data = json.dumps(data) r = signer.HttpRequest("POST", uri) r.body = json_data r.headers = {"content-type": "application/json"} self.sig.Sign(r) resp = requests.request(r.method, r.scheme+"://"+r.host+r.uri, headers=r.headers, data=r.body) # print(resp.status_code, resp.reason) # print(resp.content) return resp
def delete_picture(self, project_id, instance_name, path): uri = "https://imagesearch.cn-north-4.myhuaweicloud.com/v1/%s/%s/image"%(project_id, instance_name) data = {} data["path"] = path json_data = json.dumps(data) r = signer.HttpRequest("DELETE", uri) r.body = json_data r.headers = {"content-type": "application/json"} self.sig.Sign(r) resp = requests.request(r.method, r.scheme+"://"+r.host+r.uri, headers=r.headers, data=r.body) # print(resp.status_code, resp.reason) # print(resp.content) return resp
def check_picture(self, project_id, instance_name, path): uri = "https://imagesearch.cn-north-4.myhuaweicloud.com/v1/%s/%s/image/check"%(project_id, instance_name) data = {} data["path"] = path json_data = json.dumps(data) r = signer.HttpRequest("POST", uri) r.body = json_data r.headers = {"content-type": "application/json"} self.sig.Sign(r) resp = requests.request(r.method, r.scheme+"://"+r.host+r.uri, headers=r.headers, data=r.body) # print(resp.status_code, resp.reason) # print(resp.content) return resp
def update_picture(self, project_id, instance_name, path, tags={}): uri = "https://imagesearch.cn-north-4.myhuaweicloud.com/v1/%s/%s/image"%(project_id, instance_name) data = {} data["path"] = path data["tags"] = tags json_data = json.dumps(data) r = signer.HttpRequest("PUT", uri) r.body = json_data r.headers = {"content-type": "application/json"} self.sig.Sign(r) resp = requests.request(r.method, r.scheme+"://"+r.host+r.uri, headers=r.headers, data=r.body) # print(resp.status_code, resp.reason) # print(resp.content) return resp
复制代码


运行示例


执行 bash config.bash,脚本会自动 python 环境相关的依赖,然后根据系统提示输入 AK,SK,Project ID。


配置成功后会自动生成 cfg.ini,内容类似,其中 hwc 部分的配置和配置的结果保持一致。当然,你也可以手动配置 cfg.ini 文件。


[hwc]    ak=******    sk=******    project_id=63kkdjkd******9dkst3tskdgi53    [endpoint]    cn-north-1=https://imagesearch.cn-north-1.myhuaweicloud.com    cn-north-4=https://imagesearch.cn-north-4.myhuaweicloud.com    ap-southeast-1=https://imagesearch.ap-southeast-1.myhuaweicloud.com
复制代码


输入 python3 unit_test.py 执行单元测试


输入 python3 run.py 执行演示程序


运行结果


单元测试输出


create_instance . check_user_instance is_788802https://imagesearch.cn-north-4.myhuaweicloud.com/v1/your_project_id/service/is_788802<Response [200]>. add_picture_by_local is_788802call add_picture: ./test_picture/43474673_7bb4465a86.jpg, length of file path 38. add_picture_by_url is_788802call add_picture: https://is-smirk.obs.cn-north-4.myhuaweicloud.com:443/flowers_recognition/train/11405573_24a8a838cc_n.jpg, length of file path 105. search_picture_by_local is_788802. search_picture_by_url is_788802. update_picture is_788802. check_picture is_788802. delete_picture is_788802. delete_instance is_788802.
复制代码


Ran 10 tests in 18.108s


OK


演示程序输出


['hwc', 'endpoint'][('cn-north-1', 'https://imagesearch.cn-north-1.myhuaweicloud.com'), ('cn-north-4', 'https://imagesearch.cn-north-4.myhuaweicloud.com'), ('ap-southeast-1', 'https://imagesearch.ap-southeast-1.myhuaweicloud.com')]https://imagesearch.cn-north-1.myhuaweicloud.comyour_AKhttps://imagesearch.cn-north-4.myhuaweicloud.com/v1/your_project_id/service/hwc_democall add_picture: 151385301_153eacf6b5_n.jpg, length of file path 26add picture success: ./test_picture/train/151385301_153eacf6b5_n.jpgcall add_picture: 200557977_bf24d9550b.jpg, length of file path 24add picture success: ./test_picture/train/200557977_bf24d9550b.jpgcall add_picture: 6953297_8576bf4ea3.jpg, length of file path 22add picture success: ./test_picture/train/6953297_8576bf4ea3.jpgcall add_picture: 134409839_71069a95d1_m.jpg, length of file path 26add picture success: ./test_picture/train/134409839_71069a95d1_m.jpgcall add_picture: 510874382_f7e3435043.jpg, length of file path 24add picture success: ./test_picture/train/510874382_f7e3435043.jpgcall add_picture: 15987457_49dc11bf4b.jpg, length of file path 23add picture success: ./test_picture/train/15987457_49dc11bf4b.jpgcall add_picture: 164670176_9f5b9c7965.jpg, length of file path 24add picture success: ./test_picture/train/164670176_9f5b9c7965.jpgcall add_picture: 2059172936_032ffc12aa.jpg, length of file path 25add picture success: ./test_picture/train/2059172936_032ffc12aa.jpgcall add_picture: 2093263381_afd51358a3.jpg, length of file path 25add picture success: ./test_picture/train/2093263381_afd51358a3.jpgcall add_picture: 3203779656_3580151ea4_m.jpg, length of file path 27add picture success: ./test_picture/train/3203779656_3580151ea4_m.jpgcall add_picture: 13920113_f03e867ea7_m.jpg, length of file path 25add picture success: ./test_picture/train/13920113_f03e867ea7_m.jpgcall add_picture: 5794835_d15905c7c8_n.jpg, length of file path 24add picture success: ./test_picture/train/5794835_d15905c7c8_n.jpgcall add_picture: 146023167_f905574d97_m.jpg, length of file path 26add picture success: ./test_picture/train/146023167_f905574d97_m.jpgcall add_picture: 486234138_688e0_n.jpg, length of file path 21add failedcall add_picture: 486234138_688e0_n.jpg, length of file path 21call add_picture: 184682652_c927a49226_m.jpg, length of file path 26add picture success: ./test_picture/train/184682652_c927a49226_m.jpgcall add_picture: 40410814_fba3837226_n.jpg, length of file path 25add picture success: ./test_picture/train/40410814_fba3837226_n.jpgcall add_picture: 80846315_d997645bea_n.jpg, length of file path 25add picture success: ./test_picture/train/80846315_d997645bea_n.jpgcall add_picture: 413815348_764ae83088.jpg, length of file path 24add picture success: ./test_picture/train/413815348_764ae83088.jpgcall add_picture: 200557979_a16112aac1_n.jpg, length of file path 26add picture success: ./test_picture/train/200557979_a16112aac1_n.jpgcall add_picture: 163978992_8128b49d3e_n.jpg, length of file path 26add picture success: ./test_picture/train/163978992_8128b49d3e_n.jpgcall add_picture: 43474673_7bb4465a86.jpg, length of file path 23add picture success: ./test_picture/train/43474673_7bb4465a86.jpgcall add_picture: 21652746_cc379e0eea_m.jpg, length of file path 25add picture success: ./test_picture/train/21652746_cc379e0eea_m.jpgcall add_picture: 118974357_0faa23cce9_n.jpg, length of file path 26add failedcall add_picture: 118974357_0faa23cce9_n.jpg, length of file path 26call add_picture: 154332674_453cea64f4.jpg, length of file path 24add picture success: ./test_picture/train/154332674_453cea64f4.jpgcall add_picture: 162362896_99c7d851c8_n.jpg, length of file path 26add picture success: ./test_picture/train/162362896_99c7d851c8_n.jpgcall add_picture: 160456948_38c3817c6a_m.jpg, length of file path 26add picture success: ./test_picture/train/160456948_38c3817c6a_m.jpgcall add_picture: 5794839_200acd910c_n.jpg, length of file path 24add picture success: ./test_picture/train/5794839_200acd910c_n.jpgcall add_picture: 3179751458_9646d839f6_n.jpg, length of file path 27add picture success: ./test_picture/train/3179751458_9646d839f6_n.jpgcall add_picture: 2183357362_4b4da4b6b5.jpg, length of file path 25add picture success: ./test_picture/train/2183357362_4b4da4b6b5.jpgcall add_picture: 141652526_2be95f21c3_n.jpg, length of file path 26add failedcall add_picture: 141652526_2be95f21c3_n.jpg, length of file path 26call add_picture: 169371301_d9b91a2a42.jpg, length of file path 24add picture success: ./test_picture/train/169371301_d9b91a2a42.jpgcall add_picture: 200288046_0032f322ff_n.jpg, length of file path 26add picture success: ./test_picture/train/200288046_0032f322ff_n.jpgcall add_picture: 3450344423_63ba3190e3.jpg, length of file path 25add failedcall add_picture: 3450344423_63ba3190e3.jpg, length of file path 25call add_picture: 11405573_24a8a838cc_n.jpg, length of file path 25add picture success: ./test_picture/train/11405573_24a8a838cc_n.jpgcall add_picture: 2141413229_3f0425f972_n.jpg, length of file path 27add picture success: ./test_picture/train/2141413229_3f0425f972_n.jpgcall add_picture: 3554620445_082dd0bec4_n.jpg, length of file path 27add failedcall add_picture: 3554620445_082dd0bec4_n.jpg, length of file path 27call add_picture: 44079668_34dfee3da1_n.jpg, length of file path 25add picture success: ./test_picture/train/44079668_34dfee3da1_n.jpgcall add_picture: 40411019_526f3fc8d9_m.jpg, length of file path 25add picture success: ./test_picture/train/40411019_526f3fc8d9_m.jpgcall add_picture: 2609353769_dc3654f12f.jpg, length of file path 25add picture success: ./test_picture/train/2609353769_dc3654f12f.jpgcall add_picture: 40410963_3ac280f23a_n.jpg, length of file path 25add picture success: ./test_picture/train/40410963_3ac280f23a_n.jpg[('40410814_fba3837226_n.jpg', 0.8202), ('40411019_526f3fc8d9_m.jpg', 0.7959), ('44079668_34dfee3da1_n.jpg', 0.7957), ('40410963_3ac280f23a_n.jpg', 0.7948), ('200288046_0032f322ff_n.jpg', 0.7419), ('184682652_c927a49226_m.jpg', 0.7291), ('5794839_200acd910c_n.jpg', 0.6738), ('163978992_8128b49d3e_n.jpg', 0.6732), ('200557977_bf24d9550b.jpg', 0.6675), ('134409839_71069a95d1_m.jpg', 0.6608)][('162362896_99c7d851c8_n.jpg', 0.9541), ('163978992_8128b49d3e_n.jpg', 0.9534), ('21652746_cc379e0eea_m.jpg', 0.93), ('413815348_764ae83088.jpg', 0.8996), ('5794835_d15905c7c8_n.jpg', 0.8991), ('5794839_200acd910c_n.jpg', 0.8933), ('134409839_71069a95d1_m.jpg', 0.8806), ('169371301_d9b91a2a42.jpg', 0.8272), ('43474673_7bb4465a86.jpg', 0.78), ('44079668_34dfee3da1_n.jpg', 0.7677)][('15987457_49dc11bf4b.jpg', 0.7605), ('151385301_153eacf6b5_n.jpg', 0.7571), ('80846315_d997645bea_n.jpg', 0.7416), ('11405573_24a8a838cc_n.jpg', 0.6828), ('146023167_f905574d97_m.jpg', 0.6674), ('13920113_f03e867ea7_m.jpg', 0.6639), ('160456948_38c3817c6a_m.jpg', 0.6396), ('510874382_f7e3435043.jpg', 0.6251)][('2141413229_3f0425f972_n.jpg', 1.0), ('2093263381_afd51358a3.jpg', 0.6334)]
复制代码


演示程序会在华北-北京 4 自动创建名字为 hwc_id_demo 的图像搜索服务实例,将 test_picture/train 下的图片添加到实例中。


然后测试 test_picture/test 下图片作为搜索图片的搜索结果,在该 Demo 中过滤了相似度小于 0.6 的返回结果。

6 体验插件的魅力

华为云 devkit 已上线: https://developer.huaweicloud.com/develop/toolkit.html

用户头像

公众号:华为云PaaS服务,有见面礼等你哦! 2022-09-26 加入

还未添加个人简介

评论

发布
暂无评论
【案例教学】华为云API图像搜索ImageSearch的快捷性—AI帮助您快速归类图片_云计算_华为云PaaS服务小智_InfoQ写作社区