-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyolo_utils.py
59 lines (54 loc) · 2.57 KB
/
yolo_utils.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
import numpy as np
import argparse
import cv2 as cv
import subprocess
import time
import os
from detect import detectChecking
def generate_boxes_confidences_classids(outs, height, width, tconf):
boxes = []
confidences = []
classids = []
for out in outs:
for detection in out:
# Get the scores, classid, and the confidence of the prediction
scores = detection[5:]
classid = np.argmax(scores)
confidence = scores[classid]
# Consider only the predictions that are above a certain confidence level
if confidence > 0.35:
# TODO Check detection
box = detection[0:4] * np.array([width, height, width, height])
centerX, centerY, bwidth, bheight = box.astype('int')
# Using the center x, y coordinates to derive the top
# and the left corner of the bounding box
x = int(centerX - (bwidth / 2))
y = int(centerY - (bheight / 2))
# Append to list
boxes.append([x, y, int(bwidth), int(bheight)])
confidences.append(float(confidence))
classids.append(classid)
return boxes, confidences, classids
def infer_image(net, layer_names, height, width, img, colors, labels, FLAGS,labelh,
boxes=None, confidences=None, classids=None, idxs=None, infer=True):
if infer:
# Contructing a blob from the input image
blob = cv.dnn.blobFromImage(img, 1 / 255.0, (832, 832), swapRB=True, crop=False)
# Perform a forward pass of the YOLO object detector
net.setInput(blob)
# Getting the outputs from the output layers
start = time.time()
outs = net.forward(layer_names)
end = time.time()
# if FLAGS.show_time:
print ("[INFO] YOLOv3 took {:6f} seconds".format(end - start))
# Generate the boxes, confidences, and classIDs
boxes, confidences, classids = generate_boxes_confidences_classids(outs, height, width, FLAGS.confidence)
# Apply Non-Maxima Suppression to suppress overlapping bounding boxes
idxs = 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 '[ERROR] Required variables are set to None before drawing boxes on images.'
# Draw labels and boxes on the image
detect = detectChecking(img, boxes, confidences, classids, idxs, colors, labels,height,labelh)
print("yolo util detect ",detect)
return detect