Created
November 12, 2019 12:30
-
-
Save gtindo/6192eb9c78425489edfb322a961d7b5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileVideoStream: | |
def __init__(self, path, workers=6, queue_size=128): | |
# Get number of frame | |
self.nb_frame = imutils.video.count_frames(path) | |
# Compute number of frame per workers | |
self.bias = math.ceil(self.nb_frame / workers) | |
# self.stream = cv2.VideoCapture(path) | |
self.streams = {} | |
for i in range(1, workers): | |
start = (i * self.bias) - self.bias | |
stream = cv2.VideoCapture(path) | |
stream.set(cv2.CAP_PROP_POS_FRAMES, start) | |
self.streams[start] = { | |
"stream": stream, | |
"queue": Queue(maxsize=queue_size), | |
"stopped": False | |
} | |
print(self.streams[start]["stream"]) | |
def start(self): | |
t = {} | |
# start multiple thread to read frames from the file video stream | |
for key in self.streams: | |
t[key] = Thread(target=self.update, args=(key, )) | |
t[key].daemon = True | |
t[key].start() | |
return self | |
def update(self, index): | |
stream = self.streams[index]["stream"] | |
queue = self.streams[index]["queue"] | |
stopped = self.streams[index]["stopped"] | |
i = index | |
last_frame_index = index + self.bias | |
while True: | |
if stopped or i > last_frame_index: | |
return | |
i = i + 1 | |
if not queue.full(): | |
grabbed, frame = stream.read() | |
if not grabbed: | |
self.stop(index) | |
return | |
# add the frame to the queue | |
queue.put(frame) | |
def stop(self, index): | |
# indicate that thread should stop | |
self.streams[index]["stopped"] = True | |
def read(self, index): | |
# return next frame in the queue | |
return self.streams[index]["queue"].get() | |
def more(self, index): | |
# Return True if there are still frames in the queue | |
return self.streams[index]["queue"].qsize() > 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment