-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtransform_images.py
More file actions
62 lines (49 loc) · 2.03 KB
/
transform_images.py
File metadata and controls
62 lines (49 loc) · 2.03 KB
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
"""
Takes a set of images as inputs, transforms them using multiple algorithms to
make it suitable for ingestion into ML routines, then finally outputs them
to disk.
"""
import csv
import traceback
import numpy as np
import cv2
from common.config import get_config
from common.image_transformation import apply_image_transformation
def write_frame_to_file(frame, frame_label, writer):
"""
Convert the multi-dimensonal array of the image to a one-dimensional one
and write it to a file, along with its label.
"""
# print("Writing frame to file...")
flattened_frame = frame.flatten()
output_line = [frame_label] + np.array(flattened_frame).tolist()
writer.writerow(output_line)
#print("Done!")
def main():
images_transformed_path = get_config('images_transformed_path')
with open(images_transformed_path, 'w') as output_file:
writer = csv.writer(output_file, delimiter=',')
training_images_labels_path = get_config('training_images_labels_path')
with open(training_images_labels_path, 'r') as file:
lines = file.readlines()
for line in lines:
#print("\n\n" + line.strip())
image_path, image_label = line.split()
# Read the input image.
frame = cv2.imread(image_path)
# `frame` is a HxW numpy ndarray of triplets (pixels), where H and W are
# the dimensions of the input image.
# cv2.imshow("Original", frame)
try:
frame = apply_image_transformation(frame)
write_frame_to_file(frame, image_label, writer)
except Exception:
exception_traceback = traceback.format_exc()
print("Error while applying image transformation on image path '{}' with the following exception trace:\n{}".format(
image_path, exception_traceback))
continue
# cv2.waitKey(1000)
cv2.destroyAllWindows()
print ("The program completed successfully !!")
if __name__ == '__main__':
main()