Getting The Bing Image Search key

Hi guys, it is now 2 hours that I am trying to get the “right key” to start working, and I am still failing at it even following these instructions. The problem is the following: As you can see here (I hope the resolution is good enough), I don’t have the Bing Search API as option to be accessed. I tried several others (Computer vision, Face, Personalizer) but of course they are failing. Anyone having the same issue or having an idea on how to solve it? Many many thanks in advance!

NOTE: This is actually not working for me at the moment. It downloads images, but I just get multiple copies of very few images, and at least half do not contain what is being queried. If I can get it fixed, I’ll repost, but for now, I’m assuming that the Python module is simply flawed!!!

Please be sure to import downloader using this (not ‘import downloader’ as in one of the previous posts):

from bing_image_downloader import downloader

then download images using:

downloader.download(‘grizzly bear’, limit=100, output_dir=‘images’)

I haven’t yet incorporated the downloaded images into the jupyter notebook, so I can’t help there yet. Also, take the following into consideration:

  1. The default limit is 100, but you may want to change that value. Other options, I’m good with defaulting. You should probably look at the docs for the module at https://pypi.org/project/bing-image-downloader/

  2. A subfolder, named according to the search term, will automatically be created inside the directory that you specify.

  3. If you search on the term “grizzly” rather than “grizzly bear”, you will get images that don’t even contain a bear. Whatever search term you use, you should browse through the images that you retrieve and delete any that don’t contain an image that you expect.

  4. This is probably quite a bit slower than using an API key and the recommended libraries, but it is free and so far has worked for me. I probably wouldn’t use it for getting thousands of images.

  5. I got this to work in a simple python shell - just enter the command ‘python’ in your shell. Then enter the commands above. No need to create an actual python program file.

1 Like

I am having the same issue…

I’ve been reading this topic and I followed @klty0988’s post. Thanks! It seems to be working.
I’ll leave my two cents for anyone coming after me, hopefully it will save time.
Unfortunately I can only put 2 urls and 1 image on my reply.

1.Register on Azure
1.1Google azure and click on Azure’s website
1.2Click the green “Start Free” button
1.3 Follow the registration process

2. Create a resource - Bing Search V7
2.1 Login to Azure Portal - Home: https://portal.azure.com/#home
2.2 Click “Create a resource”
2.3 on the search bar type “Bing Search V7”, click create
2.4 Fill in all fields and create - You have created a Container and BingSearch resource

3 - Get the Key
3.1 Go again to the link on point 2.1
3.2 Click on the bingsearch resource (you might have given it a different name when you created it)
3.3 On the left side panel, click keys and endpoints


3.4 And Key 1 will be the key to replace on the code

From here just follow the post @klty0988 did - Thanks Kenneth once again! =)
https://towardsdatascience.com/classifying-images-of-alcoholic-beverages-with-fast-ai-34c4560b5543

9 Likes

Thanks a lot!
This seemed to solve the issue for me. But then, down the road, when I run:

if not path.exists():
path.mkdir()
for o in bear_types:
dest = (path/o)
dest.mkdir(exist_ok=True)
results = search_images_bing(key, f’{o} bear’)
download_images(dest, urls=results.attrgot(‘content_url’))

I get an error at the download_images() line. The error is as the following. Any idea why the download_images() function is not working?
---------------------------------------------------------------------------
_RemoteTraceback Traceback (most recent call last)
_RemoteTraceback:
“”"
Traceback (most recent call last):
File “/opt/conda/envs/fastai/lib/python3.8/concurrent/futures/process.py”, line 239, in _process_worker
r = call_item.fn(*call_item.args, **call_item.kwargs)
File “/opt/conda/envs/fastai/lib/python3.8/concurrent/futures/process.py”, line 198, in _process_chunk
return [fn(*args) for args in chunk]
File “/opt/conda/envs/fastai/lib/python3.8/concurrent/futures/process.py”, line 198, in
return [fn(*args) for args in chunk]
File “/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/xtras.py”, line 475, in _call
return g(item)
File “/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/vision/utils.py”, line 25, in _download_image_inner
url_path = Path(url)
File “/opt/conda/envs/fastai/lib/python3.8/pathlib.py”, line 1038, in new
self = cls._from_parts(args, init=False)
File “/opt/conda/envs/fastai/lib/python3.8/pathlib.py”, line 679, in _from_parts
drv, root, parts = self._parse_args(args)
File “/opt/conda/envs/fastai/lib/python3.8/pathlib.py”, line 663, in _parse_args
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType
“”"

