-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
69 lines (55 loc) · 2.05 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from pathlib import Path
from urllib.request import urlretrieve
import zipfile
from imageio import imread
from scipy.ndimage.interpolation import zoom
import numpy as np
def fetch_cropped_yaleb(data_folder, zooming=0.5, max_n_subjects=None):
"""Returns a dictionary of paths
Parameters
----------
data_folder: string
zooming: float, optional, default is 0.5
factor by which to resize the images
max_n_subjects: {None, int}, optional, default is None
if not None, only the first max_n_subjects are returned
Returns
-------
dict: {
subjects_1: {'images': [image_1, ... image_N],
'ambient': image_ambient,
}
}
images are stored as numpy arrays
"""
url = 'http://vision.ucsd.edu/extyaleb/CroppedYaleBZip/CroppedYale.zip'
yaleb_path = Path(data_folder).joinpath('cropped_yaleb')
if not yaleb_path.joinpath('CroppedYale').exists():
yaleb_path.mkdir(parents=True)
# If not already unzip, do it
if not list(yaleb_path.iterdir()):
zip_path = yaleb_path.joinpath('yaleb.zip')
# If zip not already downloaded, download it
if not zip_path.exists():
print('downloading the images...')
urlretrieve(url, zip_path.as_posix())
zfile = zipfile.ZipFile(zip_path.as_posix())
zfile.extractall(path=yaleb_path.as_posix())
yaleb = {}
for folder_path in yaleb_path.joinpath('CroppedYale').iterdir():
if max_n_subjects is not None and len(yaleb) > max_n_subjects:
return yaleb
if not folder_path.is_dir():
continue
video_name = folder_path.name
paths = sorted(list(folder_path.glob('*.pgm')))
images = []
for path in paths:
if 'Ambient' in path.name:
ambient = imread(path.as_posix())
else:
images.append(zoom(imread(path.as_posix()), zooming)[None, ...])
data = {'images': np.concatenate(images),
'ambient': ambient}
yaleb[video_name] = data
return yaleb