Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Created June 15, 2020 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/e7f2c2de9bd712aea6c8424b21999018 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/e7f2c2de9bd712aea6c8424b21999018 to your computer and use it in GitHub Desktop.
Reading images from COCO #python
import zipfile
import numpy as np
from PIL import Image
from pycocotools import coco
# coco releases annotations and images in zip files
# you can keep the images in the zip archives because they allow random access
# however the annotations should be extracted and processed as is
c = coco.COCO("./annotations/instances_train2017.json")
filename = None
for img_info in c.imgs.values():
print(img_info['id'])
print(img_info['file_name'])
print(img_info['height'])
print(img_info['width'])
filename = img_info['file_name']
break
with zipfile.ZipFile('./train2017.zip') as train2017:
with train2017.open('train2017/{}'.format(filename)) as f:
image = Image.open(f)
image = np.asarray(image, dtype=np.uint8)
print(image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment