forked from innolitics/pre-trained-keras-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_data.py
More file actions
31 lines (25 loc) · 1019 Bytes
/
Copy pathconvert_data.py
File metadata and controls
31 lines (25 loc) · 1019 Bytes
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
"""
Reads image files, decodes jpeg, resizes them, and stores them into an npz so
these operations do not have to be performed more than once.
"""
import argparse
import glob
import os
import sys
from collections import defaultdict
import numpy as np
import scipy.ndimage
import scipy.misc
import tqdm
IMG_SIZE = (256, 256)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Preprocess data for Simpsons classifier")
parser.add_argument('--data-dir', required=True, help="Directory of input data")
args = parser.parse_args(sys.argv[1:])
character_name_to_pixels = defaultdict(list)
input_data = list(glob.glob(os.path.join(args.data_dir, '**/*.jpg')))
for image_path in tqdm.tqdm(input_data):
image_pixels = scipy.ndimage.imread(image_path)
resized_image_pixels = scipy.misc.imresize(image_pixels, IMG_SIZE)
image_basepath, _ = os.path.splitext(image_path)
np.savez(image_basepath+'.npz', pixels=resized_image_pixels, compressed=True)