Lesson 2 official topic

Since Bing Image Search at Microsoft Azure is not available anymore (officially fully retired), I have rewritten the search_images_bing function of Lesson 2 in Python using free Unsplash API service:

import requests
import math

def search_images_bing(key, term, max_images=150):
photo_urls = [ ]
i = 1
while i <= math.ceil(max_images/30):
url = f’https://api.unsplash.com/search/photos?query={term}&per_page=30&page={i}&client_id={key}
response = requests.get(url)
data = response.json()
for photo in data[‘results’]:
photo_urls.append(photo[‘urls’][‘regular’])
i = i + 1
return photo_urls

If you find this approach (function) helpful/useful, you need to adjust the next cell (right after the cell with search_images_bing) as follows:

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

also in the second after words “This seems to have worked nicely, so let’s use fastai’s download_images to download all the URLs for each of our search terms. We’ll put each in a separate folder:” code cell you need to replace the code line download_images(dest, urls=results.attrgot(‘contentUrl’)) by download_images(dest, urls=results)#).attrgot(‘contentUrl’))

… and by the way in this approach you need to assign key as it is, i.e. key = ‘XXXX’ (but not key = os.environ.get(‘AZURE_SEARCH_KEY’, ‘XXX’)). And yes, you need to register on Unsplash Developers Portal and get your API key there firstly.