-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_stream.py
More file actions
43 lines (38 loc) · 1.23 KB
/
Copy pathvideo_stream.py
File metadata and controls
43 lines (38 loc) · 1.23 KB
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
import cv2
from threading import Thread, Event
from queue import Queue, Empty
class VideoStream:
def __init__(self, src, batch_size=512):
self.stream = cv2.VideoCapture(src)
self.stopped = Event()
self.queue = Queue(maxsize=1024)
self.batch_size = batch_size
self.thread = None
def start(self):
self.thread = Thread(target=self._update, daemon=True)
self.thread.start()
return self
def _update(self):
batch = []
while not self.stopped.is_set():
if not self.queue.full():
grabbed, frame = self.stream.read()
if not grabbed:
if batch:
self.queue.put(batch)
self.queue.put(None) # Signal end of stream
break
batch.append(frame)
if len(batch) == self.batch_size:
self.queue.put(batch)
batch = []
def read(self):
try:
return self.queue.get(timeout=1)
except Empty:
return []
def stop(self):
self.stopped.set()
if self.thread is not None:
self.thread.join()
self.stream.release()