-
Notifications
You must be signed in to change notification settings - Fork 1
/
cam_utils.py
116 lines (97 loc) · 2.81 KB
/
cam_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
# -*- coding: utf-8 -*-
"""
Cam utils file.
======================
Collection of useful functions for webcam detection.
"""
from utils import *
from config import *
from threading import Thread
class FPS:
"""
Class to get information about framerate.
"""
def __init__(self):
"""
Stores 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):
"""
Starts the timer.
:return: itself.
"""
self._start = datetime.now()
return self
def stop(self):
"""
Stops the timer.
:return: void.
"""
self._end = datetime.now()
def update(self):
"""
Increments the total number of frames examined during the start and end intervals
:return: void.
"""
self._numFrames += 1
def elapsed(self):
"""
Returns the total number of seconds between the start and end interval.
:return: elapsed time in seconds.
"""
return (self._end - self._start).total_seconds()
def fps(self):
"""
Computes the (approximate) frames per second.
:return: number of frame per second.
"""
return self._numFrames / self.elapsed()
class WebcamVideoStream:
"""
Class to retrieve the videostream of a capture device frame per frame.
"""
def __init__(self, src, width, height):
"""
# Initializes the video camera stream and read the first frame from the stream.
:param src: capture device identifier.
:param width: width.
:param height: height.
"""
self.stream = cv.VideoCapture(src)
self.stream.set(get_prop_id("FRAME_WIDTH"), width)
self.stream.set(get_prop_id("FRAME_HEIGHT"), height)
(self.grabbed, self.frame) = self.stream.read()
self.stopped = False
def start(self):
"""
Starts the Thread to read frames from the video stream.
:return: itself.
"""
Thread(target=self.update, args=()).start()
return self
def update(self):
"""
Keeps looping infinitely until the Thread is stopped.
:return: void.
"""
while True:
if self.stopped:
return
# Read the next frame from the stream.
(self.grabbed, self.frame) = self.stream.read()
def read(self):
"""
Returns the frame the most recently read.
:return: frame.
"""
return self.frame
def stop(self):
"""
Indicates that the Thread should be stopped.
:return: void.
"""
self.stopped = True