您现在的位置是:首页 > 文章详情

记一次使用阿里云图像搜索功能

日期:2019-03-17点击:415

记一次使用阿里云图像搜索功能

阿里云提供的图像搜索业务:

业务介绍地址:

https://ai.aliyun.com/imagesearch?spm=5176.10695662.1280361.130.11a61e6d5n1K8L

image_20190318195139873


提供两种服务:
  • 商品图像搜索;
  • 通用图像搜索;

开发文档

文档地址:

https://help.aliyun.com/document_detail/66616.html

目前在该页支持的sdk有:

  • Java
  • Nodejs
  • PHP

从文档来看使用起来十分复杂,想用python实现的话,借鉴这份资料真是费了很大力气,一直没有跑通,提示构建的签名与它计算的签名不匹配。

后面找寻python sdk。

python sdk使用指南

https://help.aliyun.com/document_detail/67117.html

按照这个介绍,自己安装了核心库,并歪打正着的通过pip安装找到了该服务的隐藏python sdk!

pip install aliyun-python-sdk-imagesearch

然后去aliyun的gihub页面找相关的包介绍:

https://github.com/aliyun/aliyun-openapi-python-sdk/tree/master/aliyun-python-sdk-imagesearch

介绍不是很详细,也花了一天的时间试错。

image_20190318195820335

最终调通,该sdk对python3目前也不支持,卡在字符串编码这块,后续应该会解决这个问题。

使用该sdk,更改下包的源码(base64编码的字符串问题),在python3下跑通整个过程,上传图片,删除图片以及搜索图片。

Add_item

#!/usr/bin/env python # fileUsing: test ali api 's add item from aliyunsdkcore.client import AcsClient from aliyunsdkimagesearch.request.v20180120.AddItemRequest import AddItemRequest import sys # set parameters accessKeyId = 'xxx' accessKeySecret = 'xxx' instanceName = 'xxx' regionId = 'cn-shanghai' domain = 'imagesearch.cn-shanghai.aliyuncs.com' client = AcsClient(accessKeyId, accessKeySecret, regionId) add_req = AddItemRequest() def add_img(img_path): """ add img """ pid = img_path.split('/')[-1].split('_')[0] item_id = pid cust_content = pid cate_id = '0' pic_name = pid pic_content = open(img_path, 'rb').read() add_req.set_instance_name(instanceName) add_req.set_item_id(item_id) add_req.set_cate_id(cate_id) add_req.set_cust_content(cust_content) add_req.add_picture(pic_name, pic_content) process_flag = add_req.build_post_content() response = client.do_action_with_exception(add_req) print(response.decode('utf8')) # test img_path = sys.argv[1] add_img(img_path)

Del_item

#!/usr/bin/env python # fileUsing: test ali api 's del item from aliyunsdkcore.client import AcsClient from aliyunsdkimagesearch.request.v20180120.DeleteItemRequest import DeleteItemRequest import sys # set parameters accessKeyId = 'xxx' accessKeySecret = 'xxxx' instanceName = 'xxxxx' regionId = 'cn-shanghai' domain = 'imagesearch.cn-shanghai.aliyuncs.com' client = AcsClient(accessKeyId, accessKeySecret, regionId) del_req = DeleteItemRequest() def del_img(img_path): """ del img """ pid = img_path.split('/')[-1].split('_')[0] item_id = pid pic_name = pid del_req.set_instance_name(instanceName) del_req.set_item_id(item_id) del_req.add_picture(pic_name) process_flag = del_req.build_post_content() response = client.do_action_with_exception(del_req) print(response.decode('utf8')) # test img_path = sys.argv[1] del_img(img_path)

Search_item

#!/usr/bin/env python # fileUsing: test ali api 's search item from aliyunsdkcore.client import AcsClient from aliyunsdkimagesearch.request.v20180120.SearchItemRequest import SearchItemRequest import sys # set parameters accessKeyId = 'xxx' accessKeySecret = 'xxxx' instanceName = 'xxxx' regionId = 'cn-shanghai' domain = 'imagesearch.cn-shanghai.aliyuncs.com' client = AcsClient(accessKeyId, accessKeySecret, regionId) search_req = SearchItemRequest() def search_img(img_path): """ search img """ pid = img_path.split('/')[-1].split('_')[0] pic_content = open(img_path, 'rb').read() search_req.set_instance_name(instanceName) search_req.set_num(100) # return nums of search search_req.set_search_picture(pic_content) process_flag = search_req.build_post_content() response = client.do_action_with_exception(search_req) print(response.decode('utf8')) # test img_path = sys.argv[1] search_img(img_path)

源码更改地方

encoded_pic_name = str(base64.b64encode(bytes(pic_name, 'utf-8')), encoding='utf-8') encoded_pic_content = base64.b64encode(pic_content).decode('utf8')
原文链接:https://yq.aliyun.com/articles/694180
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章