-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
160 lines (123 loc) · 4.28 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
154
155
156
157
158
159
160
# From http://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/
import cv2
import datetime
from threading import Thread
import math
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._numFrames = 0
def start(self):
# start the timer
self._start = datetime.datetime.now()
return self
def stop(self):
# stop the timer
self._end = datetime.datetime.now()
def update(self):
# increment the total number of frames examined during the
# start and end intervals
self._numFrames += 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._numFrames / self.elapsed()
class WebcamVideoStream:
def __init__(self, src, width=None, height=None):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src)
if width is not None:
self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, width)
if height is not None:
self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
(self.grabbed, self.frame) = self.stream.read()
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
def start(self):
# start the thread to read frames from the video stream
Thread(target=self.update, args=()).start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
# otherwise, read the next frame from the stream
(self.grabbed, self.frame) = self.stream.read()
def read(self):
# return the frame most recently read
return self.frame
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
@property
def width(self):
return self.stream.get(cv2.CAP_PROP_FRAME_WIDTH)
@property
def height(self):
return self.stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
def iou(bb1, bb2):
"""
Calculate the Intersection over Union (IoU) of two bounding boxes.
Parameters
----------
bb1 : list
['x1', 'x2', 'y1', 'y2']
The (x1, y1) position is at the top left corner,
the (x2, y2) position is at the bottom right corner
bb2 : list
['x1', 'x2', 'y1', 'y2']
The (x1, y2) position is at the top left corner,
the (x2, y2) position is at the bottom right corner
Returns
-------
float
in [0, 1]
"""
x1, y1, w1, h1 = bb1
x2, y2, w2, h2 = bb2
# determine the coordinates of the intersection rectangle
x_left = max(x1, x2)
y_top = max(y1, y2)
x_right = min(x1 + w1, x2 + w2)
y_bottom = min(y1 + h1, y2 + h2)
if x_right < x_left or y_bottom < y_top:
return 0.0
# The intersection of two axis-aligned bounding boxes is always an
# axis-aligned bounding box
intersection_area = (x_right - x_left) * (y_bottom - y_top)
# compute the area of both AABBs
bb1_area = w1 * h1
bb2_area = w2 * h2
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = intersection_area / float(bb1_area + bb2_area - intersection_area)
assert iou >= 0.0
assert iou <= 1.0
return iou
def enlarge_roi(frm, bbox, scale=2):
height, width, _ = frm.shape
x, y, w, h = bbox
factor = math.sqrt(scale)
cx, cy = int(x + w / 2), int(y + h / 2)
w2, h2 = int(w * factor), int(h * factor)
x2, y2 = int(cx - w2 / 2), int(cy - h2 / 2)
if x2 < 0:
x2 = 0
if y2 < 0:
y2 = 0
if x2 + w2 >= width:
w2 = width - x2
if y2 + h2 >= height:
h2 = height - y2
return x2, y2, w2, h2