Lesson 2 - How I solved Bing Image search issue

Hi Community,
Let me share my solution to the issue many people experiencing now when trying to run Lesson 2 notebook - bing_image_search doesn’t work.

Long story short. The search_images_bing method from utils.py uses Azure SDK that uses Bing API which is available if you create new Bing Search API v7 from Azure portal (Looks like microsoft is doing some changes to the services structure and bing search services).
To solve it, I created my own bing search function in my notebook, that uses requests module only, instead of Azure Python SDK

import requests

def search_images_bing_v7(key, term, min_sz=128):
    headers = {"Ocp-Apim-Subscription-Key": key}
    params = {"q": term, "imageType": "photo", "count": 150, "height": min_sz, "min_width": min_sz}
    response = requests.get('https://api.bing.microsoft.com/v7.0/images/search', headers=headers, params=params)
    response.raise_for_status()
    return L(response.json()['value'])

Rest of the notebook code that uses the method needs small adoption. Instead of getting content_url attribute, not it is contentUrl, e.g.:
Change any occurence of code like this results.attrgot('content_url') into that results.attrgot('contentUrl').

Hope, anyone will find it useful.

3 Likes

Hi @marcincz, you may want to use this library that is much easier and does not require an API key :

Hope it helps !
Charles

5 Likes

Thanks @nn.Charles that works like a charm

Thank you very much for your Solution! It worked perfectly!!!

Hi,
Marcin’s solution did not work for me - tells me I don’t have a correct key, though I’m copying it straight from my azure account (tried both keys even).
Also Charles’ solution doesn’t work - it does downloads pictures but they are completely irrelevant, even though I used “grizzly”, “black bear” and “teddy bear” as written.
Can anyone advise me on that?
Thanks

@OrLi. How did you configured your bing service on Azure ? I just checked it today morning (I provisioned it once again from scratch to be sure and the solution works fine).

To provision the bing service (v7 api) you can simply go directly here: https://portal.azure.com/#create/Microsoft.BingSearch

To get the key, you can copy one of the key from here:

1 Like

Using Colab and your example, this works too, also modified to show one of the downloaded images:

import requests
from PIL import Image
from io import BytesIO

results = search_images_bing(key, ‘cow’, min_sz=128, max_images=1)
ims = results.attrgot(‘contentUrl’)

r = requests.get(ims[0])
im = Image.open(BytesIO(r.content))
show_image(im)