Video-Summary/Application/VideoReader.py

135 lines
4.2 KiB
Python
Raw Normal View History

2022-08-15 10:20:28 +00:00
import multiprocessing
2020-12-26 13:58:58 +00:00
import cv2
2020-10-08 20:26:29 +00:00
import threading
import os
2022-01-09 19:25:44 +00:00
2020-10-08 20:26:29 +00:00
class VideoReader:
listOfFrames = None
2020-12-29 09:41:55 +00:00
w = None
h = None
2020-10-08 20:26:29 +00:00
2020-12-26 13:58:58 +00:00
def __init__(self, config, setOfFrames=None):
2020-10-11 15:09:49 +00:00
videoPath = config["inputPath"]
2020-10-08 20:26:29 +00:00
if videoPath is None:
2020-12-26 13:58:58 +00:00
raise Exception("ERROR: Video reader needs a videoPath!")
2020-10-08 20:26:29 +00:00
self.videoPath = videoPath
self.lastFrame = 0
2020-12-26 13:58:58 +00:00
# buffer data struct:
# buffer = Queue([(frameNumber, frame), ])
2022-08-15 10:20:28 +00:00
self.buffer = multiprocessing.Queue(config["videoBufferLength"])
#self.vc = cv2.VideoCapture(videoPath)
2020-10-08 20:26:29 +00:00
self.stopped = False
2020-11-01 16:43:05 +00:00
self.getWH()
self.calcFPS()
2020-12-22 12:58:47 +00:00
self.calcLength()
self.calcStartTime()
2020-10-18 15:36:34 +00:00
if setOfFrames is not None:
2020-12-26 13:58:58 +00:00
self.listOfFrames = sorted(setOfFrames)
2022-01-09 19:25:44 +00:00
2020-12-29 09:41:55 +00:00
def __enter__(self):
self.fillBuffer()
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()
self.vc.release()
2020-10-08 20:26:29 +00:00
def pop(self):
2022-08-15 10:20:28 +00:00
frameNumber, frame = self.buffer.get(block=True)
if frame is None:
self.stopped = True
return frameNumber, frame
2020-10-08 20:26:29 +00:00
2020-12-18 20:27:19 +00:00
def fillBuffer(self, listOfFrames=None):
2022-08-15 10:20:28 +00:00
self.endFrame = int(cv2.VideoCapture(self.videoPath).get(cv2.CAP_PROP_FRAME_COUNT))
2020-12-18 20:27:19 +00:00
if listOfFrames is not None:
self.listOfFrames = listOfFrames
2020-12-26 13:58:58 +00:00
if self.listOfFrames is not None:
2022-08-15 10:20:28 +00:00
self.thread = multiprocessing.Process(target=self.readFramesByList, args=())
else:
2022-08-15 10:20:28 +00:00
self.thread = multiprocessing.Process(target=self.readFrames, args=())
2020-10-08 20:26:29 +00:00
self.thread.start()
def readFrames(self):
2022-01-09 19:25:44 +00:00
"""Reads video from start to finish"""
2022-08-15 10:20:28 +00:00
self.vc = cv2.VideoCapture(self.videoPath)
2020-10-08 20:26:29 +00:00
while self.lastFrame < self.endFrame:
2020-10-11 12:13:27 +00:00
res, frame = self.vc.read()
if res:
self.buffer.put((self.lastFrame, frame))
self.lastFrame += 1
2022-08-15 10:20:28 +00:00
self.buffer.put((self.lastFrame, None))
2020-12-26 13:58:58 +00:00
def readFramesByList(self):
2022-01-09 19:25:44 +00:00
"""Reads all frames from a list of frame numbers"""
2022-08-15 10:20:28 +00:00
self.vc = cv2.VideoCapture(self.videoPath)
self.vc.set(1, self.listOfFrames[0])
self.lastFrame = self.listOfFrames[0]
self.endFrame = self.listOfFrames[-1]
while self.lastFrame < self.endFrame:
2020-10-17 22:02:05 +00:00
if self.lastFrame in self.listOfFrames:
res, frame = self.vc.read()
if res:
self.buffer.put((self.lastFrame, 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]
self.listOfFrames.pop(0)
self.lastFrame += 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
self.vc.set(1, self.listOfFrames[0])
self.lastFrame = self.listOfFrames[0]
2022-08-15 10:20:28 +00:00
self.buffer.put((self.lastFrame, None))
2020-12-26 13:58:58 +00:00
2020-10-08 20:26:29 +00:00
def videoEnded(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
def calcFPS(self):
2022-08-15 10:20:28 +00:00
self.fps = cv2.VideoCapture(self.videoPath).get(cv2.CAP_PROP_FPS)
2020-10-11 15:09:49 +00:00
def getFPS(self):
2020-12-22 12:58:47 +00:00
if self.fps is None:
self.calcFPS()
return self.fps
2020-12-26 13:58:58 +00:00
2020-12-22 12:58:47 +00:00
def calcLength(self):
2022-08-15 10:20:28 +00:00
fc = int(cv2.VideoCapture(self.videoPath).get(cv2.CAP_PROP_FRAME_COUNT))
2020-12-22 12:58:47 +00:00
self.length = fc / self.getFPS()
def getLength(self):
if self.length is None:
self.calcLength()
return self.length
def calcStartTime(self):
starttime = os.stat(self.videoPath).st_mtime
2020-12-22 12:58:47 +00:00
length = self.getLength()
starttime = starttime - length
self.starttime = starttime
def getStartTime(self):
return self.starttime
2020-12-29 09:41:55 +00:00
def getWH(self):
2022-01-09 19:25:44 +00:00
"""get width and height"""
2022-08-15 10:20:28 +00:00
vc = cv2.VideoCapture(self.videoPath)
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:20:28 +00:00
2020-12-29 09:41:55 +00:00
return (self.w, self.h)