本文介绍图像搜索服务Python SDK的使用方法及示例。

接口列表

接口名称 接口说明
Add 增加图片。
SearchImageByPic 使用图片进行搜索。
SearchImageByName 指定名称,使用已入库的图片进行搜索。
Delete 删除图片。

准备工作

  • 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见创建AccessKey
  • 使用如下方式安装依赖包。
  pip install alibabacloud_imagesearch20201214

Add接口

  • 代码示例
    from alibabacloud_imagesearch20201214.client import Client
    from alibabacloud_imagesearch20201214.models import AddImageAdvanceRequest
    from alibabacloud_tea_openapi.models import Config
    from alibabacloud_oss_util.models import RuntimeOptions
    def addImage():
        request = AddImageAdvanceRequest()
        request.instance_name = '<instanceName>'
        request.product_id = '<productId>'
        request.pic_name = '<picName>'
        f = open('<filePath>', 'rb')
        request.pic_content_object = f
        config = Config()
        config.access_key_id = '<accessKeyId>'
        config.access_key_secret = '<accessKeySecret>'
        config.endpoint = 'imagesearch.[regionId].aliyuncs.com'
        config.region_id = '<regionId>'
        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_imagesearch20201214.client import Client
    from alibabacloud_imagesearch20201214.models import SearchImageByPicAdvanceRequest
    from alibabacloud_tea_openapi.models import Config
    from alibabacloud_oss_util.models import RuntimeOptions
    def searchImageByPic() :
        request = SearchImageByPicAdvanceRequest()
        request.instance_name = '<instanceName>'
        f = open('<filePath>', 'rb')
        request.pic_content_object = f
        config = Config()
        config.access_key_id = '<accessKeyId>'
        config.access_key_secret = '<accessKeySecret>'
        config.endpoint = 'imagesearch.[regionId].aliyuncs.com'
        config.region_id = '<regionId>'
        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,
        'Score': 1.0
        'SortExprValues': '5.37633353624177e+24;0',
        'IntAttr': None,
        'StrAttr': None
      }, {
        'CategoryId': 20,
        'ProductId': 'test_0426_1',
        'PicName': 'test_0426_1.png',
        'CustomContent': None,
        'Score': 1.0,
        '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'
        }],
        'MultiRegion': [{
            'Region': '112,440,76,387'
         }]
      }
    }

SearchImageByName接口

  • 代码示例
    from alibabacloud_imagesearch20201214.client import Client
    from alibabacloud_imagesearch20201214.models import SearchImageByNameRequest
    from alibabacloud_tea_openapi.models import Config
    from alibabacloud_oss_util.models import RuntimeOptions
    def searchImageByName() :
        request = SearchImageByNameRequest()
        request.instance_name = '<instanceName>'
        request.product_id = '<productId>'
        request.pic_name = '<picName>'
        config = Config()
        config.access_key_id = '<accessKeyId>'
        config.access_key_secret = '<accessKeySecret>'
        config.endpoint = 'imagesearch.[regionId].aliyuncs.com'
        config.region_id = '<regionId>'
        config.type = 'access_key'
        client = Client(config)
        response = client.search_image_by_name(request)
        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,
        'score':1.0,
        'SortExprValues': '5.37633353624177e+24;0',
        'IntAttr': None,
        'StrAttr': None
      }, {
        'CategoryId': 20,
        'ProductId': 'test_0426_1',
        'PicName': 'test_0426_1.png',
        'CustomContent': None,
        'score':1.0,
        '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'
        }],
        'MultiRegion': [{
            'Region': '112,440,76,387'
         }]
      }
    }

Delete接口

  • 代码示例
    def deleteImage() :
        request = DeleteImageRequest()
        request.instance_name = '<instanceName>'
        request.product_id = '<productId>'
        request.pic_name = '<picName>'
        config = Config()
        config.access_key_id = '<accessKeyId>'
        config.access_key_secret = '<accessKeySecret>'
        config.endpoint = 'imagesearch.[regionId].aliyuncs.com'
        config.region_id = '<regionId>'
        config.type = 'access_key'
        client = Client(config)
        response = client.delete_image(request)
        print(response.to_map())
    if __name__ == '__main__':
        deleteImage()
  • 结果示例
    {
      'RequestId': '9ADA959B-B639-4B3B-841D-2399F1C34DA8',
      'Success': True,
      'Message': 'success',
      'Code': 0
    }