-
Notifications
You must be signed in to change notification settings - Fork 1
/
circle_crop.py
288 lines (242 loc) · 12 KB
/
circle_crop.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -*-*- coding utf-8 -*-*-
# to hough circling and get chess image
import cv2
import os
import shutil
import sys
import datetime
import numpy as np
from PIL import Image
import algorithm
import Global_Params
from keras.models import load_model
from keras.preprocessing.image import img_to_array
import filter
import server_one
import time
from load_data import str2int
# img_height = -1
# img_width = -1
CHESS_IMAGE_SHOW = True
SAVE_CROP = True
SHOW_GLOBAL = False
def read_origin_image():
origin_image_path = Global_Params.M_systemCamTest_path
# origin_image_path = "./test_image_process/systemCamTest"
images = os.listdir(origin_image_path)
images = sorted(images,
key=lambda files: os.path.getmtime(os.path.join(origin_image_path, files)),
reverse=False)
count_image = 0
origin_image_list = []
for image in images:
image_full_path = os.path.join(origin_image_path, image)
img = Image.open(image_full_path)
print(image_full_path, " pixel: " + str(img.size[0]) + "*" + str(img.size[1])) # debug
count_image = count_image + 1
origin_image_list.append(image_full_path)
print("image list done! count = ", count_image)
if count_image == 0:
print("ERROR: no image in path: ", origin_image_path)
return -999
return origin_image_list, count_image
def hough_circle(origin_image_list, count_image):
save_dir = Global_Params.M_model_save_path + "/" # the model stored there
folder = Global_Params.M_image_circle_test_path
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
# sort by last modified time
model_lists = os.listdir(save_dir)
model_lists = sorted(model_lists,
key=lambda files: os.path.getmtime(os.path.join(save_dir, files)),
reverse=False)
model_path_vertify = ""
for modelLists in os.listdir(save_dir):
model_path_vertify = os.path.join(save_dir, modelLists)
print(model_path_vertify)
# if model_path_vertify == "": # if the pwd is NULL
# print("No model saved!")
# exit()
#
# model = load_model(model_path_vertify)
# print("model loaded!")
# for index in range(0, count_image):
for index in range(len(origin_image_list)):
origin_image = cv2.imread(origin_image_list[index], cv2.IMREAD_COLOR)
print(origin_image_list[index])
if origin_image is None:
print("ERROR: circle.py line: 36, image loading failed!")
return -888
pil_origin_image = Image.open(origin_image_list[index])
origin_image_height = pil_origin_image.size[1]
origin_image_width = pil_origin_image.size[0]
img_height = origin_image_height
img_width = origin_image_width
gray_origin_image = cv2.cvtColor(origin_image, cv2.COLOR_RGB2GRAY)
gray_origin_image = cv2.medianBlur(gray_origin_image, 5) # 高斯滤波
# gray_origin_image = cv2.resize(gray_origin_image, (img_width, img_height))
# print("grey image show: index = ", index)
window_name = "grey of " + origin_image_list[index]
if CHESS_IMAGE_SHOW or SHOW_GLOBAL:
cv2.imshow(window_name, gray_origin_image)
if SHOW_GLOBAL:
flag = cv2.waitKey(1)
time.sleep(1)
save_path = Global_Params.M_imageProcessTestAns_path + "/grey_" + str(
index) + "_" + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + ".jpg"
cv2.imwrite(save_path, gray_origin_image)
print(save_path, " saved")
cv2.destroyWindow(window_name)
else:
flag = cv2.waitKey(0)
if flag == 13: # press enter to save the image
save_path = Global_Params.M_imageProcessTestAns_path + "/grey_" + str(index) + "_" + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + ".jpg"
cv2.imwrite(save_path, gray_origin_image)
print(save_path, " saved")
cv2.destroyWindow(window_name)
elif flag == 27:
cv2.destroyWindow(window_name)
else:
print("ERROR: file circle.py, line 56, flag invalid!")
# create mask
mask = []
circles = cv2.HoughCircles(
gray_origin_image, # input image, greyscale
cv2.HOUGH_GRADIENT,
1.0, # dp, the inverse ratio of resolution
round(min(img_height, img_width)/10.5), # Minimum distance between detected centers
param1=325, # Upper threshold for the internal Canny edge detector
param2=30, # Threshold for center detection
minRadius=round(min(img_height, img_width)/40.0), # Minimum radius to be detected, default 0
maxRadius=round(min(img_height, img_width)/15.0) # Maximum radius to be detected, default 0
)
# print(round(min(img_height, img_width)/8.0),
# round(min(img_height, img_width)/100.0),
# round(min(img_height, img_width)/20.0))
count_circle = 0
only_one_x = []
only_one_y = []
only_one_w = []
only_one_h = []
chess_x = []
chess_y = []
temp_origin = cv2.imread(origin_image_list[index], cv2.IMREAD_COLOR)
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
chess_x.append(indexCircle[0])
chess_y.append(indexCircle[1])
radius = indexCircle[2] # circle radius
cv2.circle(temp_origin, center, radius, (255, 0, 255), 3)
mask_temp = np.zeros((pil_origin_image.size[1], pil_origin_image.size[0]), 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!")
print("count_circle = ", count_circle)
# print("hough circle image show: index = ", index)
window_name = "hough circle of " + origin_image_list[index] # or np.array(origin_image_list)[index]
# cv2.imshow(window_name, temp_origin)
# print(str(index) + "_" + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + ".jpg")
if CHESS_IMAGE_SHOW or SHOW_GLOBAL:
cv2.imshow(window_name, temp_origin)
if SHOW_GLOBAL:
flag = cv2.waitKey(1)
time.sleep(1)
save_path = Global_Params.M_imageProcessTestAns_path + "/circle_" + str(
index) + "_" + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + ".jpg"
cv2.imwrite(save_path, temp_origin)
print(save_path, " saved")
cv2.destroyWindow(window_name)
else:
flag = cv2.waitKey(0)
if flag == 13: # press enter to save the image
save_path = Global_Params.M_imageProcessTestAns_path + "/circle_" + str(index) + "_" + datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + ".jpg"
cv2.imwrite(save_path, temp_origin)
print(save_path, " saved")
cv2.destroyWindow(window_name)
elif flag == 27:
cv2.destroyWindow(window_name)
else:
print("ERROR: file circle.py, line 88, flag invalid!")
data = []
for index_circle in range(count_circle):
# Copy that image using that mask
# print(origin_image.shape, " ", mask[index_circle].shape)
crop_cv_im = cv2.bitwise_and(origin_image, origin_image, mask=mask[index_circle])
# apply threshold
_, thresh = cv2.threshold(mask[index_circle], 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_circle]:only_one_y[index_circle] + only_one_h[index_circle],
only_one_x[index_circle]:only_one_x[index_circle] + only_one_w[index_circle]]
# cv2.imshow(str(index_circle + 1) + " <crop>", crop_cv_im)
# print(str(index_circle + 1), " \t<crop> ", crop_cv_im.shape)
data_no_use_path = Global_Params.M_image_circle_test_path
if CHESS_IMAGE_SHOW:
cv2.imshow(str(index_circle + 1) + " <crop>", crop_cv_im)
if SAVE_CROP:
flag = cv2.waitKey(1)
time.sleep(0.05)
crop_cv_im = cv2.resize(crop_cv_im, (Global_Params.M_norm_size, Global_Params.M_norm_size))
new_origin_name = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "_crop_" + str(
index) + "_" + str(index_circle) + ".jpg"
cv2.imwrite(os.path.join(data_no_use_path, new_origin_name), crop_cv_im)
cv2.destroyWindow(str(index_circle + 1) + " <crop>")
print("=============== " + os.path.join(data_no_use_path,
new_origin_name) + " ==SAVED===================")
else:
flag = cv2.waitKey(0)
if flag == 27:
cv2.destroyWindow(str(index_circle + 1) + " <crop>")
elif flag == 13:
crop_cv_im = cv2.resize(crop_cv_im, (Global_Params.M_norm_size, Global_Params.M_norm_size))
new_origin_name = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "_crop_" + \
str(index) + "_" + str(index_circle) + ".jpg"
cv2.imwrite(os.path.join(data_no_use_path, new_origin_name), crop_cv_im)
cv2.destroyWindow(str(index_circle + 1) + " <crop>")
print("=============== " + os.path.join(data_no_use_path, new_origin_name) + " ==SAVED===================")
else:
print("generate_data.py, line:24, esc expected")
crop_cv_im = cv2.resize(crop_cv_im, (Global_Params.M_norm_size, Global_Params.M_norm_size))
crop_cv_im = img_to_array(crop_cv_im)
data.append(crop_cv_im)
# data = np.array(data)
# data = filter.RedBlackBoost(data)
# data = data / 255.0
# crop_cv_im = data[0]
# crop_cv_im = np.expand_dims(crop_cv_im, 0) # 扩展至四维
# output = model.predict(crop_cv_im)
# print(str(index_circle + 1) + " <crop> CNN: ", output.argmax())
# chess_int.append(output.argmax())
# data = [] # clear
data = np.array(data)
print("board data shape = ", data.shape, " ===============================")
print("chess_x.size = ", len(chess_x))
print("chess_y.size = ", len(chess_y))
# del model
# return chess_x, chess_y # only one picture!!!
def generate_board_message(count):
print("Chess Detection Count = ", count)
oriImg, cnt = read_origin_image()
hough_circle(oriImg, cnt)
# use for debug
def main():
generate_board_message(1)
# 调用函数
if __name__ == '__main__':
main()