Preparations
Before installing and using Alibaba Cloud SDKs, make sure that you have registered an Alibaba Cloud account and generated an AccessKey. For more information, see Create an AccessKey.
Install the Python SDK.
(Recommended) Install the SDK by using the dependency tool.
To install the Alibaba Cloud Python SDK, run the following command:pip install aliyun-python-sdk-core
Download and install the SDK on your own.
You can use the git clone command or use other methods to download aliyun-net-sdk-core and add a solution. To download aliyun-python-sdk-core in GitHub, visit aliyun-python-sdk-core.
Install the Image Search Python SDK.
(Recommended) Install the SDK by using the dependency tool.
To install the Image Search Python SDK, run the following command:pip install aliyun-python-sdk-imagesearch
Download and install the SDK on your own.
You can run the git clone command or use other methods to download aliyun-python-sdk-imagesearch and add a solution. To download aliyun-python-sdk-imagesearch in GitHub, visit aliyun-python-sdk-imagesearch.
Sample code
# -*- coding: utf8 -*-
from aliyunsdkcore.client import AcsClient
import base64
import aliyunsdkimagesearch.request.v20190325. AddImageRequest as AddImageRequest
import aliyunsdkimagesearch.request.v20190325. DeleteImageRequest as DeleteImageRequest
import aliyunsdkimagesearch.request.v20190325. SearchImageRequest as SearchImageRequest
# Create an AcsClient instance
client = AcsClient("<your-access-key-id>", "<your-access-key-secret>", "<region>")
# Add an image
request = AddImageRequest.AddImageRequest()
request.set_endpoint("imagesearch.<region>.aliyuncs.com")
request.set_InstanceName("demo")
request.set_ProductId("test")
request.set_PicName("test")
with open('/home/admin/demo.jpg', 'rb') as imgfile:
encoded_pic_content = base64.b64encode(imgfile.read())
request.set_PicContent(encoded_pic_content)
response = client.do_action_with_exception(request)
print(response)
# Search for an image
request = SearchImageRequest.SearchImageRequest()
request.set_endpoint("imagesearch.<region>.aliyuncs.com")
request.set_InstanceName("demo")
with open('/home/admin/demo.jpg', 'rb') as imgfile:
encoded_pic_content = base64.b64encode(imgfile.read())
request.set_PicContent(encoded_pic_content)
response = client.do_action_with_exception(request)
print(response)
# Delete an image
request = DeleteImageRequest.DeleteImageRequest()
request.set_endpoint("imagesearch.<region>.aliyuncs.com")
request.set_InstanceName("demo")
request.set_ProductId("test")
response = client.do_action_with_exception(request)
print(response)