记一次使用阿里云图像搜索功能
记一次使用阿里云图像搜索功能
阿里云提供的图像搜索业务:
业务介绍地址:
https://ai.aliyun.com/imagesearch?spm=5176.10695662.1280361.130.11a61e6d5n1K8L
提供两种服务:
- 商品图像搜索;
- 通用图像搜索;
开发文档
文档地址:
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
介绍不是很详细,也花了一天的时间试错。
最终调通,该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')

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Nginx+kodexplorer(可道云部署纪要)
Nginx+kodexplorer(可道云部署纪要) 安装nginx yum install yum-utils To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents: [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/ke...
-
下一篇
Spring Cloud服务发现/注册
服务发现/注册 服务发现(Service Discovery) 在计算机网络中,一种自发现设备或者服务的技术,通过服务发现协议(Service Discovery Protocol)实现。 常见协议 java:jini(Apache River) REST:HATEOAS Web Services:UDDI(Universal Description Discovery and lntegration) 服务注册(Service Registration) 在计算机网络中,为了更好地治理多个设备或者服务,这些设备或者服务主动或者被动注册到管理中心,以便服务被发现和消费。 常见注册中心 Apache Zookeper 性能最差 Netflix Eureka 高可用,高 一致性差 Consul 高可用,高一致性(某些方面) 高可用架构 高可用(High Availability) 一种系统特性,致力于确保可接受程度的执行操作,通常采用上线时间作为基准。其中,以一年内的上线时间与自然时间的比率来描述可用性。 基本原则 消灭单点故障 可靠性交迭 故障探测 Spring Cloud Netfli...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- MySQL数据库在高并发下的优化方案
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Docker容器配置,解决镜像无法拉取问题
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker快速安装Oracle11G,搭建oracle11g学习环境