-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert_image_data.py
66 lines (47 loc) · 1.68 KB
/
convert_image_data.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
import os
import os.path
from os import listdir
from os.path import join, isdir
import cv2
import numpy as np
from images_utils import get_images_recursively
def imread(path):
img_bgr = cv2.imread(path)
img_rgb = img_bgr[..., ::-1]
# img_hsl = convert_to_hsl(img_rgb)
return img_rgb.astype(np.float)
def normalize_rgb(image):
return np.array(image) / 127.5 - 1.
def is_normalized(image):
for x, row in enumerate(image):
for y, p in enumerate(row):
if -1 > p[0] > 1 and -1 > p[1] > 1 and -1 > p[2] > 1:
return False
return True
def create_folder_is_not_exists(folder):
if not os.path.exists(folder):
os.mkdir(folder)
def convert_to_files(data_set):
images_paths = get_images_recursively('data/' + data_set)
data_set_rgb = data_set + '-rgb'
# data_set_hsl = data_set + '-hsl'
if not os.path.exists('data/' + data_set_rgb):
os.mkdir('data/' + data_set_rgb)
count = 0
for image_path in images_paths:
print('converting {} to RGB'.format(image_path))
rgb_normalized = normalize_rgb(imread(image_path))
np.save('data/' + data_set_rgb + '/' + str(count) + '.npy', rgb_normalized)
count += 1
# if not os.path.exists('data/' + data_set_hsl):
# os.mkdir('data/' + data_set_hsl)
# count = 0
# for image_path in images_paths:
# print('converting {} to HSL'.format(image_path))
# hsl = convert_to_hsl(imread(image_path))
# np.save('data/' + data_set_hsl + '/' + str(count) + '.npy', hsl)
# count += 1
folders = [f for f in listdir('data') if isdir(join('data', f))]
for data_set in folders:
if not data_set.endswith('-rgb') and not data_set.endswith('-hsl'):
convert_to_files(data_set)