-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebcamVideoStream.py
49 lines (39 loc) · 1.62 KB
/
WebcamVideoStream.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
# https://www.pyimagesearch.com/2015/12/21/increasing-webcam-fps-with-python-and-opencv/
# import the necessary packages
from threading import Thread
import cv2
class WebcamVideoStream:
def __init__(self, src=0):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src)
self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
self.stream.set(cv2.CAP_PROP_EXPOSURE, -6.0)
self.stream.set(cv2.CAP_PROP_FPS, 30)
self.stream.set(cv2.CAP_PROP_GAIN, 0)
self.stream.set(cv2.CAP_PROP_BRIGHTNESS, 64)
self.stream.set(cv2.CAP_PROP_CONTRAST, 64)
self.stream.set(cv2.CAP_PROP_SATURATION, 64)
(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