2020-10-08 20:26:29 +00:00
|
|
|
|
|
|
|
|
import multiprocessing
|
|
|
|
|
import cv2
|
|
|
|
|
from time import sleep
|
|
|
|
|
from queue import Queue
|
|
|
|
|
import threading
|
2020-10-18 15:36:34 +00:00
|
|
|
from Application.Config import Config
|
2020-10-09 21:59:04 +00:00
|
|
|
|
|
|
|
|
|
2020-10-08 20:26:29 +00:00
|
|
|
class VideoReader:
|
2020-10-09 21:59:04 +00:00
|
|
|
listOfFrames = None
|
2020-10-18 15:36:34 +00:00
|
|
|
w = 0
|
|
|
|
|
h = 0
|
2020-10-08 20:26:29 +00:00
|
|
|
|
2020-10-11 15:09:49 +00:00
|
|
|
def __init__(self, config, setOfFrames = None):
|
|
|
|
|
videoPath = config["inputPath"]
|
2020-10-08 20:26:29 +00:00
|
|
|
if videoPath is None:
|
2020-10-18 15:36:34 +00:00
|
|
|
print("ERROR: Video reader needs a videoPath!")
|
2020-10-08 20:26:29 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
self.videoPath = videoPath
|
|
|
|
|
self.lastFrame = 0
|
2020-10-13 22:16:39 +00:00
|
|
|
#buffer = Queue([(frameNumber, frame), ])
|
2020-10-11 15:09:49 +00:00
|
|
|
self.buffer = Queue(config["videoBufferLength"])
|
2020-10-08 20:26:29 +00:00
|
|
|
self.vc = cv2.VideoCapture(videoPath)
|
|
|
|
|
self.stopped = False
|
2020-10-18 15:36:34 +00:00
|
|
|
self.getWH()
|
|
|
|
|
if setOfFrames is not None:
|
|
|
|
|
self.listOfFrames = sorted(setOfFrames)
|
|
|
|
|
|
|
|
|
|
def getWH(self):
|
2020-10-08 20:26:29 +00:00
|
|
|
res, image = self.vc.read()
|
|
|
|
|
self.w = image.shape[1]
|
|
|
|
|
self.h = image.shape[0]
|
2020-10-18 15:36:34 +00:00
|
|
|
return (self.w, self.h)
|
2020-10-08 20:26:29 +00:00
|
|
|
|
|
|
|
|
def pop(self):
|
|
|
|
|
return self.buffer.get(block=True)
|
|
|
|
|
|
|
|
|
|
def get(self):
|
|
|
|
|
return self.buffer[-1]
|
|
|
|
|
|
|
|
|
|
def fillBuffer(self):
|
|
|
|
|
if self.buffer.full():
|
|
|
|
|
print("VideoReader::fillBuffer was called when buffer was full.")
|
|
|
|
|
self.endFrame = int(self.vc.get(cv2.CAP_PROP_FRAME_COUNT))
|
2020-10-09 21:59:04 +00:00
|
|
|
|
|
|
|
|
#self.endFrame = 10*60*30
|
|
|
|
|
if self.listOfFrames is not None:
|
|
|
|
|
self.thread = threading.Thread(target=self.readFramesByList, args=())
|
|
|
|
|
else:
|
|
|
|
|
self.thread = threading.Thread(target=self.readFrames, args=())
|
2020-10-08 20:26:29 +00:00
|
|
|
self.thread.start()
|
|
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
|
self.thread.join()
|
|
|
|
|
self.vc.release()
|
|
|
|
|
|
|
|
|
|
def readFrames(self):
|
|
|
|
|
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
|
|
|
|
|
|
2020-10-09 21:59:04 +00:00
|
|
|
self.stopped = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def readFramesByList(self):
|
|
|
|
|
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))
|
|
|
|
|
# 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]
|
|
|
|
|
|
2020-10-08 20:26:29 +00:00
|
|
|
self.stopped = True
|
|
|
|
|
|
|
|
|
|
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 getFPS(self):
|
|
|
|
|
return self.vc.get(cv2.CAP_PROP_FPS)
|