Vision utils
Some utils function to quickly download a bunch of images, check them and pre-resize them
/usr/local/lib/python3.8/dist-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
return torch._C._cuda_getDeviceCount() > 0
with tempfile.TemporaryDirectory() as d:
d = Path(d)
url = "https://www.fast.ai/images/jh-head.jpg"
_download_image_inner(d, (125,url))
assert (d/'00000125.jpg').is_file()
with tempfile.TemporaryDirectory() as d:
d = Path(d)
url = "https://www.fast.ai/images/jh-head.jpg"
_download_image_inner(d, (125,url), preserve_filename=True)
assert (d/'jh-head.jpg').is_file()
assert not (d/'jh-head.jpg1').exists()
_download_image_inner(d, (125,url), preserve_filename=True)
assert (d/'jh-head.jpg').is_file()
assert (d/'jh-head1.jpg').is_file()
download_images
[source]
download_images
(dest
,url_file
=None
,urls
=None
,max_pics
=1000
,n_workers
=8
,timeout
=4
,preserve_filename
=False
)
Download images listed in text file url_file
to path dest
, at most max_pics
with tempfile.TemporaryDirectory() as d:
d = Path(d)
url_file = d/'urls.txt'
url_file.write_text("n".join([f"https://www.fast.ai/images/{n}" for n in "jh-head.jpg thomas.JPG sg-head.jpg".split()]))
download_images(d, url_file)
for i in [0,2]: assert (d/f'0000000{i}.jpg').is_file()
assert (d/f'00000001.JPG').is_file()
with tempfile.TemporaryDirectory() as d:
d = Path(d)
url_file = d/'urls.txt'
url_file.write_text("n".join([f"https://www.fast.ai/images/{n}" for n in "jh-head.jpg thomas.JPG sg-head.jpg".split()]))
download_images(d, url_file, preserve_filename=True)
assert (d/'jh-head.jpg').is_file()
assert (d/'thomas.JPG').is_file()
assert (d/'sg-head.jpg').is_file()
assert not (d/'jh-head1.jpg').exists()
assert not (d/'thomas1.JPG').exists()
assert not (d/'sg-head1.jpg').exists()
download_images(d, url_file, preserve_filename=True)
assert (d/'jh-head.jpg').is_file()
assert (d/'thomas.JPG').is_file()
assert (d/'sg-head.jpg').is_file()
assert (d/'jh-head1.jpg').is_file()
assert (d/'thomas1.JPG').is_file()
assert (d/'sg-head1.jpg').is_file()
resize_to
[source]
resize_to
(img
,targ_sz
,use_min
=False
)
Size to resize to, to hit targ_sz
at same aspect ratio, in PIL coords (i.e w*h)
class _FakeImg():
def __init__(self, size): self.size=size
img = _FakeImg((200,500))
test_eq(resize_to(img, 400), [160,400])
test_eq(resize_to(img, 400, use_min=True), [400,1000])
verify_image
[source]
verify_image
(fn
)
Confirm that fn
can be opened
verify_images
[source]
verify_images
(fns
)
Find images in fns
that can’t be opened
resize_image
[source]
resize_image
(file
,dest
,max_size
=None
,n_channels
=3
,ext
=None
,img_format
=None
,resample
=2
,resume
=False
, **kwargs
)
Resize file to dest to max_size
file = Path('images/puppy.jpg')
dest = Path('.')
resize_image(file, max_size=400, dest=dest)
im = Image.open(dest/file.name)
test_eq(im.shape[1],400)
(dest/file.name).unlink()
resize_images
[source]
resize_images
(path
,max_workers
=2
,max_size
=None
,recurse
=False
,dest
=Path('.')
,n_channels
=3
,ext
=None
,img_format
=None
,resample
=2
,resume
=None
, **kwargs
)
Resize files on path recursively to dest to max_size
with tempfile.TemporaryDirectory() as d:
dest = Path(d)/'resized_images'
resize_images('images', max_size=100, dest=dest)
/Users/hamelsmu/anaconda3/lib/python3.8/site-packages/PIL/Image.py:962: UserWarning: Palette images with Transparency expressed in bytes should be converted to RGBA images
warnings.warn(
©2021 fast.ai. All rights reserved.
Site last generated: Mar 31, 2021