MCPcopy Index your code
hub / github.com/PyImageSearch/imutils / FileVideoStream

Class FileVideoStream

imutils/video/filevideostream.py:16–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14
15
16class FileVideoStream:
17 def __init__(self, path, transform=None, queue_size=128):
18 # initialize the file video stream along with the boolean
19 # used to indicate if the thread should be stopped or not
20 self.stream = cv2.VideoCapture(path)
21 self.stopped = False
22 self.transform = transform
23
24 # initialize the queue used to store frames read from
25 # the video file
26 self.Q = Queue(maxsize=queue_size)
27 # intialize thread
28 self.thread = Thread(target=self.update, args=())
29 self.thread.daemon = True
30
31 def start(self):
32 # start a thread to read frames from the file video stream
33 self.thread.start()
34 return self
35
36 def update(self):
37 # keep looping infinitely
38 while True:
39 # if the thread indicator variable is set, stop the
40 # thread
41 if self.stopped:
42 break
43
44 # otherwise, ensure the queue has room in it
45 if not self.Q.full():
46 # read the next frame from the file
47 (grabbed, frame) = self.stream.read()
48
49 # if the `grabbed` boolean is `False`, then we have
50 # reached the end of the video file
51 if not grabbed:
52 self.stopped = True
53
54 # if there are transforms to be done, might as well
55 # do them on producer thread before handing back to
56 # consumer thread. ie. Usually the producer is so far
57 # ahead of consumer that we have time to spare.
58 #
59 # Python is not parallel but the transform operations
60 # are usually OpenCV native so release the GIL.
61 #
62 # Really just trying to avoid spinning up additional
63 # native threads and overheads of additional
64 # producer/consumer queues since this one was generally
65 # idle grabbing frames.
66 if self.transform:
67 frame = self.transform(frame)
68
69 # add the frame to the queue
70 self.Q.put(frame)
71 else:
72 time.sleep(0.1) # Rest for 10ms, we have a full queue
73

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…