-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththreadedcam.py
38 lines (31 loc) · 1.14 KB
/
threadedcam.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
from threading import Thread
import cv2, time
# https://stackoverflow.com/questions/58293187/opencv-real-time-streaming-video-capture-is-slow-how-to-drop-frames-or-get-sync
class ThreadedCamera(object):
def __init__(self, src=0):
self.capture = cv2.VideoCapture(src)
self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 2)
# FPS = 1/X
# X = desired FPS
self.FPS = 1/30
self.FPS_MS = int(self.FPS * 1000)
# Start frame retrieval thread
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
time.sleep(self.FPS)
def get_frame(self):
cv2.imshow('frame', self.frame)
return cv2.waitKey(self.FPS_MS)
if __name__ == '__main__':
src = 'http://wzmedia.dot.ca.gov:1935/D3/80_donner_lake.stream/index.m3u8'
threaded_camera = ThreadedCamera(src)
while True:
try:
key = threaded_camera.get_frame()
except AttributeError:
pass