Share via


Quickstart: Get image insights using the Bing Visual Search REST API and Python

Warning

On October 30, 2020, the Bing Search APIs moved from Azure AI services to Bing Search Services. This documentation is provided for reference only. For updated documentation, see the Bing search API documentation. For instructions on creating new Azure resources for Bing search, see Create a Bing Search resource through the Azure Marketplace.

Use this quickstart to make your first call to the Bing Visual Search API. This Python application uploads an image to the API and displays the information it returns. Although this application is written in Python, the API is a RESTful Web service compatible with most programming languages.

Prerequisites

Create an Azure resource

Start using the Bing Visual Search API by creating one of the following Azure resources:

Bing Search v7 resource

  • Available through the Azure portal until you delete the resource.
  • Select the S9 pricing tier.

Multi-service resource

  • Available through the Azure portal until you delete the resource.
  • Use the same key and endpoint for your applications, across multiple Azure AI services.

Initialize the application

  1. Create a new Python file in your favorite IDE or editor, and add the following import statement:

    import requests, json
    
  2. Create variables for your subscription key, endpoint, and the path to the image you're uploading. For the value of BASE_URI, you can use the global endpoint in the following code, or use the custom subdomain endpoint displayed in the Azure portal for your resource.

    
    BASE_URI = 'https://api.cognitive.microsoft.com/bing/v7.0/images/visualsearch'
    SUBSCRIPTION_KEY = 'your-subscription-key'
    imagePath = 'your-image-path'
    
  3. When you upload a local image, the form data must include the Content-Disposition header. Set its name parameter to "image", and set the filename parameter to the file name of your image. The contents of the form include the binary data of the image. The maximum image size you can upload is 1 MB.

    --boundary_1234-abcd
    Content-Disposition: form-data; name="image"; filename="myimagefile.jpg"
    
    ÿØÿà JFIF ÖÆ68g-¤CWŸþ29ÌÄøÖ‘º«™æ±èuZiÀ)"óÓß°Î= ØJ9á+*G¦...
    
    --boundary_1234-abcd--
    
  4. Create a dictionary object to hold your request's header information. Bind your subscription key to the string Ocp-Apim-Subscription-Key.

    HEADERS = {'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY}
    
  5. Create another dictionary to contain your image, which is opened and uploaded when you send the request.

    file = {'image' : ('myfile', open(imagePath, 'rb'))}
    

Parse the JSON response

Create a method called print_json() to accept the API response, and print the JSON.

def print_json(obj):
    """Print the object as json"""
    print(json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')))

Send the request

Use requests.post() to send a request to the Bing Visual Search API. Include the string for your endpoint, header, and file information. Print response.json() with print_json().

try:
    response = requests.post(BASE_URI, headers=HEADERS, files=file)
    response.raise_for_status()
    print_json(response.json())
    
except Exception as ex:
    raise ex

Next steps