記一次使用阿裡雲圖像搜尋功能
阿裡雲提供的圖像搜尋業務:
業務介紹位址:
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')