forked from sthanhng/yoloface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
153 lines (122 loc) · 4.73 KB
/
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
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
# *******************************************************************
#
# Author : Thanh Nguyen, 2018
# Email : [email protected]
# Github : https://github.com/sthanhng
#
# BAP, AI Team
# Face detection using the YOLOv3 algorithm
#
# Description : utils.py
# This file contains the code of the parameters and help functions
#
# *******************************************************************
import datetime
import numpy as np
import cv2
# -------------------------------------------------------------------
# Parameters
# -------------------------------------------------------------------
CONF_THRESHOLD = 0.5
NMS_THRESHOLD = 0.4
IMG_WIDTH = 416
IMG_HEIGHT = 416
# Default colors
COLOR_BLUE = (255, 0, 0)
COLOR_GREEN = (0, 255, 0)
COLOR_RED = (0, 0, 255)
COLOR_WHITE = (255, 255, 255)
COLOR_YELLOW = (0, 255, 255)
# -------------------------------------------------------------------
# Help functions
# -------------------------------------------------------------------
# Get the names of the output layers
def get_outputs_names(net):
# Get the names of all the layers in the network
layers_names = net.getLayerNames()
# Get the names of the output layers, i.e. the layers with unconnected
# outputs
return [layers_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# Draw the predicted bounding box
def draw_predict(frame, conf, left, top, right, bottom):
# Draw a bounding box.
cv2.rectangle(frame, (left, top), (right, bottom), COLOR_YELLOW, 2)
text = '{:.2f}'.format(conf)
# Display the label at the top of the bounding box
label_size, base_line = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
top = max(top, label_size[1])
cv2.putText(frame, text, (left, top - 4), cv2.FONT_HERSHEY_SIMPLEX, 0.4,
COLOR_WHITE, 1)
def post_process(frame, outs, conf_threshold, nms_threshold):
frame_height = frame.shape[0]
frame_width = frame.shape[1]
# Scan through all the bounding boxes output from the network and keep only
# the ones with high confidence scores. Assign the box's class label as the
# class with the highest score.
confidences = []
boxes = []
final_boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold:
center_x = int(detection[0] * frame_width)
center_y = int(detection[1] * frame_height)
width = int(detection[2] * frame_width)
height = int(detection[3] * frame_height)
left = int(center_x - width / 2)
top = int(center_y - height / 2)
confidences.append(float(confidence))
boxes.append([left, top, width, height])
# Perform non maximum suppression to eliminate redundant
# overlapping boxes with lower confidences.
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold,
nms_threshold)
for i in indices:
i = i[0]
box = boxes[i]
left = box[0]
top = box[1]
width = box[2]
height = box[3]
final_boxes.append(box)
left, top, right, bottom = refined_box(left, top, width, height)
# draw_predict(frame, confidences[i], left, top, left + width,
# top + height)
draw_predict(frame, confidences[i], left, top, right, bottom)
return final_boxes
class FPS:
def __init__(self):
# store the start time, end time, and total number of frames
# that were examined between the start and end intervals
self._start = None
self._end = None
self._num_frames = 0
def start(self):
self._start = datetime.datetime.now()
return self
def stop(self):
self._end = datetime.datetime.now()
def update(self):
# increment the total number of frames examined during the
# start and end intervals
self._num_frames += 1
def elapsed(self):
# return the total number of seconds between the start and
# end interval
return (self._end - self._start).total_seconds()
def fps(self):
# compute the (approximate) frames per second
return self._num_frames / self.elapsed()
def refined_box(left, top, width, height):
right = left + width
bottom = top + height
original_vert_height = bottom - top
top = int(top + original_vert_height * 0.15)
bottom = int(bottom - original_vert_height * 0.05)
margin = ((bottom - top) - (right - left)) // 2
left = left - margin if (bottom - top - right + left) % 2 == 0 else left - margin - 1
right = right + margin
return left, top, right, bottom