-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_generator.py
172 lines (102 loc) · 5.11 KB
/
image_generator.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import numpy as np
import cv2
import math
import argparse
from config import Config, MakeDir, ClearDir, RemoveDir
config = Config()
parser = argparse.ArgumentParser()
parser.add_argument("--nimages", default = config.number_of_images, type = int)
parser.add_argument("--ttv", default = 'Train')
parser.add_argument("--save", default = True)
a = parser.parse_args()
number_of_images = a.nimages
number_of_piechart_images = number_of_images//2
number_of_barchart_images = number_of_images//2
if a.ttv == 'Train':
save_dir = config.train_data_dir
elif a.ttv == 'Test':
save_dir = config.test_data_dir
elif a.ttv == 'Validation':
save_dir = config.validation_data_dir
def Normalize(arr):
return arr / np.sum(arr)
def generate_barchart(number_of_barchart_images):
barchart_images = []
for i in range(number_of_barchart_images):
image_log_for_statistics = {}
if config.number_of_channels == 3:
colors = np.random.uniform(0.0, 0.9,size = (config.max_obj_num_for_bar,3))
image = np.ones(shape=(config.image_width, config.image_height, config.number_of_channels))
number_of_bars = np.random.randint(2, config.max_obj_num_for_bar + 1)
thickness = np.random.randint(1, config.max_thickness)
padding = np.random.randint(0, 10)
ratio_for_padding = (config.image_width - 2* padding)/ config.image_width
height = np.random.randint(10, config.image_height * ratio_for_padding, size = number_of_bars)
image_log_for_statistics['heigth'] = height
image_log_for_statistics['number_of_bars'] = number_of_bars
barWidth = int( ((config.image_width-3*(number_of_bars+1)-3)//number_of_bars * (np.random.randint(50,100)/100.0))* ratio_for_padding )
barWidth = max(barWidth, 4)
spaceWidth = int((config.image_width-(barWidth)*number_of_bars)//(number_of_bars+1) * ratio_for_padding)
sx = (config.image_width - barWidth*number_of_bars - spaceWidth*(number_of_bars-1))//2
for j in range(number_of_bars):
sy = int(config.image_width * ratio_for_padding)
ex = sx + barWidth
ey = sy - height[j]
if config.number_of_channels == 3:
cv2.rectangle(image,(sx,sy),(ex,ey),colors[j],-1)
else:
cv2.rectangle(image,(sx,sy),(ex,ey),0,thickness)
sx = ex + spaceWidth
noises = np.random.uniform(0, 0.05, (config.image_width, config.image_height,3))
image = image + noises
_min = 0.0
_max = image.max()
image -= _min
image /= (_max - _min)
barchart_images.append(image)
if a.save:
#np.save(save_dir+ '/Bar_image{}.npy'.format(i), image* 255)
cv2.imwrite( save_dir+ '/Bar_image{}.PNG'.format(i), image* 255)
return barchart_images
def generate_piechart(number_of_piechart_images):
piechart_images = []
for i in range(number_of_piechart_images):
number_of_pies = np.random.randint(2, config.max_obj_num_for_pie + 1)
max_w_h = max(config.image_width, config.image_height)/2
r = np.random.randint(0.1* max_w_h,max_w_h -20)
thickness = np.random.randint(1,3)
colors = np.random.uniform(0.0, 0.9,size = (config.max_obj_num_for_pie,3))
center = (int(config.image_width/2),int(config.image_height/2))
image = np.ones(shape=(config.image_width, config.image_height, 3))
angles = Normalize(np.random.randint(10,60,size=(number_of_pies)))
start_angle = 90 - np.random.randint(0,360*angles[0])/2.0
_cur_start_angle = start_angle
# cv2.circle(image,center,r,0,thickness)
for j in range(number_of_pies):
_cur_end_angle = _cur_start_angle + angles[j] * 360.0
cv2.ellipse(image, center, (r, r), 270, -_cur_start_angle, -_cur_end_angle, colors[j], -1)
_cur_start_angle = _cur_end_angle
noises = np.random.uniform(0, 0.05, (config.image_width, config.image_height,3))
image = image + noises
_min = 0.0
_max = image.max()
image -= _min
image /= (_max - _min)
piechart_images.append(image)
if a.save:
#np.save(save_dir+ '/Pie_image{}.npy'.format(i), image* 255)
cv2.imwrite( save_dir + '/Pie_image{}.PNG'.format(i), image *255)
return piechart_images
def generate_data():
barchart_images = generate_barchart(number_of_barchart_images)
piechart_images = generate_piechart(number_of_piechart_images)
return barchart_images, piechart_images
if __name__ == '__main__':
MakeDir(config.base_dir)
if a.ttv == 'Train':
ClearDir(config.train_data_dir)
if a.ttv == 'Validation':
ClearDir(config.validation_data_dir)
if a.ttv == 'Test':
ClearDir(config.test_data_dir)
generate_data()