Spaces:
Sleeping
Sleeping
File size: 1,498 Bytes
02273ec 76e5119 02273ec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | from duckduckgo_search import DDGS
from fastcore.all import L, Path
from fastdownload import download_url
from fastai.vision.all import (
download_images,
resize_images,
verify_images,
get_image_files,
DataBlock,
ImageBlock,
CategoryBlock,
RandomSplitter,
parent_label,
Resize,
vision_learner,
resnet18,
error_rate,
PILImage
)
def search_images(term, max_images=100):
'Search images with search term "term"'
print(f"Searching for '{term}'")
with DDGS() as ddgs:
return L(ddgs.images(term, max_results=max_images)).itemgot('image')
searches = 'crimson rosella', 'cockatoo', 'australian magpie'
path = Path('australian_birds')
for o in searches:
dest = path/o
dest.mkdir(exist_ok=True, parents=True)
download_images(dest, urls=search_images(f'{o} photo'))
resize_images(path/o, max_size=400, dest=path/o)
failed = verify_images(get_image_files(path))
failed.map(Path.unlink)
dls = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=[Resize(192, method='squish')]
).dataloaders(path, bs=32)
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(3)
learn.export()
urls = search_images('cockatoo', max_images=1)
DEST = 'cockatoo.jpg'
download_url(urls[0], DEST, show_progress=False)
bird_prediction, _, probs = learn.predict(PILImage.create('cockatoo.jpg'))
|