|
6 | 6 |
|
7 | 7 | # import the Queue class from Python 3 |
8 | 8 | if sys.version_info >= (3, 0): |
9 | | - from queue import Queue |
| 9 | + from queue import Queue, Full |
10 | 10 |
|
11 | 11 | # otherwise, import the Queue class for Python 2.7 |
12 | 12 | else: |
13 | 13 | from Queue import Queue |
14 | 14 |
|
15 | 15 |
|
| 16 | +class EndOfStreamException(Exception): |
| 17 | + pass |
| 18 | + |
| 19 | + |
16 | 20 | class FileVideoStream: |
17 | | - def __init__(self, path, transform=None, queue_size=128): |
| 21 | + |
| 22 | + def __init__(self, path, transform=None, queue_size=128, skip_frames=True, queue_put_timeout=0.5): |
18 | 23 | # initialize the file video stream along with the boolean |
19 | 24 | # used to indicate if the thread should be stopped or not |
| 25 | + self.queue_put_timeout = queue_put_timeout |
| 26 | + self.skip_frames = skip_frames |
20 | 27 | self.stream = cv2.VideoCapture(path) |
| 28 | + self.total_frames = self.stream.get(cv2.CAP_PROP_FRAME_COUNT) |
| 29 | + self.skipped_frames = 0 |
21 | 30 | self.stopped = False |
22 | 31 | self.transform = transform |
23 | 32 |
|
24 | 33 | # initialize the queue used to store frames read from |
25 | 34 | # the video file |
26 | 35 | self.Q = Queue(maxsize=queue_size) |
27 | | - # intialize thread |
| 36 | + # initialize thread |
28 | 37 | self.thread = Thread(target=self.update, args=()) |
29 | 38 | self.thread.daemon = True |
30 | 39 |
|
| 40 | + def check_eos(self): |
| 41 | + if self.stream.get(cv2.CAP_PROP_POS_FRAMES) == self.total_frames: |
| 42 | + # If the number of captured frames is equal to the total number of frames, |
| 43 | + # we stop |
| 44 | + self.stop() |
| 45 | + raise EndOfStreamException("End of stream") |
| 46 | + |
31 | 47 | def start(self): |
32 | 48 | # start a thread to read frames from the file video stream |
33 | 49 | self.thread.start() |
34 | 50 | return self |
35 | 51 |
|
| 52 | + def get_frame(self): |
| 53 | + # grab the current frame |
| 54 | + flag, frame = self.stream.read() |
| 55 | + |
| 56 | + while not flag and self.skip_frames: |
| 57 | + self.check_eos() |
| 58 | + # if self.skipped_frames == 0: |
| 59 | + # print(f"Skipping frame(s)") |
| 60 | + self.skipped_frames += 1 |
| 61 | + flag, frame = self.stream.read() |
| 62 | + if self.skipped_frames > 0: |
| 63 | + # print(f"Resuming video...") |
| 64 | + self.skipped_frames = 0 |
| 65 | + |
| 66 | + return flag, frame |
| 67 | + |
36 | 68 | def update(self): |
37 | 69 | # keep looping infinitely |
38 | 70 | while True: |
39 | 71 | # if the thread indicator variable is set, stop the |
40 | 72 | # thread |
41 | 73 | if self.stopped: |
42 | 74 | break |
43 | | - |
44 | | - # otherwise, ensure the queue has room in it |
45 | | - if not self.Q.full(): |
| 75 | + try: |
46 | 76 | # read the next frame from the file |
47 | | - (grabbed, frame) = self.stream.read() |
48 | | - |
| 77 | + grabbed, frame = self.get_frame() |
49 | 78 | # if the `grabbed` boolean is `False`, then we have |
50 | 79 | # reached the end of the video file |
51 | 80 | if not grabbed: |
52 | 81 | self.stopped = True |
53 | | - |
| 82 | + raise EndOfStreamException() |
| 83 | + |
54 | 84 | # if there are transforms to be done, might as well |
55 | 85 | # do them on producer thread before handing back to |
56 | | - # consumer thread. ie. Usually the producer is so far |
| 86 | + # consumer thread. i.e. Usually the producer is so far |
57 | 87 | # ahead of consumer that we have time to spare. |
58 | 88 | # |
59 | 89 | # Python is not parallel but the transform operations |
60 | | - # are usually OpenCV native so release the GIL. |
| 90 | + # are typically OpenCV native so release the GIL. |
61 | 91 | # |
62 | 92 | # Really just trying to avoid spinning up additional |
63 | 93 | # native threads and overheads of additional |
64 | 94 | # producer/consumer queues since this one was generally |
65 | 95 | # idle grabbing frames. |
66 | 96 | if self.transform: |
67 | 97 | frame = self.transform(frame) |
| 98 | + while True: |
| 99 | + try: |
| 100 | + # try to put it into the queue |
| 101 | + self.Q.put(frame, True, self.queue_put_timeout) |
| 102 | + break |
| 103 | + except Full: |
| 104 | + # after timeout seconds check if we are still not stopped |
| 105 | + if self.stopped: |
| 106 | + raise EndOfStreamException() |
| 107 | + except EndOfStreamException: |
| 108 | + # if we got stopped or reached eos |
| 109 | + self.stopped = True |
| 110 | + break |
| 111 | + self.stream.release() |
68 | 112 |
|
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 |
| 113 | + def dim(self): |
| 114 | + width = int(self.stream.get(cv2.CAP_PROP_FRAME_WIDTH)) |
| 115 | + height = int(self.stream.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
73 | 116 |
|
74 | | - self.stream.release() |
| 117 | + return [width, height] |
75 | 118 |
|
76 | 119 | def read(self): |
77 | 120 | # return next frame in the queue |
|
0 commit comments