Created
June 15, 2020 01:36
-
-
Save CMCDragonkai/e7f2c2de9bd712aea6c8424b21999018 to your computer and use it in GitHub Desktop.
Reading images from COCO #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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