Vision utils

Open In Colab

Some utils function to quickly download a bunch of images, check them and pre-resize them

  1. /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.)
  2. return torch._C._cuda_getDeviceCount() > 0
  1. with tempfile.TemporaryDirectory() as d:
  2. d = Path(d)
  3. url = "https://www.fast.ai/images/jh-head.jpg"
  4. _download_image_inner(d, (125,url))
  5. assert (d/'00000125.jpg').is_file()
  6. with tempfile.TemporaryDirectory() as d:
  7. d = Path(d)
  8. url = "https://www.fast.ai/images/jh-head.jpg"
  9. _download_image_inner(d, (125,url), preserve_filename=True)
  10. assert (d/'jh-head.jpg').is_file()
  11. assert not (d/'jh-head.jpg1').exists()
  12. _download_image_inner(d, (125,url), preserve_filename=True)
  13. assert (d/'jh-head.jpg').is_file()
  14. 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

  1. with tempfile.TemporaryDirectory() as d:
  2. d = Path(d)
  3. url_file = d/'urls.txt'
  4. 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()]))
  5. download_images(d, url_file)
  6. for i in [0,2]: assert (d/f'0000000{i}.jpg').is_file()
  7. assert (d/f'00000001.JPG').is_file()
  8. with tempfile.TemporaryDirectory() as d:
  9. d = Path(d)
  10. url_file = d/'urls.txt'
  11. 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()]))
  12. download_images(d, url_file, preserve_filename=True)
  13. assert (d/'jh-head.jpg').is_file()
  14. assert (d/'thomas.JPG').is_file()
  15. assert (d/'sg-head.jpg').is_file()
  16. assert not (d/'jh-head1.jpg').exists()
  17. assert not (d/'thomas1.JPG').exists()
  18. assert not (d/'sg-head1.jpg').exists()
  19. download_images(d, url_file, preserve_filename=True)
  20. assert (d/'jh-head.jpg').is_file()
  21. assert (d/'thomas.JPG').is_file()
  22. assert (d/'sg-head.jpg').is_file()
  23. assert (d/'jh-head1.jpg').is_file()
  24. assert (d/'thomas1.JPG').is_file()
  25. 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)

  1. class _FakeImg():
  2. def __init__(self, size): self.size=size
  3. img = _FakeImg((200,500))
  4. test_eq(resize_to(img, 400), [160,400])
  5. 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

  1. file = Path('images/puppy.jpg')
  2. dest = Path('.')
  3. resize_image(file, max_size=400, dest=dest)
  4. im = Image.open(dest/file.name)
  5. test_eq(im.shape[1],400)
  6. (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

  1. with tempfile.TemporaryDirectory() as d:
  2. dest = Path(d)/'resized_images'
  3. resize_images('images', max_size=100, dest=dest)
  1. /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
  2. warnings.warn(

Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Mar 31, 2021