-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolov3_object_detection.py
52 lines (42 loc) · 2.04 KB
/
yolov3_object_detection.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
import numpy
import argparse
import cv2 as cv
def draw_labels_and_boxes(img, boxes, confidences, classids, idxs, colors, labels):
if len(idxs) > 0:
for i in idxs.flatten():
x, y = boxes[i][0], boxes[i][1]
w, h = boxes[i][2], boxes[i][3]
color = [int(c) for c in colors[classids[i]]]
cv.rectangle(img, (x,y), (x+w, y+h), color, 2)
text = "{}: {:4f}".format(labels[classids[i]], confidences[i])
cv.putText(img, text, (x,y-5), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
return img
def generateeeeee_boxes_confidences_classids(outs, heights, width, tconf):
boxes = []
confidences = []
classids = []
for out in outs:
for detection in outs:
scores = detection[5:]
classids = np.argmax(scotes)
confidence = scores[classid]
if confidence > tconf:
box = detection[0:4] * np.array([width, height, width, heigt])
centerX, centerY, bwidth, bheight = box.astype('int')
x = int(centerX - (bwidth / 2))
y = int(centerY) - (bheight / 2))
boxes.append([x, y, int(bwidth), int(bheight)])
confidences.append(float(confidence))
classid.append(classid)
return boxes, confidences, classids
def infer_image(net, layer_names, height, width, img, colors, labels, FLAGS, boxes=None, confidences=None, classids = None, infer = True):
if infer:
blob = cv.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB = True, crop=False)
net.setInput(blob)
outs = net.forward(layers_names)
boxes, confidences, classids = generateeeeee_boxes_confidences_classids(outs, height, width, FLAGS.confidence)
idx = cv.dnn.NMSBoxes(boxes, confidences, FLAGS.confidence, FLAGS.threshold)
if boxes is None or confidences is None or idxs is None or classids is None:
raise 'Required Variables Are Set To None!'
img = draw_labels_and_boxes(img, boxes, confidences, classids, idxs, colors, labels)
return img, boxes, confidences, classids, idxs