Cognitive services: Creating Image dataset using Azure’s Bing Image Search API

Syed Sohaib Uddin
3 min readJun 6, 2020

--

Gathering data is a huge task in AI/ML applications. However, it can be smartly handled using one of Azure’s cognitive services, the Bing search API.

Bing Web Search API

The Bing Web Search API is a RESTful service that provides instant answers to search queries. Results are easily configured to include web pages, images, videos, news, translations, and more. This API is optimal for applications that need access to all content that is relevant to a user’s search query. When building an application that requires only a specific type of result, using the Bing Image Search API, Bing Video Search API, or Bing News Search API proves to be very handy.

Pre-requisites: An active Azure subscription. You may use the Azure for Students to get a subscription for free or else you can use Azure free account.

Building a Bing web search service

  1. Navigate to your Azure portal and search for ‘cognitive services’.
  2. Click on ‘Add’ and in the following tab type in the search bar ‘bing search’ and click on the service as shown below.

3. Create the service as below. Be sure to create a new resource group and select a suitable price tier. Create the service once done.

4. On successful deployment, navigate to the resource page and click on ‘Keys and Endpoint’ and copy a key.

5. Next, create an empty folder and clone my repository from here into it.

The repo consists of python code that makes the API call via a request as below:

search = requests.get(URL, headers=headers, params=params)
  • The URL is the search URL typically a constant.
  • The header consists of the API key.
  • The params are as follows:

params = {“q”: term, “offset”: 0, “count”: GROUP_SIZE}

They include the Query argument ‘q’, the offset and the count i.e batch.

6. Open the folder in an editor and create a virtual environment by typingvirtualenv <env-name> into the terminal.

7. Activate the environment by again executing into the terminal.

<env-name>/scripts/activate

8. Install all the dependencies by runningpip install -r requirements.txt and paste your API key into the .env file.

10. Once done, the setup is all ready for a final call to the service. Execute the following to download your images into a folder called data set.

python search_bing_api.py --query <your-imagesearch-query>

The result looks like below. I ran the above query for gathering a dataset containing Cristiano Ronaldo’s images.

Dataset in the File structure

Usage and cleanup

By adjusting the group size in the request parameters, the number of images in the dataset can be adjusted. The query can be run for all the different types of images desired in the dataset.

  • Make sure that you delete the resource group after use in order to avoid the overhead costs.

Conclusion

We have successfully created and consumed a bing web service and downloaded an image dataset using it. Further, this can be explored with Bing News, Video, Visual APIs.

Further Reading

--

--