Twantie
February 9, 2023, 12:59pm
1
Hello,
I am currently reviewing the first part of the course because it has been a couple of weeks since I last looked at it and am stuck at searching urls with the ddg_images function.
The method used in the video course didnt work for me, so I used the method presented in the according kaggle notebook. The last time I used it it worked, this time it didnt and I dont know why.
from duckduckgo_search import ddg_images
from fastcore.all import *
def search_images(term, max_images=30):
print(f"Searching for '{term}'")
return L(ddg_images(term, max_results=max_images)).itemgot('image')
This function will return a empty list no matter the input term, but it wont throw any exceptions.
Does anybody know what the problem could be?
Kamui
February 9, 2023, 1:24pm
2
if you want the function on the video to work import this first
import fastbook
from fastbook import *
and then search_images_ddg
will work
3 Likes
Yes it worked, dont know how that slipped past me when I tried it
Thank you!
1 Like
insect
(c)
May 11, 2023, 8:10pm
4
I’m having the same problem, did the recommended fix and still am not getting any results. Using Python3.9 on Mac Intel
Guys, I think its not an issue with any import or library. It seems like ddg is blocking the ip address. So when you restart the router/ change public ip address it will work for once.
1 Like
maitland
(iain maitland)
May 15, 2023, 10:02am
6
I did have the same experience as you Dev Patel when using ddg_images
, restarted notebook and it worked once.
I am now using search_images_ddg
per Kamui’s suggestion and it works for me.
This works for me:
def get_image_files_sorted(path, recurse=True, folders=None):
return get_image_files(path, recurse, folders).sorted()
def search_images_bing(key, term, min_sz=128, max_images=150):
params = dict(q=term, count=max_images, min_height=min_sz, min_width=min_sz)
search_url = "https://api.bing.microsoft.com/v7.0/images/search"
response = requests.get(search_url, headers={"Ocp-Apim-Subscription-Key":key}, params=params)
response.raise_for_status()
return L(response.json()['value'])
def search_images_ddg(term, max_images=200):
"Search for `term` with DuckDuckGo and return a unique urls of about `max_images` images"
assert max_images<1000
url = 'https://duckduckgo.com/'
res = urlread(url,data={'q':term})
searchObj = re.search(r'vqd=([\d-]+)\&', res)
assert searchObj
requestUrl = url + 'i.js'
params = dict(l='us-en', o='json', q=term, vqd=searchObj.group(1), f=',,,', p='1', v7exp='a')
urls,data = set(),{'next':1}
headers = dict(referer='https://duckduckgo.com/')
This does not:
_save_json(
f"ddg_{keywords}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json", results
)
elif output == 'print':
for i, x in enumerate(results, start=1):
print(f"{i}.", json.dumps(x, ensure_ascii=False, indent=2))
input()
return results
def ddg_images(
keywords,
region="wt-wt",
safesearch="Moderate",
time=None,
size=None,
color=None,
type_image=None,
layout=None,
license_image=None,
max_results=100,
1 Like
s1monw1
(Simon)
February 16, 2024, 12:01pm
8
I cannot make this work, can anyone share a working notebook?
schunico
(Nicole)
February 19, 2024, 6:35am
9
I used what (Profile - hazarath - fast.ai Course Forums ) suggested in another thread and it worked
"yeah, ddg_images is depricated
you can replace
from duckduckgo_search import ddg_images
def search_images(term, max_images=30):
print(f"Searching for '{term}'")
return L(ddg_images(term, max_results=max_images)).itemgot('image')
with
from duckduckgo_search import DDGS
def search_images(keywords, max_images = 30):
print(f"Searching for {keywords}")
return L(DDGS().images(keywords,max_results=max_images)).itemgot('image')
cdsmith007
(Craig Smith)
February 20, 2024, 8:11pm
10
Hello all, I am having the same issue. I have updated the code but it keeps blowing up, would someone be able to help:
from fastcore.all import *
from duckduckgo_search import DDGS
def search_images(keywords, max_images = 30):
print(f"Searching for {keywords}")
return L(DDGS().images(keywords,max_results=max_images)).itemgot(‘image’)
urls = search_images(‘bird photos’, max_images=1)
urls[0]
Which returns a rather long error, but ultimately ends up with:
TypeError: images() got an unexpected keyword argument ‘max_results’
Any help would be appreciated!
Thank you
@cdsmith007 I updated the original nb to use duckduckgo_search
in asyn mode, see [bird-notebook ]:
%pip install --upgrade --quiet fastai fastcore fastdownload duckduckgo-search
from duckduckgo_search import AsyncDDGS
import asyncio
async def search_images(keywords, max_results=5):
async with AsyncDDGS() as ddgs:
results = [r['image'] async for r in ddgs.images(keywords, max_results=max_results)]
return results
# search/download 'bird' image
dest = 'temp.jpg'
r = await search_images("bird")
download_url(r[0], dest=dest, show_progress=False)
im = Image.open(dest)
im.to_thumb(256,256)
this should work with duckduckgo-search==4.4.3
1 Like
cdsmith007
(Craig Smith)
February 20, 2024, 10:06pm
12
Thank you, I will try this.
I actually just got it to work. For some strange reason it wouldn’t work in Kaggle but would work in google colab. Not sure why but relieved!