本文介绍图像搜索服务Python SDK的使用方法及示例。
接口列表
接口名称
接口说明
Add
增加图片。
SearchImageByPic
使用图片进行搜索。
SearchImageByName
指定名称,使用已入库的图片进行搜索。
Delete
删除图片。
准备工作
使用如下方式安装依赖包。pip install alibabacloud_imagesearch20200212
Add接口
代码示例from alibabacloud_imagesearch20200212.client import Client
from alibabacloud_imagesearch20200212.models import AddImageAdvanceRequest
from alibabacloud_tea_rpc.models import Config
from alibabacloud_oss_util.models import RuntimeOptions
def addImage():
request = AddImageAdvanceRequest()
request.instance_name = ''
request.product_id = ''
request.pic_name = ''
f = open('', 'rb')
request.pic_content_object = f
config = Config()
config.access_key_id = ''
config.access_key_secret = ''
config.endpoint = 'imagesearch.[regionId].aliyuncs.com'
config.region_id = ''
config.type = 'access_key'
client = Client(config)
runtime_option = RuntimeOptions()
response = client.add_image_advance(request, runtime_option)
print(response.to_map())
f.close()
if __name__ == '__main__':
addImage()
结果示例{
'RequestId': '7F769FFC-4F45-476E-BE6C-E4EF82E012A7',
'Success': True,
'Message': 'success',
'Code': 0,
'PicInfo': {
'CategoryId': 20,
'Region': '474,747,497,784'
}
}
SearchImageByPic接口
代码示例from alibabacloud_imagesearch20200212.client import Client
from alibabacloud_imagesearch20200212.models import SearchImageByPicAdvanceRequest
from alibabacloud_tea_rpc.models import Config
from alibabacloud_oss_util.models import RuntimeOptions
def searchImageByPic() :
request = SearchImageByPicAdvanceRequest()
request.instance_name = ''
f = open('', 'rb')
request.pic_content_object = f
config = Config()
config.access_key_id = ''
config.access_key_secret = ''
config.endpoint = 'imagesearch.[regionId].aliyuncs.com'
config.region_id = ''
config.type = 'access_key'
client = Client(config)
runtime_option = RuntimeOptions()
response = client.search_image_by_pic_advance(request, runtime_option)
print(response.to_map())
f.close()
if __name__ == '__main__':
searchImageByPic()
结果示例{
'RequestId': '53C481F3-E064-428D-AB25-B6C57A704E68',
'Success': True,
'Code': 0,
'Msg': 'success',
'Auctions': [{
'CategoryId': 20,
'ProductId': 'test-version-001',
'PicName': 'test-version-001.jpg',
'CustomContent': None,
'SortExprValues': '5.37633353624177e+24;0',
'IntAttr': None,
'StrAttr': None
}, {
'CategoryId': 20,
'ProductId': 'test_0426_1',
'PicName': 'test_0426_1.png',
'CustomContent': None,
'SortExprValues': '2.71303606033325;263',
'IntAttr': None,
'StrAttr': None
}],
'Head': {
'DocsReturn': 5,
'DocsFound': 5,
'SearchTime': 295
},
'PicInfo': {
'CategoryId': 20,
'Region': '474,747,497,784',
'AllCategories': [{
'Id': 0,
'Name': 'Tops'
}, {
'Id': 1,
'Name': 'Dress'
}, {
'Id': 2,
'Name': 'Bottoms'
}, {
'Id': 3,
'Name': 'Bag'
}, {
'Id': 4,
'Name': 'Shoes'
}, {
'Id': 5,
'Name': 'Accessories'
}, {
'Id': 6,
'Name': 'Snack'
}, {
'Id': 7,
'Name': 'Makeup'
}, {
'Id': 8,
'Name': 'Bottle'
}, {
'Id': 9,
'Name': 'Furniture'
}, {
'Id': 20,
'Name': 'Toy'
}, {
'Id': 21,
'Name': 'Underwear'
}, {
'Id': 22,
'Name': 'Digital device'
}, {
'Id': 88888888,
'Name': 'Other'
}]
}
}
SearchImageByName接口
代码示例from alibabacloud_imagesearch20200212.client import Client
from alibabacloud_imagesearch20200212.models import SearchImageByNameRequest
from alibabacloud_tea_rpc.models import Config
from alibabacloud_oss_util.models import RuntimeOptions
def searchImageByName() :
request = SearchImageByNameRequest()
request.instance_name = ''
request.product_id = ''
request.pic_name = ''
config = Config()
config.access_key_id = ''
config.access_key_secret = ''
config.endpoint = 'imagesearch.[regionId].aliyuncs.com'
config.region_id = ''
config.type = 'access_key'
client = Client(config)
runtime_option = RuntimeOptions()
response = client.search_image_by_name(request, runtime_option)
print(response.to_map())
if __name__ == '__main__':
searchImageByName()
结果示例{
'RequestId': '7BC00158-3B9B-49C4-9E25-FFEC28AF3CE8',
'Success': True,
'Code': 0,
'Msg': 'success',
'Auctions': [{
'CategoryId': 20,
'ProductId': 'test-version-001',
'PicName': 'test-version-001.jpg',
'CustomContent': None,
'SortExprValues': '5.37633353624177e+24;0',
'IntAttr': None,
'StrAttr': None
}, {
'CategoryId': 20,
'ProductId': 'test_0426_1',
'PicName': 'test_0426_1.png',
'CustomContent': None,
'SortExprValues': '2.71303606033325;263',
'IntAttr': None,
'StrAttr': None
}],
'Head': {
'DocsReturn': 5,
'DocsFound': 5,
'SearchTime': 15
},
'PicInfo': {
'CategoryId': 20,
'Region': None,
'AllCategories': [{
'Id': 0,
'Name': 'Tops'
}, {
'Id': 1,
'Name': 'Dress'
}, {
'Id': 2,
'Name': 'Bottoms'
}, {
'Id': 3,
'Name': 'Bag'
}, {
'Id': 4,
'Name': 'Shoes'
}, {
'Id': 5,
'Name': 'Accessories'
}, {
'Id': 6,
'Name': 'Snack'
}, {
'Id': 7,
'Name': 'Makeup'
}, {
'Id': 8,
'Name': 'Bottle'
}, {
'Id': 9,
'Name': 'Furniture'
}, {
'Id': 20,
'Name': 'Toy'
}, {
'Id': 21,
'Name': 'Underwear'
}, {
'Id': 22,
'Name': 'Digital device'
}, {
'Id': 88888888,
'Name': 'Other'
}]
}
}
Delete接口
代码示例def deleteImage() :
request = DeleteImageRequest()
request.instance_name = ''
request.product_id = ''
request.pic_name = ''
config = Config()
config.access_key_id = ''
config.access_key_secret = ''
config.endpoint = 'imagesearch.[regionId].aliyuncs.com'
config.region_id = ''
config.type = 'access_key'
client = Client(config)
runtime_option = RuntimeOptions()
response = client.delete_image(request, runtime_option)
print(response.to_map())
if __name__ == '__main__':
deleteImage()
结果示例{
'RequestId': '9ADA959B-B639-4B3B-841D-2399F1C34DA8',
'Success': True,
'Message': 'success',
'Code': 0
}