-
Notifications
You must be signed in to change notification settings - Fork 3
/
img_rec.py
315 lines (256 loc) · 9.18 KB
/
img_rec.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
315
import os
import socket
import cv2
import numpy
import darknet
PORT = 3055
FORMAT = "utf-8"
SERVER = "192.168.13.13"
ADDRESS = (SERVER, PORT)
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect(ADDRESS)
WEIGHT_FILE_PATH = "./backup/yolov4_custom_train_final.weights"
CONFIG_FILE_PATH = "./cfg/yolov4_custom_test.cfg"
DATA_FILE_PATH = "./data/yolov4.data"
RPI_IP = "192.168.13.13"
MJPEG_STREAM_URL = "http://" + RPI_IP + "/html/cam_pic_new.php"
YOLO_BATCH_SIZE = 4
THRESH = 0.9
os.chdir("C:\\darknet\\darknet-master\\build\\darknet\\x64")
def stack_images(scale, img_array):
"""
Given array of arrays, stack images into one giant image.
Taken from https://github.com/murtazahassan/Learn-OpenCV-in-3-hours/blob/master/chapter8.py and modified
"""
rows = len(img_array)
cols = len(img_array[0])
rows_available = isinstance(img_array[0], list)
width = img_array[0][0].shape[1]
height = img_array[0][0].shape[0]
if rows_available:
for x in range(0, rows):
for y in range(0, cols):
if img_array[x][y].shape[:2] == img_array[0][0].shape[:2]:
img_array[x][y] = cv2.resize(img_array[x][y], (0, 0), None, scale, scale)
else:
img_array[x][y] = cv2.resize(img_array[x][y], (img_array[0][0].shape[1], img_array[0][0].shape[0]),
None, scale, scale)
if len(img_array[x][y].shape) == 2:
img_array[x][y] = cv2.cvtColor(img_array[x][y], cv2.COLOR_GRAY2BGR)
image_blank = numpy.zeros((height, width, 3), numpy.uint8)
hor = [image_blank] * rows
for x in range(0, rows):
hor[x] = numpy.hstack(img_array[x])
ver = numpy.vstack(hor)
else:
for x in range(0, rows):
if img_array[x].shape[:2] == img_array[0].shape[:2]:
img_array[x] = cv2.resize(img_array[x], (0, 0), None, scale, scale)
else:
img_array[x] = cv2.resize(img_array[x], (img_array[0].shape[1], img_array[0].shape[0]), None, scale,
scale)
if len(img_array[x].shape) == 2:
img_array[x] = cv2.cvtColor(img_array[x], cv2.COLOR_GRAY2BGR)
hor = numpy.hstack(img_array)
ver = hor
return ver
def capture_img():
"""
Captures a frame from mjpeg stream and returns an OpenCV image.
"""
cap = cv2.VideoCapture(MJPEG_STREAM_URL)
ret, frame = cap.read()
return frame
def run_image_detection(image, network, class_names, class_colors, thresh):
"""
Does image detection using darknet and returns detection
"""
width = darknet.network_width(network)
height = darknet.network_height(network)
darknet_image = darknet.make_image(width, height, 3)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (width, height),
interpolation=cv2.INTER_LINEAR)
darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
darknet.free_image(darknet_image)
image = darknet.draw_boxes(detections, image_resized, class_colors)
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
def chunks(lst, n):
"""
Splits an array of images into chunks of size n.
Taken from https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
"""
for counter in range(0, len(lst), n):
yield lst[counter:counter + n]
def show_all_images(frame_list):
"""
Shows all the images in the given array.
It will ensure that the array is of size divisible by 3, so the giant image can be properly shown.
"""
blank_image = numpy.zeros_like(frame_list[0])
for i in range(3):
if len(frame_list) % 3 == 0:
break
frame_list.append(blank_image)
formatted_list = tuple(chunks(frame_list, 3))
img_stack = stack_images(1, formatted_list)
cv2.imshow("Images", img_stack)
cv2.imwrite("detected_images.jpg", img_stack)
if cv2.waitKey() & 0xFF == ord("q"):
cv2.destroyAllWindows()
def continuous_detection():
"""
Runs detection continuously.
"""
# Mapping of image names to id
mapping = {
"bullseye": 0,
"up": 1,
"down": 2,
"right": 3,
"left": 4,
"stop": 5,
"one": 6,
"two": 7,
"three": 8,
"four": 9,
"five": 10,
"six": 11,
"seven": 12,
"eight": 13,
"nine": 14,
"A": 15,
"B": 16,
"C": 17,
"D": 18,
"E": 19,
"F": 20,
"G": 21,
"H": 22,
"S": 23,
"T": 24,
"U": 25,
"V": 26,
"W": 27,
"X": 28,
"Y": 29,
"Z": 30
}
results = {}
images = {}
network, class_names, class_colors = darknet.load_network(
CONFIG_FILE_PATH,
DATA_FILE_PATH,
WEIGHT_FILE_PATH,
YOLO_BATCH_SIZE
)
try:
print("Image recognition started!")
to_stop = False
image_counter = 0
counter = 5
while not to_stop or counter > 0:
cv2.waitKey(50)
msg = read()
# Show read message
print(msg)
if msg[0] == "R": # take photo command
pass
elif msg[0] == "S": # stop img rec after last send
to_stop = True
else:
continue
obstacle_id = msg[1:]
img_rec_string = "Nothing detected..."
# structure: in a list, (id, confidence, [(bbox)])
# index: 0-id 1-confidence 2-bbox
# bbox: x,y,w,h
frame = capture_img()
image, detections = run_image_detection(frame, network, class_names, class_colors, THRESH)
# keep track of bigger image
curr_height = 0
for i in detections:
image_id = i[0]
bbox = i[2]
x_coordinate = int(bbox[0])
height = int(bbox[3])
# calculate distance
if height > 190:
distance = 15
elif height > 170:
distance = 20
elif height > 150:
distance = 25
elif height > 133:
distance = 30
elif height > 117:
distance = 35
elif height > 95:
distance = 40
elif height > 85:
distance = 45
elif height > 78:
distance = 50
elif height > 72:
distance = 55
elif height > 64:
distance = 60
elif height > 61:
distance = 65
elif height > 58:
distance = 70
else:
distance = 75
# 52 is height of 80 cm
# calculate position
if x_coordinate < 83:
position = "LEFT"
elif x_coordinate < 166:
position = "CENTRE"
else:
position = "RIGHT"
# 250 is maximum
# find the bigger image and don't detect bullseye
if height > curr_height and image_id != "bullseye":
curr_height = height
img_rec_string = obstacle_id + "|" + str(mapping[image_id]) + "|" + str(
distance) + "|" + position
results[obstacle_id] = i
images[obstacle_id] = image
if obstacle_id not in images:
conn.send(img_rec_string.encode(FORMAT))
if to_stop:
counter -= 1
continue
if to_stop:
counter = 0
# draw text of obstacle on image
images[obstacle_id] = cv2.putText(images[obstacle_id],
"Obstacle #" + obstacle_id + ": " + results[obstacle_id][0] + "(" + str(
mapping[results[obstacle_id][0]]) + ")", (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
image_counter += 1
message = img_rec_string.encode(FORMAT)
conn.send(message)
except KeyboardInterrupt:
print("End of image recognition.")
# generate stitch image and save it
result_frame_list = list(images.values())
show_all_images(result_frame_list)
def read():
"""
Reads message from server.
"""
try:
msg = conn.recv(1024).decode()
return msg
except socket.error as e:
print("exception: ", e)
def main():
"""
Main method
"""
continuous_detection()
if __name__ == "__main__":
main()