How to search images with a text query using the Bing Image Search API of Azure

How to search images with a text query using the Bing Image Search API of Azure

There are a lot of solutions if you search how to determine the name of a thing inside a picture like Google Vision API, Amazon Rekognition and others. However, is quite difficult to find an API that does the inverse thing, searching an image by its name with a simple string. You may need an API that does this for you if you need to find an image related to a word, for example if you search "Apple Fruit", you should obtain pictures of Apples.

For example, in our application we will have about 5000 artists and bands in a database from which we only know the name, for example James Blunt, Metallica etc. So the idea is to automatize the way we gather an image for every artist with this API. This technique is used widely among developers that work with Machine Learning, as it's a pretty easy way to gather image datasets from something like a celebrity or so.

In this article, we will explain you how to easily configure all that you need to get started with the Bing Image Search API.

1. Create a free account on Azure Cloud

Azure, is a cloud computing service created by Microsoft for building, testing, deploying, and managing applications and services through Microsoft-managed data centers. The first thing you need to access the Bing Image Search API is an account that you can create in Azure Cloud here.

2. Create Bing web search service

Once you have an account, what you need to do is to access the Azure Portal and search for the Cognitive Services:

Cognitive Services Bing Web Search Service

Click on the Cognitive Services and wait until the page loads. On the new interface click on Add:

Cognitive Search Add Bing

You will another view that will allow you to search for the services. In this searchbox, type "Bing Search" and select the Bing Search Option.

Bing Search Azure

Click then on Create and fill the form with the information of your new project:

Search image with a text query Azure

Here you will have to select the type of subscription, that in this case will be free and it will be limited to 3 calls per second. Note that according to your needs, either you are using it for commercial purposes, you will have to use another plan that fits to your needs 

3. Retrieve keys and endpoint

Now that your instance is available, you will need to know the endpoint of the API and the Keys to access it, in order to do this, simply go to the resource management of the instance you just launched and click on Keys and endpoint:

Azure Keys and Endpoint

In this case, the endpoint of our service is the following URL: https://ourcodeworld-imagesearch.cognitiveservices.azure.com/bing/v7.0. We will use the images search, so the URL that we will use to request information will be: https://ourcodeworld-imagesearch.cognitiveservices.azure.com/bing/v7.0/images/search. The url will depend of the name that you used during the creation of the instance. Now, the only thing you need to do in order to request information from the service, is to add the Ocp-Apim-Subscription-Key header to the request with one of the keys previously obtained.

For example, the most basic request with cURL would look like this:

curl -X GET 'https://YOUR-INSTANCE-NAME.cognitiveservices.azure.com/bing/v7.0/images/search?q=Metallica' \
-H 'Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY_1' | json_pp

We will add the query parameter q with the value as "Metallica", which would return a response like this this:

{
    "_type": "Images",
    "instrumentation": {
        "_type": "ResponseInstrumentation"
    },
    "readLink": "https://ourcodeworld-imagesearch.cognitiveservices.azure.com/api/v7/images/search?q=Metallica",
    "webSearchUrl": "https://www.bing.com/images/search?q=Metallica&FORM=OIIARP",
    "queryContext": {
        "originalQuery": "Metallica",
        "alterationDisplayQuery": "metallica",
        "alterationOverrideQuery": "+Metallica",
        "alterationMethod": "AM_JustChangeIt",
        "alterationType": "CombinedAlterationsChained"
    },
    "totalEstimatedMatches": 748,
    "nextOffset": 45,
    "currentOffset": 0,
    "value": [
        {
            "webSearchUrl": "https://www.bing.com/images/search?view=detailv2&FORM=OIIRPO&q=Metallica&id=0E6BBDFC0053BF061011856A5BDB627856238509&simid=607996785844817365",
            "name": "Metallica Discuss Blackened, Their Patent-Pending New ...",
            "thumbnailUrl": "https://tse1.mm.bing.net/th?id=OIP.R6UXXQAJ5QV915tFvgxK7gHaE8&pid=Api",
            "datePublished": "2019-09-20T19:47:00.0000000Z",
            "isFamilyFriendly": true,
            "contentUrl": "https://www.rollingstone.com/wp-content/uploads/2018/09/metallica-whiskey.jpg",
            "hostPageUrl": "https://www.rollingstone.com/music/music-features/metallica-whiskey-distilling-process-blackened-723508/",
            "contentSize": "5540234 B",
            "encodingFormat": "jpeg",
            "hostPageDisplayUrl": "https://www.rollingstone.com/music/music-features/metallica-whiskey-distilling-process...",
            "width": 4112,
            "height": 2742,
            "hostPageFavIconUrl": "https://www.bing.com/th?id=ODF.Ns2QQRhgXqY4qm1otNlT4g&pid=Api",
            "hostPageDomainFriendlyName": "Rolling Stone",
            "thumbnail": {
                "width": 474,
                "height": 316
            },
            "imageInsightsToken": "ccid_R6UXXQAJ*cp_AE9D900D455A88168B14D495D2C23746*mid_0E6BBDFC0053BF061011856A5BDB627856238509*simid_607996785844817365*thid_OIP.R6UXXQAJ5QV915tFvgxK7gHaE8",
            "insightsMetadata": {
                "pagesIncludingCount": 53,
                "availableSizesCount": 35
            },
            "imageId": "0E6BBDFC0053BF061011856A5BDB627856238509",
            "accentColor": "585454"
        }

        // ... //
    ]
}

As you can see, the response is a JSON string that contains the results in the value key (an array). The first result returns an object with the information of the image and in this case, the thumbnail that we will use hosted in the following url https://tse1.mm.bing.net/th?id=OIP.R6UXXQAJ5QV915tFvgxK7gHaE8&pid=Api:

Metallica Bing Image Search API

Is up to you the integration of this API in your application with your favorite language, PHP, Python, JavaScript, Go etc.

Happy coding ❤️!

References and external resources

This could interest you

Become a more social person