| 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 | |
| 74 | self.stream.release() |
| 75 | |
| 76 | def read(self): |
| 77 | # return next frame in the queue |