-
Notifications
You must be signed in to change notification settings - Fork 1
/
Videoq.py
47 lines (39 loc) · 1.15 KB
/
Videoq.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
import cv2, queue, threading, time
# bufferless VideoCapture
class VideoCapture:
def __init__(self, name):
self.cap = cv2.VideoCapture(name)
self.q = queue.Queue()
t = threading.Thread(target=self._reader)
t.daemon = True
t.start()
# read frames as soon as they are available, keeping only most recent one
def _reader(self):
while True:
ret, frame = self.cap.read()
if not ret:
break
if not self.q.empty():
try:
self.q.get_nowait() # discard previous (unprocessed) frame
except queue.Empty:
pass
self.q.put(frame)
def read(self):
return self.q.get()
def get(self):
return self.cap.get(cv2.CAP_PROP_FPS)
def release(self):
self.cap.release()
if __name__ == '__main__':
# cap = cv2.VideoCapture("rtmp://127.0.0.1:9999/live/test")
cap2=VideoCapture("rtmp://127.0.0.1:9999/live/test")
while True:
# time.sleep() # simulate time between events
frame1 = cap2.read()
print(frame1.shape)
# _,frame = cap.read()
# cv2.imshow("frame", frame)
cv2.imshow("frame2",frame1)
if chr(cv2.waitKey(1)&255) == 'q':
break