This topic shows you how to use Image Search SDK for Python and provides sample code.

Methods

Method Description
Add Adds images.
SearchImageByPic Searches for similar images based on a new image.
SearchImageByName Searches for similar images based on the name of an existing image in the image gallery.
Delete Deletes images.

Before you begin

Run the following command to install the dependency:
  pip install alibabacloud_imagesearch20201214

Add

  • Sample code
    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()
  • Sample responses
    {
      'RequestId': '7F769FFC-4F45-476E-BE6C-E4EF82E012A7',
      'Success': True,
      'Message': 'success',
      'Code': 0,
      'PicInfo': {
        'CategoryId': 20,
        'Region': '474,747,497,784'
      }
    }

SearchImageByPic

  • Sample code
    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()
  • Sample responses
    {
      '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

  • Sample code
    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)
        runtime_option = RuntimeOptions()
        response = client.search_image_by_name(request, runtime_option)
        print(response.to_map())
    if __name__ == '__main__':
        searchImageByName()
  • Sample responses
    {
      '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

  • Sample code
    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)
        runtime_option = RuntimeOptions()
        response = client.delete_image(request, runtime_option)
        print(response.to_map())
    if __name__ == '__main__':
        deleteImage()
  • Sample responses
    {
      'RequestId': '9ADA959B-B639-4B3B-841D-2399F1C34DA8',
      'Success': True,
      'Message': 'success',
      'Code': 0
    }