SHARE

We looked at the Bing Visual Search API in a previous blog post, in this blog post we continue to explore another Bing API – the Bing Image Search API.

The Bing Image Search API (which is different from the Bing Visual Search API), lets you add search options to your app or website, from static and animated images to detailed insights, and makes it easy for you to ship solutions that contain functionality that you can find in Bing.com/images.

Features

The API lets you supply many parameters to help you find, filter and page through images you’re interested in, and while the key use case for the Bing Image Search API is image search, it also contains some other additional features to help you further refine your search queries which include the following:

Filtering and image restriction: Narrow search results by filtering the images that Bing returns by editing query parameters, for example, only clipart-style images, images found in the last 2 weeks, images with a face, etc.

Custom thumbnails: Resize, expand, or crop thumbnail previews for the images returned by Bing Image Search, for example, smartly crop around face.

Pivot and Intelligent Search: Expand your search capabilities by including and displaying Bing-suggested search terms to queries.

Get Trending Images: Customise a search for trending images from around the world.

It doesn’t end there though! You can improve your search experience by augmenting your Bing Image Search solution by integrating other services such as the Bing Autosuggest API to help guide your users with search terms as they type. You could even take this a step further and introduce another API from the Cognitive Services stack such as the Visual Search API to infer further insights from images you’ve retrieved from the web.

You may even want to apply the Text Analytics API to surface popular keywords within the Bing Image Search API JSON for analytics purposes.

The Endpoints

Now you have an idea of what the Bing Image Search API can do, it’s worth mentioning the key endpoints you need to connect to. Each endpoint offers different functionality and as such, can accepts different parameters.

Search: Returns images that are relevant to the user’s search query: https://api.cognitive.microsoft.com/bing/v7.0/images/search

Details: Returns insights about an image, such as webpages that include the image: https://api.cognitive.microsoft.com/bing/v7.0/images/details

Trending: Returns images that are trending based on search requests made by others. The images are broken out into different categories. For example, Popular People Searches: https://api.cognitive.microsoft.com/bing/v7.0/images/trending

You can find out more about the specific endpoints here. Before using these endpoints, you need to create a subscription key in the Azure Portal. You can create one for free here.

Consuming the Bing Image Search API

The Bing Image Search API, like other APIs in the Bing ecosystem, is available as a RESTful webservice and can be consumed in a language of your choice. Integration to your existing application is straightforward, whether it be using the dedicated SDK or manually constructing the REST requests and parsing out the JSON response yourself, the choice is up to you.

The process typically involves:

  1. Getting the relevant Cognitive Services key from your Azure Portal (you can create an account for free).
  2. Constructing a valid request.
  3. Processing the JSON message by parsing out the data returned by the Bing Image Search API.

I prefer to build my own APIs in C#, that way I have full control over how the data is processed when it’s retuned by an API.  This approach can mean you need to create custom objects and map JSON to specific properties as the data is returned, but NuGet packages such as Json.NET and online services like www.jsutils.com can speed this up.

Constructing Requests

Now that we’ve explained what the Bing Image Search API is and explored some of the key features it has to offer, let’s look at some examples that show you how to build a request and submit them to the Search and Trending endpoints.

Invoking the Search Endpoint with Postman

The search endpoint lets you search for images based on your query and can be invoked with just one parameter! I like to initially test out these endpoints in Postman first, this gives you a feel for the optional and mandatory parameters; it also lets you inspect the output JSON response.

In this screenshot I’m adding the subscription key to the Headers Tab, I’m also supplying a query “Audi A3” for the parameter “q”.  For readability, I’m restricting the Bing Image Search API to only return 1 image but setting the parameter “count” to 1.

Invoking the Search Endpoint with Postman

When we submit this request by clicking send, the Bing Image Search API returns the following JSON, the key nodes to look for are:

  • webSearchUrl (our search term)
  • totalEstimatedMatches (the number of potential matches)
  • isFamilyFriendly (is the image safe?)
  • contentURL (the URL that contains the image related to our search query)

Note: The Bing Image Search API response contains other nodes in this response that include, but are not limited to, Query Expansions, Pivot Suggestions and Related Searches. For this example, we’re looking at the main body of the response in the value node.

If you take the value from the node “contentURL” and paste it into your browser, an Audi A3 is returned!

Invoking the Search Endpoint with C#

We stay with the same search query “Audi A3” but this time, use C# to manually construct the web request. You can see how it takes just a few lines of code to construct a request in C# and send it to the Bing Image Search API!

A quick inspection of the JSON Visualiser in the Visual Studio debugger shows you our code has ran successfully and is returning data from the Bing Image Search API!

By drilling into the “value” nodes you can see URLs of images related to “Audi A3”:

Granted this is a simple console application but it shows you just how easy it is to integrate the Bing Image Search API using a language of your choice. You might decide to save this information in a database or integrate some of the datapoints with information in other systems.

Full source code for this console application can be found on my GitHub repo here.

Final Thoughts

Like other Search APIs we’ve previously looked at, the Bing Image Search API is easy to consume, has an active developer community, and a comprehensive set of examples, Code Samples, tutorials, and documentation to help you get up and running quickly. There is also a preview demo to see it in action:

A demonstration of the Image Search preview results for “cute animals”. Source: Microsoft

I see a few use cases for the API. These are just some examples that spring to mind and I’m sure you’ll have your own:

Marketing: Do you need to identify popular visual content that’s trending on the web? Use the Bing Image Search API to fetch what images are currently trending, take it a step further and introduce some other APIs from the Cognitive Services family and apply the Text Analytics API to surface further insights from Image Meta Data to help inform your marketing efforts.

Augment your existing search experience: Are you building a search feature in your existing application? Supplement its functionality and save development time by integrating the Bing Image Search API to give users relevant visual information.

Image filtering: Do you need to build complex search logic and filter images from the web? Let the Bing Image Search API do the heavy lifting for you!

Research and Content Curation: Are you interested in the most popular content providers of specific images on the web? Use the API to crawl the web for content and surface the most common websites or URLs that contain the images that you’re interested in.

Summary

In this blog post we’ve introduced the Bing Image Search API, we’ve explored some of the key features the API has to offer, the data it can return, and walked through a code example of it in action.

Are you considering using the Cognitive Services Search APIs in your software project? Contact the Grey Matter AI team to discuss your use case and to help get you set up: ai@greymatter.com or call +44 (0)1364 655 133.

Find out more about how Grey Matter Ltd can help you with this subject. Send us a message:

We’d love to be able to send you news about our great offers and the latest info.

Yes keep me updated please