jjfrank
(Frank Jimenez)
January 13, 2021, 2:11pm
1
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!
1 Like
jjfrank
(Frank Jimenez)
January 13, 2021, 2:46pm
3
Hi @manju-dev ,
results/ims is 150
Path variable is “Path(‘bears’)”
jjfrank:
results/ims is 150
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.
jjfrank
(Frank Jimenez)
January 13, 2021, 3:14pm
5
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.
jjfrank:
if not path.exists():
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).
1 Like
jjfrank
(Frank Jimenez)
January 13, 2021, 4:08pm
7
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
Many thanks.
1 Like
10:57 I was getting an error after running dls = bears.dataloaders(path) from the cells further fown in the notebook. I needed to change the quotations marks in the cell with bear_types for the variable path = Path(“bears”) (double quotation marks)… Took me some googling around to make it work!
Reply
1 second ago
I’ve also needed deindent the for loop for the bear_types and remove the folders created before running the code again.
if not path.exists():
path.mkdir()
for o in bear_types:
dest = (path/o)
dest.mkdir(exist_ok=True)
download_images(dest, urls=search_images_ddg(f’{o} bear photo’))
Conwyn
February 18, 2024, 8:09pm
9
Hi Michal
It might be the forum formatting but python requires the block of code after the : has to be indented.
Python Thing :
Do things
if (boolean) :
command 1
command 2
if (another_boolean):
Conwyn
February 18, 2024, 8:11pm
10
Yes. It has de-formatted my reply.
Conwyn
February 18, 2024, 8:18pm
11
The quote is often a problem when you copy from the Internet ``````````` and ‘’‘’‘’‘’‘’‘’ seem to switch over. One is back tick and the other single quote.
“`” U+0060 Grave Accent Unicode Character and single quote is 0027
1 Like