Video-Summary/Application/VideoReader.py

143 lines
4.6 KiB
Python
Raw Normal View History

2022-08-15 10:20:28 +00:00
import multiprocessing
import os
2022-08-16 21:28:56 +00:00
import queue
import threading
2020-12-26 13:58:58 +00:00
import cv2
2022-01-09 19:25:44 +00:00
2020-10-08 20:26:29 +00:00
class VideoReader:
2022-09-11 09:25:36 +00:00
list_of_frames = None
2020-12-29 09:41:55 +00:00
w = None
h = None
2020-10-08 20:26:29 +00:00
2022-09-11 09:25:36 +00:00
def __init__(self, config, set_of_frames=None, multiprocess=False):
video_path = config["inputPath"]
if video_path is None:
raise Exception("ERROR: Video reader needs a video_path!")
self.video_path = video_path
self.last_frame = 0
2020-12-26 13:58:58 +00:00
# buffer data struct:
# buffer = Queue([(frameNumber, frame), ])
2022-08-16 21:28:56 +00:00
self.multiprocess = multiprocess
if multiprocess:
2022-09-11 09:25:36 +00:00
self.buffer = multiprocessing.Manager().Queue(config["videoBufferLength"])
2022-08-16 21:28:56 +00:00
else:
self.buffer = queue.Queue(config["videoBufferLength"])
2020-10-08 20:26:29 +00:00
self.stopped = False
2022-09-11 09:25:36 +00:00
self.get_wh()
self.calc_fps()
self.calc_length()
self.calc_start_time()
if set_of_frames is not None:
self.list_of_frames = sorted(set_of_frames)
2022-01-09 19:25:44 +00:00
2020-12-29 09:41:55 +00:00
def __enter__(self):
2022-09-11 09:25:36 +00:00
self.fill_buffer()
2020-12-29 09:41:55 +00:00
return self
2020-10-18 15:36:34 +00:00
2020-12-29 09:41:55 +00:00
def __exit__(self, type, value, traceback):
self.stop()
def stop(self):
self.thread.join()
2020-10-08 20:26:29 +00:00
def pop(self):
2022-09-11 09:25:36 +00:00
frame_number, frame = self.buffer.get(block=True)
2022-08-15 10:20:28 +00:00
if frame is None:
self.stopped = True
2022-09-11 09:25:36 +00:00
return frame_number, frame
2020-10-08 20:26:29 +00:00
2022-09-11 09:25:36 +00:00
def fill_buffer(self, list_of_frames=None):
self.end_frame = int(cv2.VideoCapture(self.video_path).get(cv2.CAP_PROP_FRAME_COUNT))
if list_of_frames is not None:
self.list_of_frames = list_of_frames
2020-12-26 13:58:58 +00:00
2022-08-16 21:28:56 +00:00
if self.multiprocess:
2022-09-11 09:25:36 +00:00
if self.list_of_frames is not None:
self.thread = multiprocessing.Process(target=self.read_frames_by_list, args=())
2022-08-16 21:28:56 +00:00
else:
2022-09-11 09:25:36 +00:00
self.thread = multiprocessing.Process(target=self.read_frames, args=())
else:
2022-09-11 09:25:36 +00:00
if self.list_of_frames is not None:
self.thread = threading.Thread(target=self.read_frames_by_list, args=())
2022-08-16 21:28:56 +00:00
else:
2022-09-11 09:25:36 +00:00
self.thread = threading.Thread(target=self.read_frames, args=())
2020-10-08 20:26:29 +00:00
self.thread.start()
2022-09-11 09:25:36 +00:00
def read_frames(self):
2022-01-09 19:25:44 +00:00
"""Reads video from start to finish"""
2022-09-11 09:25:36 +00:00
self.vc = cv2.VideoCapture(self.video_path)
while self.last_frame < self.end_frame:
2020-10-11 12:13:27 +00:00
res, frame = self.vc.read()
if res:
2022-09-11 09:25:36 +00:00
self.buffer.put((self.last_frame, frame))
self.last_frame += 1
self.buffer.put((self.last_frame, None))
2020-12-26 13:58:58 +00:00
2022-09-11 09:25:36 +00:00
def read_frames_by_list(self):
2022-01-09 19:25:44 +00:00
"""Reads all frames from a list of frame numbers"""
2022-09-11 09:25:36 +00:00
self.vc = cv2.VideoCapture(self.video_path)
self.vc.set(1, self.list_of_frames[0])
self.last_frame = self.list_of_frames[0]
self.end_frame = self.list_of_frames[-1]
2022-09-11 09:25:36 +00:00
while self.last_frame < self.end_frame:
if self.last_frame in self.list_of_frames:
2020-10-17 22:02:05 +00:00
res, frame = self.vc.read()
if res:
2022-09-11 09:25:36 +00:00
self.buffer.put((self.last_frame, frame))
2020-10-31 19:36:43 +00:00
else:
2022-08-15 10:20:28 +00:00
print("Couldn't read Frame")
2020-10-17 22:02:05 +00:00
# since the list is sorted the first element is always the lowest relevant framenumber
# [0,1,2,3,32,33,34,35,67,68,69]
2022-09-11 09:25:36 +00:00
self.list_of_frames.pop(0)
self.last_frame += 1
2020-10-08 20:26:29 +00:00
else:
2020-10-17 22:02:05 +00:00
# if current Frame number is not in list of Frames, we can skip a few frames
2022-09-11 09:25:36 +00:00
self.vc.set(1, self.list_of_frames[0])
self.last_frame = self.list_of_frames[0]
self.buffer.put((self.last_frame, None))
2020-12-26 13:58:58 +00:00
2022-09-11 09:25:36 +00:00
def video_ended(self):
2020-10-17 22:02:05 +00:00
if self.stopped and self.buffer.empty():
return True
else:
return False
2020-10-11 15:09:49 +00:00
2022-09-11 09:25:36 +00:00
def calc_fps(self):
self.fps = cv2.VideoCapture(self.video_path).get(cv2.CAP_PROP_FPS)
2022-09-11 09:25:36 +00:00
def get_fps(self):
2020-12-22 12:58:47 +00:00
if self.fps is None:
2022-09-11 09:25:36 +00:00
self.calc_fps()
return self.fps
2020-12-26 13:58:58 +00:00
2022-09-11 09:25:36 +00:00
def calc_length(self):
fc = int(cv2.VideoCapture(self.video_path).get(cv2.CAP_PROP_FRAME_COUNT))
self.length = fc / self.get_fps()
2020-12-22 12:58:47 +00:00
2022-09-11 09:25:36 +00:00
def get_length(self):
2020-12-22 12:58:47 +00:00
if self.length is None:
2022-09-11 09:25:36 +00:00
self.calc_length()
2020-12-22 12:58:47 +00:00
return self.length
2022-09-11 09:25:36 +00:00
def calc_start_time(self):
starttime = os.stat(self.video_path).st_mtime
length = self.get_length()
starttime = starttime - length
self.starttime = starttime
2022-09-11 09:25:36 +00:00
def get_start_time(self):
return self.starttime
2020-12-29 09:41:55 +00:00
2022-09-11 09:25:36 +00:00
def get_wh(self):
2022-01-09 19:25:44 +00:00
"""get width and height"""
2022-09-11 09:25:36 +00:00
vc = cv2.VideoCapture(self.video_path)
2020-12-29 09:41:55 +00:00
if self.w is None or self.h is None:
2022-08-15 10:20:28 +00:00
res, image = vc.read()
2020-12-29 09:41:55 +00:00
self.w = image.shape[1]
self.h = image.shape[0]
2022-08-15 10:21:20 +00:00
2020-12-29 09:41:55 +00:00
return (self.w, self.h)