本文介紹Image Search服務Python SDK的使用方法及樣本。

準備工作

使用如下方式安裝依賴包。
  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 = '<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_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 = '<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,
        '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 = '<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()
  • 結果樣本
    {
      '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 = '<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()
  • 結果樣本
    {
      'RequestId': '9ADA959B-B639-4B3B-841D-2399F1C34DA8',
      'Success': True,
      'Message': 'success',
      'Code': 0
    }