-
Notifications
You must be signed in to change notification settings - Fork 0
/
Detector.py
314 lines (268 loc) · 9.67 KB
/
Detector.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import os
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import interpolation
def print_shape(xmin, ymin,xmax, ymax, fname, file):
xdiff = xmax-xmin
ydiff = ymax-ymin
xmargin = xdiff*0.3
if(abs(ydiff-xdiff) >= int(xmargin)):
# print("Rectangle shape " + fname)
file.write("Rectangle shape " + fname+ "\n")
else:
# print("Square shape " + fname)
file.write("Square shape " + fname+ "\n")
def skew_correction(image, delta=1, maxlimit=90):
def return_score(arr, angle):
data = interpolation.rotate(arr, angle, reshape=False, order=0)
histogram = np.sum(data, axis=1)
score = np.sum((histogram[1:] - histogram[:-1]) ** 2)
return histogram, score
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
threshold = cv2.threshold(
gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
scores = []
angles = np.arange(-maxlimit, maxlimit + delta, delta)
for angle in angles:
histogram, score = return_score(threshold, angle)
scores.append(score)
best_angle = angles[scores.index(max(scores))]
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, best_angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_REPLICATE)
dst = cv2.fastNlMeansDenoisingColored(
rotated, None, 10, 10, 7, 21) # image denoising
return best_angle, dst
def get_parent_dir(n=1):
""" returns the n-th parent dicrectory of the current
working directory """
current_path = os.path.dirname(os.path.abspath(__file__))
for k in range(n):
current_path = os.path.dirname(current_path)
return current_path
src_path = os.path.join(get_parent_dir(1), "2_Training", "src")
utils_path = os.path.join(get_parent_dir(1), "Utils")
sys.path.append(src_path)
sys.path.append(utils_path)
import argparse
from keras_yolo3.yolo import YOLO, detect_video
from PIL import Image
from timeit import default_timer as timer
from utils import load_extractor_model, load_features, parse_input, detect_object
import test
import utils
import pandas as pd
import numpy as np
from Get_File_Paths import GetFileList
import random
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
# Set up folder names for default values
data_folder = os.path.join(get_parent_dir(n=1), "Data")
image_folder = os.path.join(data_folder, "Source_Images")
image_test_folder = os.path.join(image_folder, "Test_Images")
detection_results_folder = os.path.join(image_folder, "Test_Image_Detection_Results")
detection_results_file = os.path.join(detection_results_folder, "Detection_Results.csv")
model_folder = os.path.join(data_folder, "Model_Weights")
model_weights = os.path.join(model_folder, "trained_weights_final.h5")
model_classes = os.path.join(model_folder, "data_classes.txt")
anchors_path = os.path.join(src_path, "keras_yolo3", "model_data", "yolo_anchors.txt")
FLAGS = None
if __name__ == "__main__":
# Delete all default flags
parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
"""
Command line options
"""
parser.add_argument(
"--input_path",
type=str,
default=image_test_folder,
help="Path to image/video directory. All subdirectories will be included. Default is "
+ image_test_folder,
)
parser.add_argument(
"--output",
type=str,
default=detection_results_folder,
help="Output path for detection results. Default is "
+ detection_results_folder,
)
parser.add_argument(
"--no_save_img",
default=False,
action="store_true",
help="Only save bounding box coordinates but do not save output images with annotated boxes. Default is False.",
)
parser.add_argument(
"--file_types",
"--names-list",
nargs="*",
default=[],
help="Specify list of file types to include. Default is --file_types .jpg .jpeg .png .mp4",
)
parser.add_argument(
"--yolo_model",
type=str,
dest="model_path",
default=model_weights,
help="Path to pre-trained weight files. Default is " + model_weights,
)
parser.add_argument(
"--anchors",
type=str,
dest="anchors_path",
default=anchors_path,
help="Path to YOLO anchors. Default is " + anchors_path,
)
parser.add_argument(
"--classes",
type=str,
dest="classes_path",
default=model_classes,
help="Path to YOLO class specifications. Default is " + model_classes,
)
parser.add_argument(
"--gpu_num", type=int, default=1, help="Number of GPU to use. Default is 1"
)
parser.add_argument(
"--confidence",
type=float,
dest="score",
default=0.25,
help="Threshold for YOLO object confidence score to show predictions. Default is 0.25.",
)
parser.add_argument(
"--box_file",
type=str,
dest="box",
default=detection_results_file,
help="File to save bounding box results to. Default is "
+ detection_results_file,
)
parser.add_argument(
"--postfix",
type=str,
dest="postfix",
default="_catface",
help='Specify the postfix for images with bounding boxes. Default is "_catface"',
)
FLAGS = parser.parse_args()
save_img = not FLAGS.no_save_img
file_types = FLAGS.file_types
if file_types:
input_paths = GetFileList(FLAGS.input_path, endings=file_types)
else:
input_paths = GetFileList(FLAGS.input_path)
img_endings = (".jpg", ".jpg", ".png")
input_image_paths = []
for item in input_paths:
if item.endswith(img_endings):
input_image_paths.append(item)
output_path = FLAGS.output
if not os.path.exists(output_path):
os.makedirs(output_path)
# define YOLO detector
yolo = YOLO(
**{
"model_path": FLAGS.model_path,
"anchors_path": FLAGS.anchors_path,
"classes_path": FLAGS.classes_path,
"score": FLAGS.score,
"gpu_num": FLAGS.gpu_num,
"model_image_size": (416, 416),
}
)
# Make a dataframe for the prediction outputs
out_df = pd.DataFrame(
columns=[
"image",
"image_path",
"xmin",
"ymin",
"xmax",
"ymax",
"label",
"confidence",
"x_size",
"y_size",
]
)
# labels to draw on images
class_file = open(FLAGS.classes_path, "r")
input_labels = [line.rstrip("\n") for line in class_file.readlines()]
print("Found {} input labels: {} ...".format(len(input_labels), input_labels))
fileResults = open("detectedShapes.txt","w")
fileResults.write("Detected shapes: \n")
if input_image_paths:
print(
"Found {} input images: {} ...".format(
len(input_image_paths),
[os.path.basename(f) for f in input_image_paths[:5]],
)
)
start = timer()
text_out = ""
# This is for images
for i, img_path in enumerate(input_image_paths):
print(img_path)
prediction, image = detect_object(
yolo,
img_path,
save_img=save_img,
save_img_path=FLAGS.output,
postfix=FLAGS.postfix,
)
y_size, x_size, _ = np.array(image).shape
for single_prediction in prediction:
out_df = out_df.append(
pd.DataFrame(
[
[
os.path.basename(img_path.rstrip("\n")),
img_path.rstrip("\n"),
]
+ single_prediction
+ [x_size, y_size]
],
columns=[
"image",
"image_path",
"xmin",
"ymin",
"xmax",
"ymax",
"label",
"confidence",
"x_size",
"y_size",
],
)
)
img = Image.open(img_path)
area = (single_prediction[0], single_prediction[1], single_prediction[2], single_prediction[3])
cropped_img = img.crop(area)
outpath = os.path.join(get_parent_dir(1), "CroppedImages")
src_fname, ext = os.path.splitext(img_path)
save_fname = os.path.join(outpath, os.path.basename(src_fname)+'.jpg')
print_shape(single_prediction[0], single_prediction[1], single_prediction[2], single_prediction[3], os.path.basename(src_fname), fileResults)
cropped_img.save(save_fname)
imageToBeRotated = cv2.imread(save_fname)
angle, rotated = skew_correction(imageToBeRotated)
print("Skew angle: " + str(angle))
cv2.imwrite(save_fname, rotated)
end = timer()
print(
"Processed {} images in {:.1f}sec - {:.1f}FPS".format(
len(input_image_paths),
end - start,
len(input_image_paths) / (end - start),
)
)
fileResults.close()
out_df.to_csv(FLAGS.box, index=False)
# Close the current yolo session
yolo.close_session()