-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.py
More file actions
28 lines (25 loc) · 858 Bytes
/
thread.py
File metadata and controls
28 lines (25 loc) · 858 Bytes
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
import cv2, threading, queue
class ThreadingClass:
# initiate threading class
def __init__(self, name):
self.cap = cv2.VideoCapture(name)
# define an empty queue and thread
self.q = queue.Queue()
t = threading.Thread(target=self._reader)
t.daemon = True
t.start()
# read the frames as soon as they are available, discard any unprocessed frames;
# this approach removes OpenCV's internal buffer and reduces the frame lag
def _reader(self):
while True:
(ret, frame) = self.cap.read() # read the frames and ---
if not ret:
break
if not self.q.empty():
try:
self.q.get_nowait()
except queue.Empty:
pass
self.q.put(frame) # --- store them in a queue (instead of the buffer)
def read(self):
return self.q.get() # fetch frames from the queue one by one