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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

uncle_ll 2019-03-18 20:12:40 浏览756
展开阅读全文

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

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

业务介绍地址:

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')

网友评论

登录后评论
0/500
评论
uncle_ll
+ 关注