-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileVideoStream.py
67 lines (60 loc) · 1.88 KB
/
FileVideoStream.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from threading import Thread
import cv2
import time
import psutil
from queue import Queue
class FileVideoStream():
def __init__(self, path, queueSize=1000):
# initialize the file video stream along with the boolean
# used to indicate if the thread should be stopped or not
self.stream = cv2.VideoCapture(path)
self.w = int(self.stream.get(3))
self.h = int(self.stream.get(4))
self.stopped = False
# initialize the queue used to store frames read from
# the video file
self.Q = Queue(maxsize=queueSize)
def start(self):
# start a thread to read frames from the file video stream
t = Thread(target=self.update, args=())
t.daemon = True
t.start()
return self, (self.w, self.h)
def update(self):
# keep looping infinitely
while True:
time.sleep(0.0001)
virtualMemoryInfo = psutil.virtual_memory()
availableMemory = virtualMemoryInfo.available
MEMORY_WARNING = 200*1024*1024
# if the thread indicator variable is set, stop the
# thread
if self.stopped:
return
# otherwise, ensure the queue has room in it
if not self.Q.full():
if availableMemory > MEMORY_WARNING:
# read the next frame from the file
(grabbed, frame) = self.stream.read()
if grabbed:
vid_timer = int(self.stream.get(cv2.CAP_PROP_POS_MSEC))
# if the `grabbed` boolean is `False`, then we have
# reached the end of the video file
if not grabbed:
self.stop()
return
# add the frame to the queue
self.Q.put((grabbed,frame,vid_timer))
else:
print("memory warning!")
def read(self):
# return next frame in the queue
return self.Q.get()
def more(self):
# return True if there are still frames in the queue
return self.Q.qsize() > 0
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
def set_timer(self, min_vid_timer):
self.stream.set(cv2.CAP_PROP_POS_MSEC, min_vid_timer)