Getting The Bing Image Search key

Are you sure? the documentation says otherwise.

I am really confused because I do agree with you about the documentation mentioned. But when I tried it on paperspace with the url specified (https://www.googleapis.com/customsearch/v1?) it gave me a 404 not found error.
That’s when I found this google page that helps you use the API whitout code, and you can see in the code part generated automatically that it uses https://customsearch.googleapis.com
Maybe I am doing something wrong here without realising it.
Do your code still work without encoutering errors ?

Yes, I tried with both the URIs on Kaggle and both return the same information in the same format.

After a bit of slow reading I see here, here and here that they have differentiated both the URIs and best practices. In brief, https://customsearch.googleapis.com/customsearch/v1 works better with “Google-provided client libraries” since it uses gRPC transcoding syntax and https://www.googleapis.com/customsearch/v1 for conventional RESTful requests. The query parameters remain the same. My understanding may be wrong (not an expert with these jargons) but this is what I was able to gather.

@ maxk it’s also worked for me

#https://pypi.org/project/bing-image-downloader/

import sys
!{sys.executable} -m pip install numpy

from bing_image_downloader import downloader
import os, shutil
from pathlib import Path

dataset_name='bears'
path=Path(dataset_name)

#creates the parent dataset folder
if not path.exists():
    path.mkdir()

labels=['grizzly', 'black', 'teddy']

for l in labels:
    downloader.download(query=f'{l} bear',limit=100,output_dir=path,adult_filter_off=True)

    #changes the folder name from default to fastAI label specific
    if not Path(f"{path}/{l}").exists():
        os.rename(f"{path}/{l} bear", f"{path}/{l}")

    #add code to handle folder management if code is run multiple times 

Instruction

  • Install the package (bing-image-downloader ¡ PyPI)
  • Import the library at the start of the notebook
  • Replace the downloading images part of the code with the code provided here.

repo

Hope this helps.

2 Likes

Thanks for this solution! Was getting splits of only 2 folders before: grizzly and a combo of black bear and teddy. This solved it.

DuckDuckGo does not require a key

!pip install -Uqq duckduckgo_search
from duckduckgo_search import ddg_images

def search_images(term, max_images=200):
  return L(ddg_images(term, max_results=max_images)).itemgot('image')

results = search_images('teddy bear')

Use the following syntax:

download_images(dest, urls=results)

Ref:

4 Likes

After more than an hour of struggling, this worked for me: 2022-12-14.

I don’t think that the keys can be found using this method anymore.

You can also use this Google Images API for scraping Images.

Hi guys, for those who are advocating the use of the Azure API, as of July 2023, you must utilize the following code line in order to access the API Key:

key = os.environ.get('BING_SEARCH_V7_SUBSCRIPTION_KEY', 'XXXX')
1 Like