mirror of
https://github.com/Laurent2916/REVA-QCAV.git
synced 2024-11-08 22:42:02 +00:00
02e2314149
Former-commit-id: c981801ccc3b74047e94c76e67c4ff1f3097226c
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
#
|
|
# load.py : utils on generators / lists of ids to transform from strings to
|
|
# cropped images and masks
|
|
|
|
import os
|
|
|
|
import numpy as np
|
|
from PIL import Image
|
|
|
|
from .utils import resize_and_crop, get_square, normalize, hwc_to_chw
|
|
|
|
|
|
def get_ids(dir):
|
|
"""Returns a list of the ids in the directory"""
|
|
return (f[:-4] for f in os.listdir(dir))
|
|
|
|
|
|
def split_ids(ids, n=2):
|
|
"""Split each id in n, creating n tuples (id, k) for each id"""
|
|
return ((id, i) for i in range(n) for id in ids)
|
|
|
|
|
|
def to_cropped_imgs(ids, dir, suffix, scale):
|
|
"""From a list of tuples, returns the correct cropped img"""
|
|
for id, pos in ids:
|
|
im = resize_and_crop(Image.open(dir + id + suffix), scale=scale)
|
|
yield get_square(im, pos)
|
|
|
|
def get_imgs_and_masks(ids, dir_img, dir_mask, scale):
|
|
"""Return all the couples (img, mask)"""
|
|
|
|
imgs = to_cropped_imgs(ids, dir_img, '.jpg', scale)
|
|
|
|
# need to transform from HWC to CHW
|
|
imgs_switched = map(hwc_to_chw, imgs)
|
|
imgs_normalized = map(normalize, imgs_switched)
|
|
|
|
masks = to_cropped_imgs(ids, dir_mask, '_mask.gif', scale)
|
|
|
|
return zip(imgs_normalized, masks)
|
|
|
|
|
|
def get_full_img_and_mask(id, dir_img, dir_mask):
|
|
im = Image.open(dir_img + id + '.jpg')
|
|
mask = Image.open(dir_mask + id + '_mask.gif')
|
|
return np.array(im), np.array(mask)
|