I had the exact same question!
According to the Bing Image Search API reference, the maximum images you can get in one request is 150 images.
However, using the count and offset query parameters, we can page through and get 150 images at a time through multiple requests.
After some debugging, I’ve made the following changes to the search_images_bing() function in the utils.py file:
# +
# pip install azure-cognitiveservices-search-imagesearch
from itertools import chain
from azure.cognitiveservices.search.imagesearch import ImageSearchClient as api
from msrest.authentication import CognitiveServicesCredentials as auth
def search_images_bing(key, term, total_count=150, min_sz=128):
"""Search for images using the Bing API
:param key: Your Bing API key
:type key: str
:param term: The search term to search for
:type term: str
:param total_count: The total number of images you want to return (default is 150)
:type total_count: int
:param min_sz: the minimum height and width of the images to search for (default is 128)
:type min_sz: int
:returns: An L-collection of ImageObject
:rtype: L
"""
max_count = 150
client = api("https://api.cognitive.microsoft.com", auth(key))
imgs = [
client.images.search(
query=term, min_height=min_sz, min_width=min_sz, count=count, offset=offset
).value
for count, offset in (
(
max_count if total_count - offset > max_count else total_count - offset,
offset,
)
for offset in range(0, total_count, max_count)
)
]
return L(chain(*imgs))
You can now use the search_images_bing() function in your notebooks as follows:
from utils import *
results = search_images_bing(key, "grizzly bear", 500)
I have not been able to find if there is a maximum to the number of times we can apply an offset. For example, if Bing runs out of images. So I don’t know what will happen if you request an extremely large number of images (10,000+) and Bing has less than that amount. But as long as you don’t hit that limit, you should be OK.
Please test and let me know how it works for you. If anyone has any suggestions to improve the code, I’d love to know! If it works well, I may put in a pull request to the course repo.