Getting The Bing Image Search key

This is extremely useful. Thanks!

Hello forum this is @rajeshkumarkarra from INDIAThanks a lot @Ivy I’ve spent so much time to get the information about this key and all then i found the solution in this forum.
It’s a very helpful.
Thanks to @jeremy @rachel

Hi, I had the same issue, and had to abort the signup process midway because of this. But even after than(perhaps because I was still logged in with my windows account), I could add API v-7 on the Bing Search API page with 7 day validity for the key. That was good enough for me.

Hi, I am getting an error of Access denied.
Key entered is authenticated.
I am using Paperspace container.
I have signed up for free services in azure and I still have 6 days left.
Please Help me.
code-
results = search_images_bing(key , 'grizzly bear')
Error–

ErrorResponseException                    Traceback (most recent call last)
<ipython-input-24-d69757562e4b> in <module>
----> 1 results = search_images_bing(key , 'grizzly bear')
      2 ims = results.attrgot('content_url')
      3 len(ims)

<ipython-input-23-1ad6fe773e21> in search_images_bing(key, term, min_sz)
      1 def search_images_bing(key, term, min_sz=128):
      2     client = api('https://api.cognitive.microsoft.com/', auth(key))
----> 3     return L(client.images.search(query=term, count=150, min_height=min_sz, min_width=min_sz).value)

/opt/conda/envs/fastai/lib/python3.8/site-packages/azure/cognitiveservices/search/imagesearch/operations/_images_operations.py in search(self, query, accept_language, user_agent, client_id, client_ip, location, aspect, color, country_code, count, freshness, height, id, image_content, image_type, license, market, max_file_size, max_height, max_width, min_file_size, min_height, min_width, offset, safe_search, size, set_lang, width, custom_headers, raw, **operation_config)
    489 
    490         if response.status_code not in [200]:
--> 491             raise models.ErrorResponseException(self._deserialize, response)
    492 
    493         deserialized = None

ErrorResponseException: Operation returned an invalid status code 'Access Denied'
1 Like

Very useful. Thx a lot🙏

I made the mistake of using Bing Spell Check APIs v7

Thank you @Ivy for this info. I spent hours trying to get this key!
Very kind of you to show the way to us, mere mortals

1 Like

i was able to get this to work when i first attempted in september, using the URLs that were given when signing up and configuring the azure bing search resource. the ‘search_images_bing()’ function worked flawlessly.
but when i returned to go over the material the other day, it did not work. no matter what i tried. i even signed up for a new account, got new keys, everything.
after spending a fair amount of time trying to tease apart how that search function integrates to the azure cognitive service python module, i came away with the feeling that something had changed - and you may notice that microsoft made some big change around bing search on 10/30/2020 - but i was really unable to relate the info in those notices to the error messages i was getting (‘resource not found’). i think the endpoint urls were simply improper, and the microsoft client sdk was having trouble.
finally, i was able to get bing image search to work, but i had to code my own using the the technique shown here:

1 Like

It does seem like the old method of retrieving the API keys from https://azure.microsoft.com/en-us/try/cognitive-services/?api=bing-image-search-api is no longer working.

I used the following code as described on the Microsoft quickstart guide: https://docs.microsoft.com/en-us/bing/search-apis/bing-image-search/quickstarts/rest/python

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

search_term = "grizzly"

params  = {"q": search_term, "license": "public", "imageType": "photo"}

response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()

img_urls = [img['contentUrl'] for img in search_results["value"]]

When checking the length of img_urls, it seems like it is now 35 instead of the earlier 150. Does anyone have an idea how to increase the number of image URLs that can be retrieved from the Bing Search API?

3 Likes

I have got the same problem. This is clearly the problem of Microsoft, links are broken on their website, documentation missing.

I have looked around for the substitute and my best shot so far is SerpApi, https://serpapi.com/

1 Like

Oh by the way I have figured out the image call limit. Just add count inside the params

params  = {"q": search_term, "license": "public", "imageType": "photo", "count":"150"}
2 Likes

Yeah it happened very recently, and caused quite a bit of hassle for all of us while sorting out this Azure issue.

Hi everyone,

I found this library that does not require a key to query images from Bing : https://pypi.org/project/bing-image-downloader/
It is very simple to use and at least is free for more than 7 days. :slight_smile:
!pip install bing-image-downloader
from bing_image_downloader import downloader
for q in [“grizzly”, “black bear”, “teddy bear”]:
downloader.download(q, limit=20, output_dir=‘bears’, adult_filter_off=True, force_replace=False, timeout=5)
Hope it helps !
Charles

7 Likes

I’m having a very similar issue, on the code cell that has.
results = search_images_bing(key, ‘grizzly bear’)
ims = results.attrgot(‘content_url’)
len(ims)
I am receiving.
ErrorResponseException: Operation returned an invalid status code 'PermissionDenied'

@klty0988 What cell do I replace in the Clean Section of the Jupyter notebook with this code ?

Hi Jeremy,

Because of the API issue, I have actually abandoned the search_images_bing function to retrieve the image URLs.

Here is what I have done instead (to obtain the ims, which I have renamed as img_urls) to match the updated Bing Search V7 API.

search_term = "grizzly"

#Add in count parameter so that max number of images (150) is #retrieved upon each API call. Otherwise, default is 35.
params  = {"q": search_term, "license": "public", "imageType": "photo", "count":"150"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json() # Generating a json file

# Collating the image URLs into a list
img_urls = [img['contentUrl'] for img in search_results["value"]]

# Get length of the img_urls list
len(img_urls)
        
# Downloading images from the list of image URLs
download_images(dest, urls=img_urls)

The above performs the same function as the code you provided, and it has worked for me. I am currently writing a Medium post on this (with the updated codes), so I will share it with you guys once it is completed.

3 Likes

Hi Kenneth,

Thank you for such a clear response. Regarding the script you provided, I was able to move past the initial error however now I’am presented with this one.
FileExistsError: [Errno 17] File exists: 'images/grizzly.jpg'

is this something you have encounter before ? and if so how do I go about this error ?

I suspect it is because you already have the grizzly.jpg image inside your images folder from your earlier attempts. You might want to consider restarting it all from a clean slate, and you can do so by clicking Factory reset runtime from the Runtime dropdown menu to reset everything and delete any pre-existing files.

Thank you for the updates, after following through with your instruction I have a new issue.

HTTPError: 401 Client Error: PermissionDenied for url: https://api.bing.microsoft.com/v7.0/images/search?q=grizzly&license=public&imageType=photo&count=150

would you be open to a zoom call , to assist me further on this ?

Update: After restarting the kernel and running the code I am presented with the error.

NameError: name 'dest' is not defined

However “dest” references: dest = 'images/grizzly.jpg'

so be default it will always grab the same image, should I be grabbing a different image from my search queries or am I missing something ?

Can someone post all the cells from the beginning to show how to solve this problem? I have been going over the solutions posted here with no success. This is a major break in the initial steps of the process for chapter two. One of the authors or someone associated with the book needs to get involved and take this issue seriously. The notebook for chapter two should be updated to reflect the recent changes by Azure that have broken the code in the notebook.