The above exception was the direct cause of the following exception:

TypeError Traceback (most recent call last)
in
5 dest.mkdir(exist_ok=True)
6 results = search_images_bing(key, f’{o} bear’)
----> 7 download_images(dest, urls=results.attrgot(‘content_url’))

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/vision/utils.py in download_images(dest, url_file, urls, max_pics, n_workers, timeout, preserve_filename)
35 dest = Path(dest)
36 dest.mkdir(exist_ok=True)
—> 37 parallel(partial(_download_image_inner, dest, timeout=timeout, preserve_filename=preserve_filename),
38 list(enumerate(urls)), n_workers=n_workers)
39

Hey folks, I ran into an issue getting the api key as well and so far this is what has worked for me.

  1. Make sure you have the azure account created
  2. go to Bing Search API Pricing | Microsoft Bing while logged in already
  3. click on try now and it should take you to where the resource can be created. which is this url: Create - Microsoft Azure
  4. create the resource and then click the “go to resource” button and the key is in the “keys and endpoints” section on the left hand side.

this worked for me. Hopefully it works for anyone else struggling with this until microsoft fixes the issue.

7 Likes

hi
i had the same issue. Seems Bing changed the json properties (check out the result).
It is not “content_url” but “contentUrl” now:

urls = results.attrgot(‘contentUrl’)

1 Like

Hey all,

I have my API key but whenever I post it and replace the ‘XXX’ with my key I get the following:

Does anyone know what is going wrong? I don’t know where to define os

I fixed it. I didn’t run the stuff in the notebook before it.

1 Like

This worked for me with some tweaking. I think there is an error in the first line of the second code block which should read: from bing_image_downloader import downloader

Here is my light-weight approach that I verified just now to be working. First I followed the guide @thebigtoona posted :+1:

I then added this cell below the one that starts with (key = os ...) to redefine the included function search_bing_images (this is adopted from this quickstart guide) :

def my_search_images_bing(key, search_term):

    search_url = "https://api.bing.microsoft.com/v7.0/images/search"
    headers = {"Ocp-Apim-Subscription-Key" : key}
    min_size = 128

    params  = {"q": search_term,
               "imageType": "photo",
               "minHeight": min_size,
               "minWidth": min_size,
               "count": 150}
    response = requests.get(search_url, headers=headers, params=params)
    response.raise_for_status()
    search_results = response.json()

    return L(search_results['value'])

search_images_bing = my_search_images_bing
search_images_bing

Edit: clarified code
Hope someone might find this useful.

1 Like

thank you so much, it worked :slight_smile:

Hi all,

Resharing this walkthrough tutorial article for everyone’s reference, since there still seems to be issues with the Bing image downloader: https://towardsdatascience.com/classifying-images-of-alcoholic-beverages-with-fast-ai-34c4560b5543

Cheers!

Hey Kenneth - FYI that link requires a paid medium account to view :frowning:

Hey Tim, sharing the friend link here so that the paywall is deactivated: https://towardsdatascience.com/classifying-images-of-alcoholic-beverages-with-fast-ai-34c4560b5543?sk=d0efa0e6b6d214c52b337a0381a4fd3d

Just did this now. I had already signed up for an Azure account, but the link above did not work.

However, I searched “cognitive services” and found the Bing Search v7 in that.

I believe I first clicked “All Resources” and used that search bar.

NOTE: it no longer says API anywhere as it did in the pictures above

1 Like

Thank you. I follow your suggestion, and use Bing Search v7 instead.
I seems that Bing Search Key link has been changed.


In addition, ims = results.attrgot(‘content_url’) should be changed to ims = results.attrgot(‘contentUrl’) to notebook to work
3 Likes

Hi, @czechcheck. SerpApi developer here. I’ve randomly noticed this comment. Thank you for trying our service to get image URLs.

We can offer free search credits for fast.ai students. Just write us a message via a contact form at serpapi.com.

I’ll post a wrapper function like search_images_bing for SerpApi if someone is interested.

2 Likes

It seems everything has changed from how it was back in 2020. Maybe this is useful for someone who is trying to set up Bing Seach API in 2021 (March).

  1. Go over to the Microsoft API website
  2. Click on “try now”
  3. Finish the setup with Azure.
  4. Fill in the details to create the Bing resource. Make sure to create a new “resource group” and click “create”.
  5. Find your key under the section “Keys and Endpoint”
  6. Copy the key, place it in a file and call it when needed.

Cheers,

9 Likes

Man, you saved my life.