Lesson two: I tried everything and nothing works :(

Hi guys,

I am stuck at the same place as many people are/have been: the azure bing search and downloading images.

I have used this code (I believe from a user called Retuso):

def search_images_bing(key, term, max_images: int = 100, **kwargs):
params = {‘q’:term, ‘count’:max_images}
headers = {“Ocp-Apim-Subscription-Key”:key}
search_url = “https://api.bing.microsoft.com/v7.0/images/search
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()
return L(search_results[‘value’])

I also changed the content_url to contentUrl:

results = search_images_bing(key, ‘grizzly bear’, min_sz=128)
ims = results.attrgot(‘contentUrl’)

However, when I go down to displaying the pictures, it says the variable does not have any inside.

fns = get_image_files(path)
fns

(#0) []

Anyone can shed a bit of light? Hopefully this helps others in the same position as me as I haven’t yet found anyone with the same issues as me.

Many thanks!

  • what is the length of results/ims? (To make sure that function is working correctly.)
  • what is in path variable(in get_image_files)? I don’t see it initialized in your code.

Hi @manju-dev,

results/ims is 150
Path variable is “Path(‘bears’)”

So it seems the call to the API was successful (and also the extraction of the URLs from the returned object).

have you downloaded images to that path? After the API call, you need to run download_images. I don’t see it in above code.

Hi @manju-dev, I cannot understand your question well. This is my code if this helps…

results = search_images_bing(key, ‘grizzly bear’)
ims = results.attrgot(‘contentUrl’)
len(ims)

150

#hide
ims = [‘http://3.bp.blogspot.com/-S1scRCkI3vY/UHzV2kucsPI/AAAAAAAAA-k/YQ5UzHEm9Ss/s1600/Grizzly%2BBear%2BWildlife.jpg’]

[{“metadata”:{“trusted”:true},“cell_type”:“code”,“source”:“dest = ‘images/grizzly.jpg’\ndownload_url(ims[0], dest)”,“execution_count”:21,“outputs”:[]}]

im = Image.open(dest)
im.to_thumb(128,128)

bear_types = ‘grizzly’,‘black’,‘teddy’
path = Path(‘bears’)

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(‘contentUrl’))

fns = get_image_files(path)
fns

(#0) []

This code is same as in the book. I get it. We need to check what’s hppening with your code step by step.

Here are somethings you need to check:

So, have you set the key properly? This question is answered if you get the output 150.

This should throw an error because the values should be inside a list or tuple.

If the directory already exists (../bear), the code won’t run the loop. So make sure you don’t have that folder already.

You can post a screenshot or link to your actual notebook if possible(without the key of course).

That worked. You were right. When I had trouble with the API key, I went ahead and ran the cell that creates a folder (‘Bears’)… and of course, it created an empty folder! I deleted the folder and because the API is working as it should, it created again the ‘Bears’ folder and downloaded the images as it should.

Hopefully this is helpful for others in the same situation :slight_smile:

Many thanks.