-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateAugmentedData.py
More file actions
126 lines (104 loc) · 4.69 KB
/
CreateAugmentedData.py
File metadata and controls
126 lines (104 loc) · 4.69 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Created on Tue Jun 25 17:38:08 2019
@author: bhuwan
Website: https://medium.com/@bhuwanbhattarai/image-data-augmentation-and-parsing-into-an-xml-file-in-pascal-voc-format-for-object-detection-4cca3d24b33b
"""
import cv2
import numpy as np
import os
import xml.etree.ElementTree as ET
import StaticMethods as sm
xml_input_folder_path = "AutoAugmentXML"
picture_input_path = "AutoAugmentImages"
textfile_out_path = "AugmentedXmlDataPath.txt"
augmented_img_path = "AugmentedImages"
sm.createFolder(augmented_img_path)
def pca_color_augmentation(image):
assert image.ndim == 3 and image.shape[2] == 3
assert image.dtype == np.uint8
img = image.reshape(-1, 3).astype(np.float32)
sf = np.sqrt(3.0 / np.sum(np.var(img, axis=0)))
img = (img - np.mean(img, axis=0)) * sf
cov = np.cov(img, rowvar=False) # calculate the covariance matrix
value, p = np.linalg.eig(cov) # calculation of eigen vector and eigen value
rand = np.random.randn(3) * 0.08
delta = np.dot(p, rand * value)
delta = (delta * 255.0).astype(np.int32)[np.newaxis, np.newaxis, :]
img_out = np.clip(image + delta, 0, 255).astype(np.uint8)
return img_out
xml_paths = [os.path.join(xml_input_folder_path, s) for s in os.listdir(xml_input_folder_path)]
# print(xml_paths)
pwd_lines = []
for xml_file in xml_paths:
et = ET.parse(xml_file)
element = et.getroot()
element_objs = element.findall('object')
element_filename = element.find('filename').text
base_filename = os.path.join(picture_input_path, element_filename)
print("Create augmention for file: %s" % base_filename)
img = cv2.imread(base_filename)
rows, cols = img.shape[:2]
img_split = element_filename.strip().split('.jpg')
for element_obj in element_objs:
class_name = element_obj.find('name').text # return name tag ie class of disease from xml file
obj_bbox = element_obj.find('bndbox')
# print(obj_bbox)
x1 = int(round(float(obj_bbox.find('xmin').text)))
y1 = int(round(float(obj_bbox.find('ymin').text)))
x2 = int(round(float(obj_bbox.find('xmax').text)))
y2 = int(round(float(obj_bbox.find('ymax').text)))
# if you specify range(1) total number of augmented data is 6 for one image
# 6 types of augmentation means pca, horizontal and vertical flip, 3 rotation
# if you specify range(2) total number of augmented data is 12 for one image (or multiply it as often as you want)
for color in range(2):
img_color = pca_color_augmentation(img)
color_name = img_split[0] + '-color' + str(color)
color_jpg = color_name + '.jpg'
new_path = os.path.join(augmented_img_path, color_jpg) # join with augmented image path
lines = [color_jpg, ',', str(x1), ',', str(y1), ',', str(x2), ',', str(y2), ',', class_name, '\n']
pwd_lines.append(lines)
if not os.path.isfile(new_path):
cv2.imwrite(new_path, img_color)
# for horizontal and vertical flip
f_points = [0, 1]
for f in f_points:
f_img = cv2.flip(img_color, f)
h, w = img_color.shape[:2]
if f == 1:
f_x1 = w - x2
f_y1 = y1
f_x2 = w - x1
f_y2 = y2
f_str = 'f1'
elif f == 0:
f_x1 = x1
f_y1 = h - y2
f_x2 = x2
f_y2 = h - y1
f_str = 'f0'
new_name = color_name + '-' + f_str + '.jpg'
new_path = os.path.join(augmented_img_path, new_name)
lines = [new_name, ',', str(f_x1), ',', str(f_y1), ',', str(f_x2), ',', str(f_y2), ',', class_name,
'\n']
pwd_lines.append(lines)
if not os.path.isfile(new_path):
cv2.imwrite(new_path, f_img)
# for angle 180
img_180 = cv2.flip(img_color, -1)
ang_x1 = w - x2
ang_y1 = h - y2
ang_x2 = w - x1
ang_y2 = h - y1
new_name_180 = color_name + '-' + 'rotate_180' + '.jpg'
new_path_180 = os.path.join(augmented_img_path, new_name_180)
lines_180 = [new_name_180, ',', str(ang_x1), ',', str(ang_y1), ',', str(ang_x2), ',', str(ang_y2), ',',
class_name, '\n']
pwd_lines.append(lines_180)
if not os.path.isfile(new_path_180):
cv2.imwrite(new_path_180, img_180)
# print(pwd_lines)
if len(pwd_lines) > 0:
with open(textfile_out_path, 'w') as f:
for line in pwd_lines:
f.writelines(line)
print('End')