Video-Summary/Application/Exporter.py

154 lines
5.8 KiB
Python
Raw Normal View History

import imageio
2020-09-25 17:52:41 +00:00
import imutils
import numpy as np
2020-10-18 15:36:34 +00:00
from Application.Layer import Layer
2020-09-25 17:52:41 +00:00
import cv2
2020-10-18 15:36:34 +00:00
from Application.VideoReader import VideoReader
2020-10-16 08:36:52 +00:00
import pickle
2020-11-11 17:37:25 +00:00
import time
2020-09-20 20:01:54 +00:00
class Exporter:
fps = 30
2020-09-24 13:47:49 +00:00
2020-10-11 15:09:49 +00:00
def __init__(self, config):
self.footagePath = config["inputPath"]
self.outputPath = config["outputPath"]
self.resizeWidth = config["resizeWidth"]
self.config = config
print("Exporter initiated")
2020-09-24 13:47:49 +00:00
2020-11-08 15:28:47 +00:00
def export(self, layers, contours, raw = True, overlayed = True):
2020-10-23 22:14:43 +00:00
if raw:
2020-11-08 15:28:47 +00:00
self.exportRawData(layers, contours)
if overlayed:
2020-10-23 22:14:43 +00:00
self.exportOverlayed(layers)
2020-11-08 15:28:47 +00:00
else:
self.exportLayers(layers)
2020-10-23 22:14:43 +00:00
2020-09-25 17:52:41 +00:00
2020-11-11 17:37:25 +00:00
def exportLayers(self, layers):
2020-10-11 12:13:27 +00:00
listOfFrames = self.makeListOfFrames(layers)
2020-10-11 15:09:49 +00:00
videoReader = VideoReader(self.config, listOfFrames)
2020-10-11 12:13:27 +00:00
videoReader.fillBuffer()
maxLength = self.getMaxLengthOfLayers(layers)
2020-10-11 15:09:49 +00:00
underlay = cv2.VideoCapture(self.footagePath).read()[1]
2020-10-11 12:13:27 +00:00
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
exportFrame = 0
2020-10-11 15:09:49 +00:00
self.fps = videoReader.getFPS()
writer = imageio.get_writer(self.outputPath, fps=self.fps)
2020-11-11 17:37:25 +00:00
start = time.time()
for i, layer in enumerate(layers):
2020-11-21 18:13:17 +00:00
print(f"\r {i}/{len(layers)} {round(i/len(layers)*100,2)}% {round((time.time() - start), 2)}s", end='\r')
2020-11-11 17:37:25 +00:00
if len(layer.bounds[0]) == 0:
2020-09-29 20:52:36 +00:00
continue
2020-11-21 18:13:17 +00:00
if layer.stats["dev"] < 5:
continue
2020-10-11 12:13:27 +00:00
2020-11-11 17:37:25 +00:00
listOfFrames = self.makeListOfFrames([layer])
videoReader = VideoReader(self.config, listOfFrames)
videoReader.fillBuffer()
while not videoReader.videoEnded():
frameCount, frame = videoReader.pop()
if frameCount % (60*self.fps) == 0:
print("Minutes processed: ", frameCount/(60*self.fps))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame2 = np.copy(underlay)
for (x, y, w, h) in layer.bounds[frameCount - layer.startFrame]:
if x is None:
continue
factor = videoReader.w / self.resizeWidth
x = int(x * factor)
y = int(y * factor)
w = int(w * factor)
h = int(h * factor)
frame2[y:y+h, x:x+w] = np.copy(frame[y:y+h, x:x+w])
cv2.putText(frame2, str(i) + " " + str(int(frameCount/self.fps)), (int(x+w/2), int(y+h/2)), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255), 2)
cv2.putText(frame2, str(layer.stats["avg"]) + " " + str(layer.stats["var"]) + " " + str(layer.stats["dev"]), (int(500), int(500)), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,0,255), 2)
2020-11-11 17:37:25 +00:00
writer.append_data(frame2)
videoReader.thread.join()
2020-11-21 18:13:17 +00:00
videoReader.vc.release()
writer.close()
2020-10-11 15:09:49 +00:00
def exportOverlayed(self, layers):
listOfFrames = self.makeListOfFrames(layers)
2020-10-11 15:09:49 +00:00
videoReader = VideoReader(self.config, listOfFrames)
videoReader.fillBuffer()
2020-09-30 17:22:10 +00:00
maxLength = self.getMaxLengthOfLayers(layers)
2020-10-11 15:09:49 +00:00
underlay = cv2.VideoCapture(self.footagePath).read()[1]
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
frames = [underlay]*maxLength
exportFrame = 0
2020-09-30 17:22:10 +00:00
2020-10-13 22:16:39 +00:00
while not videoReader.videoEnded():
frameCount, frame = videoReader.pop()
if frameCount % (60*self.fps) == 0:
2020-11-21 18:13:17 +00:00
print("Minutes processed: ", frameCount/(60*self.fps), end="\r")
if frame is None:
print("ContourExtractor: frame was None")
continue
2020-10-13 22:16:39 +00:00
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
2020-09-30 17:22:10 +00:00
for layer in layers:
if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount:
2020-10-11 20:54:12 +00:00
for (x, y, w, h) in layer.bounds[frameCount - layer.startFrame]:
2020-10-18 15:36:34 +00:00
if x is None:
break
2020-10-11 20:54:12 +00:00
factor = videoReader.w / self.resizeWidth
x = int(x * factor)
y = int(y * factor)
w = int(w * factor)
h = int(h * factor)
# if exportFrame as index instead of frameCount - layer.startFrame then we have layer after layer
frame2 = frames[frameCount - layer.startFrame]
2020-10-23 22:14:43 +00:00
frame2[y:y+h, x:x+w] = frame2[y:y+h, x:x+w]/2 + frame[y:y+h, x:x+w]/2
2020-10-17 22:02:05 +00:00
2020-10-11 20:54:12 +00:00
frames[frameCount - layer.startFrame] = np.copy(frame2)
2020-10-17 22:02:05 +00:00
cv2.putText(frames[frameCount - layer.startFrame], str(int(frameCount/self.fps)), (int(x+w/2), int(y+h/2)), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255), 2)
videoReader.thread.join()
2020-11-21 18:13:17 +00:00
videoReader.vc.release()
2020-10-11 15:09:49 +00:00
self.fps = videoReader.getFPS()
fps = self.fps
2020-10-11 15:09:49 +00:00
writer = imageio.get_writer(self.outputPath, fps=fps)
for frame in frames:
writer.append_data(frame)
writer.close()
2020-09-30 17:22:10 +00:00
2020-11-08 15:28:47 +00:00
def exportRawData(self, layers, contours):
with open(self.config["importPath"], "wb+") as file:
pickle.dump((layers, contours), file)
2020-10-16 08:36:52 +00:00
2020-09-30 17:22:10 +00:00
def getMaxLengthOfLayers(self, layers):
maxLength = 0
for layer in layers:
if layer.getLength() > maxLength:
maxLength = layer.getLength()
return maxLength
def makeListOfFrames(self, layers):
'''Returns set of all Frames which are relavant to the Layers'''
frameNumbers = set()
for layer in layers:
frameNumbers.update(
list(range(layer.startFrame, layer.startFrame + len(layer.bounds))))
2020-10-19 19:35:15 +00:00
return sorted(list(frameNumbers))