-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
86 lines (71 loc) · 2.5 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
import datetime
from threading import Thread
import cv2
import time
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 update(self):
# increment the total number of frames examined during the
# start and end intervals
self._numFrames += 1
# self._end = datetime.datetime.now()
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()
def stop(self):
# indicate that the thread should be stopped
self._end = datetime.datetime.now()
class VideoStream:
def __init__(self, src=0, fps=False):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src)
(self.grabbed, self.frame) = self.stream.read()
if not self.grabbed:
return
if fps:
self.fps = FPS()
else:
self.fps = None
# 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()
if self.fps:
self.fps.start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while self.grabbed:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
if self.fps:
self.fps.update()
# 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.stream.isOpened(), self.frame
def release(self):
# indicate that the thread should be stopped
self.stopped = True
self.stream.release()
if self.fps:
self.fps.stop()
print("Approximate fps: {}".format(self.fps.fps()))