Remember to use Alibaba cloud image search function once

Remember to use Alibaba cloud image search function once

The image search service provided by alicloud:

Business address:

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



There are two services:
  • Product image search;
  • General image search;

Developing documents

Document address:

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

Currently, the SDKs supported on this page are:

  • Java
  • Nodejs
  • PHP

From the perspective of documents, it is very complex to use. If you want to use python for implementation, it takes a lot of effort to learn from this information, and you have not been able to run through it all the time. It indicates that the constructed signature does not match the signature it calculates.

Look for the python sdk later.

python sdk Guide

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

According to this introduction, I installed the core library and found the hidden python sdk of the service through the pip installation!

pip install aliyun-python-sdk-imagesearch

Then go to the gihub page of aliyun to find the relevant package introduction:

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

The presentation wasn't very detailed, and it took a day to try things out.


Finally, the sdk does not support Python 3 at present. It is stuck in string encoding. This problem should be solved later.

Using the sdk, change the source code of the next package (base64 encoded string problem), run through the whole process in Python 3, upload pictures, delete pictures and search pictures.

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)

Source code change place

encoded_pic_name = str(base64.b64encode(bytes(pic_name, 'utf-8')), encoding='utf-8')
encoded_pic_content = base64.b64encode(pic_content).decode('utf8')

Tags: Python SDK pip encoding

Posted on Tue, 03 Dec 2019 03:19:35 -0500 by rthconsultants