-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_data.py
215 lines (183 loc) · 9.02 KB
/
generate_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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-
# generate data by resizing and rotation range(360)
# LI Haodong
import cv2
import os
import numpy as np
from load_data import norm_size
import datetime
from PIL import Image
import Global_Params
number_angle = Global_Params.M_number_angle
IMAGE_SHOW = False
def clear(data_gen_path, data_no_use_path):
data_no_use = os.listdir(data_no_use_path)
for file in data_no_use:
os.remove(os.path.join(data_no_use_path, file))
print(data_no_use_path, " removed")
one_level = os.listdir(data_gen_path)
for two_level in one_level:
temp_path = os.path.join(data_gen_path, two_level)
temp_files = os.listdir(temp_path)
print(two_level, " removed")
for file in temp_files:
os.remove(os.path.join(temp_path, file))
def generate_data(data_new_path, data_gen_path, data_no_use_path):
img_width = norm_size
img_height = norm_size
ret_img = []
origin_images = os.listdir(data_new_path)
for origin_image in origin_images:
im = Image.open(os.path.join(data_new_path, origin_image))
im = im.resize((img_width, img_height))
cv_im = cv2.imread(os.path.join(data_new_path, origin_image))
cv_im = cv2.resize(cv_im, (img_width, img_height))
print(os.path.join(data_new_path, origin_image), "pixel -> ", im.size[0], "*", im.size[1])
# cv2.imshow(origin_image, cv_im)
# flag = cv2.waitKey(0)
# if flag == 27:
# cv2.destroyWindow(origin_image)
# else:
# print("generate_data.py, line:24, esc expected")
# create mask
mask = []
cv_im_gray = cv2.imread(os.path.join(data_new_path, origin_image), cv2.IMREAD_GRAYSCALE)
cv_im_gray = cv2.resize(cv_im_gray, (img_width, img_height))
circles = cv2.HoughCircles(cv_im_gray,
cv2.HOUGH_GRADIENT,
1.0,
im.size[0]/8.0,
param1=425,
param2=30,
minRadius=round(min(im.size[0], im.size[1])/40.0),
maxRadius=round(max(im.size[0], im.size[1])/20.0))
# only one circle
only_one_x = []
only_one_y = []
only_one_w = []
only_one_h = []
count_circle = 0
if circles is not None:
circles = np.uint16(np.around(circles))
for indexCircle in circles[0, :]:
only_one_x.append(indexCircle[0] - indexCircle[2])
only_one_y.append(indexCircle[1] - indexCircle[2])
only_one_w.append(indexCircle[2]*2)
only_one_h.append(indexCircle[2]*2)
# print("count_circle = ", count_circle)
count_circle = count_circle + 1
center = (indexCircle[0], indexCircle[1]) # circle center
radius = indexCircle[2] # circle radius
cv2.circle(cv_im, center, radius, (255, 0, 255), 3)
mask_temp = np.zeros((im.size[0], im.size[1]), np.uint8)
cv2.circle(mask_temp, center, radius, (255, 255, 255), thickness=-1)
mask.append(mask_temp)
if count_circle == 0:
print("count_circle = 0, no circles found!")
continue
else:
print("count_circle = ", count_circle)
if IMAGE_SHOW:
cv2.imshow(origin_image + " <circle>", cv_im)
flag = cv2.waitKey(0)
if flag == 27:
cv2.destroyWindow(origin_image + " <circle>")
elif flag == 13:
new_origin_name = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "_circle_" + origin_image
cv2.imwrite(os.path.join(data_no_use_path, new_origin_name), cv_im)
cv2.destroyWindow(origin_image + " <circle>")
print("===============" + new_origin_name + "==SAVED===================")
else:
print("generate_data.py, line:55, esc or enter expected")
crop_result = []
for index in range(count_circle):
# Copy that image using that mask
print(cv_im.shape, " ", mask[index].shape)
crop_cv_im = cv2.bitwise_and(cv_im, cv_im, mask=mask[index])
# apply threshold
_, thresh = cv2.threshold(mask[index], 1, 255, cv2.THRESH_BINARY)
# find contour
# contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
crop_cv_im = crop_cv_im[only_one_y[index]:only_one_y[index] + only_one_h[index],
only_one_x[index]:only_one_x[index] + only_one_w[index]]
crop_result.append(crop_cv_im)
for index in range(count_circle):
index -= 1
if IMAGE_SHOW:
cv2.imshow(origin_image + " <crop>", crop_result[index])
flag = cv2.waitKey(0)
if flag == 27:
cv2.destroyWindow(origin_image + " <crop>")
elif flag == 13:
new_origin_name = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "_crop_" + origin_image
cv2.imwrite(os.path.join(data_no_use_path, new_origin_name), crop_result[index])
cv2.destroyWindow(origin_image + " <crop>")
print("===============" + new_origin_name + "==SAVED===================")
else:
print("generate_data.py, line:24, esc expected")
ret_img.append(crop_cv_im) # ensure that only one image is stored
return ret_img
def generate_date_360(data_per_360_path, data_360_path):
init_groups = os.listdir(data_per_360_path)
chess_map = {}
for each_group in init_groups:
init_images = os.listdir(os.path.join(data_per_360_path, each_group))
print("In generate_date_360() <rename process> ", each_group)
count_image_num = 0
for image in init_images:
old_file = os.path.join(os.path.join(data_per_360_path, each_group), image)
new_file = os.path.join(os.path.join(data_per_360_path, each_group),
"crop_" + each_group + str(count_image_num) + ".jpg")
os.rename(old_file, new_file)
count_image_num += 1
chess_map[each_group] = count_image_num
# new api
each_rotate_angle = 360.0/Global_Params.M_number_angle
count = 0
for each_group in init_groups:
init_images = os.listdir(os.path.join(data_per_360_path, each_group))
print("<image rotating process> <", each_group, "> ======================================")
current_image_num = chess_map[each_group]
current_rotate_angle = 360.0/current_image_num
current_rotate_number = current_rotate_angle * (Global_Params.M_number_angle/360.0)
data_360_path_spec = data_360_path + "/" + each_group
count_image = 0
count_rotate = 0
for image in init_images:
img = Image.open(os.path.join(os.path.join(data_per_360_path, each_group), image))
start_angle = count_image * current_rotate_angle
count_image += 1
for i in range(int(current_rotate_number)):
img_ro = img.rotate(start_angle + i * each_rotate_angle)
data_360_path_spec_save = data_360_path_spec + "/" + str(count_rotate) + "_" + each_group + ".jpg"
count_rotate += 1
img_ro.save(data_360_path_spec_save, quality=95, subsampling=0)
count += 1
print(data_360_path_spec_save + " \t\t\t<<< angle = " + str(count_rotate) + " >>> \t\t\t" + str(count))
# old api
# origin_images = os.listdir(data_per_360_path)
# count = 0
# for origin_image in origin_images:
# im = Image.open(os.path.join(data_per_360_path, origin_image))
# # print(origin_image[5:11])
# data_360_path_spec = data_360_path + "/" + origin_image[5:11]
# print("============================== " + data_360_path_spec + " <saving> ==============================")
# for angle in range(number_angle):
# im_ro = im.rotate(angle * (360.0/number_angle))
# data_360_path_spec_save = data_360_path_spec + "/" + str(angle) + "_" + origin_image[5:11] + ".jpg"
# im_ro.save(data_360_path_spec_save, quality=95, subsampling=0)
# print(data_360_path_spec_save + " \t\t\t<<< angle = " + str(angle) + " >>> \t\t\t" + str(count))
# count += 1
# use for debug
def main():
data_new_path = Global_Params.M_data_new_path
data_gen_path = Global_Params.M_data_gen_path
data_no_use_path = Global_Params.M_data_no_use_path
data_per_360_path = Global_Params.M_data_per_360_path
data_360_path = Global_Params.M_data_360_path
clear(data_gen_path, data_no_use_path)
# circle_img = generate_data(data_new_path, data_gen_path, data_no_use_path)
generate_date_360(data_per_360_path, data_360_path)
# 调用函数
if __name__ == '__main__':
main()