reworked layers

This commit is contained in:
Askill 2020-10-04 00:27:36 +02:00
parent de85715787
commit 11cae0e6f5
14 changed files with 76 additions and 31 deletions

View File

@ -15,9 +15,9 @@ class ContourExtractor:
#X = {frame_number: [(contour, (x,y,w,h)), ...], } #X = {frame_number: [(contour, (x,y,w,h)), ...], }
extractedContours = dict() extractedContours = dict()
min_area = 100 min_area = 500
max_area = 50000 max_area = 5000
threashold = 12 threashold = 20
xDim = 0 xDim = 0
yDim = 0 yDim = 0
@ -27,7 +27,7 @@ class ContourExtractor:
def __init__(self): def __init__(self):
print("ContourExtractor initiated") print("ContourExtractor initiated")
def extractContours(self, videoPath): def extractContours(self, videoPath, resizeWidth):
min_area = self.min_area min_area = self.min_area
max_area = self.max_area max_area = self.max_area
threashold = self.threashold threashold = self.threashold
@ -49,8 +49,8 @@ class ContourExtractor:
print("ContourExtractor: frame was None") print("ContourExtractor: frame was None")
break break
frame = imutils.resize(frame, width=500) frame = imutils.resize(frame, width=resizeWidth)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0) gray = cv2.GaussianBlur(gray, (5, 5), 0)
@ -73,7 +73,7 @@ class ContourExtractor:
continue continue
(x, y, w, h) = cv2.boundingRect(c) (x, y, w, h) = cv2.boundingRect(c)
#print((x, y, w, h)) #print((x, y, w, h))
contours.append((frame[y:y+h, x:x+w], (x, y, w, h))) contours.append((x, y, w, h))
#cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) #cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if len(contours) != 0: if len(contours) != 0:

View File

@ -39,23 +39,23 @@ class Exporter:
writer.close() writer.close()
#cv2.destroyAllWindows() #cv2.destroyAllWindows()
def exportOverlayed(self, layers, outputPath): def exportOverlayed(self, underlay, layers, outputPath, resizeWidth):
fps = self.fps fps = self.fps
writer = imageio.get_writer(outputPath, fps=fps) writer = imageio.get_writer(outputPath, fps=fps)
maxLength = self.getMaxLengthOfLayers(layers) maxLength = self.getMaxLengthOfLayers(layers)
for i in range(maxLength): for i in range(maxLength):
frame1 = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8) frame1 = underlay
frame1 = imutils.resize(frame1, width=512) frame1 = imutils.resize(frame1, width=resizeWidth)
frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB)
for layer in layers: for layer in layers:
data = layer.data data = layer.data
if len(layer.data) > i: if len(layer.data) > i:
(x, y, w, h) = layer.bounds[i]
frame = layer.data[i] frame = layer.data[i]
(x, y, w, h) = frame[1] if frame is not None:
frame = frame[0] frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame1[y:y+h, x:x+w] = frame frame1[y:y+h, x:x+w] = frame
writer.append_data(np.array(frame1)) writer.append_data(np.array(frame1))
writer.close() writer.close()

View File

@ -1,3 +1,6 @@
import numpy as np
import cv2
import imutils
class Layer: class Layer:
#data = [(contour, (x,y,w,h)),] #data = [(contour, (x,y,w,h)),]
@ -10,16 +13,38 @@ class Layer:
self.lastFrame = startFrame self.lastFrame = startFrame
self.data = [] self.data = []
self.data.append(data) self.bounds = []
self.bounds.append(data)
#print("Layer constructed") #print("Layer constructed")
def add(self, frameNumber, data): def add(self, frameNumber, data):
if not (self.startFrame + len(self.data) - frameNumber < 0): if not (self.startFrame + len(self.bounds) - frameNumber < 0):
self.lastFrame = frameNumber self.lastFrame = frameNumber
self.data.append(data)
self.bounds.append(data)
def getLength(self): def getLength(self):
self.length = len(self.data) self.length = len(self.data)
return self.length return self.length
def fill(self, inputPath):
'''reads in the contour data, needed for export'''
cap = cv2.VideoCapture(inputPath)
self.data = [None]*len(self.bounds)
i = 0
cap.set(1, self.startFrame)
while i < len(self.bounds):
ret, frame = cap.read()
if ret:
frame = imutils.resize(frame, width=512)
(x, y, w, h) = self.bounds[i]
self.data[i] = frame[y:y+h, x:x+w]
i+=1
cap.release()

View File

@ -10,6 +10,13 @@ class LayerFactory:
if data is not None: if data is not None:
self.extractLayers(data) self.extractLayers(data)
def freeData(self, maxLayerLength):
self.data.clear()
for i in range(len(self.layers)):
if self.layers[i].getLength() > maxLayerLength:
del self.layers[i]
def extractLayers(self, data = None): def extractLayers(self, data = None):
tol = self.tolerance tol = self.tolerance
@ -29,27 +36,27 @@ class LayerFactory:
# inserts all the fucking contours as layers? # inserts all the fucking contours as layers?
for frameNumber, contours in data.items(): for frameNumber, contours in data.items():
for contour, (x,y,w,h) in contours: for (x,y,w,h) in contours:
foundLayer = False foundLayer = False
i = 0 i = 0
for i in range(0, len(layers)): for i in range(0, len(layers)):
layer = layers[i] layer = layers[i]
if len(layer.data[-1][1]) != 4: if len(layer.bounds[-1]) != 4:
# should never be called, hints at problem in ContourExtractor # should never be called, hints at problem in ContourExtractor
print("LayerFactory: Layer knew no bounds") print("LayerFactory: Layer knew no bounds")
continue continue
if frameNumber - layer.lastFrame <= 20: if frameNumber - layer.lastFrame <= 20:
(x2,y2,w2,h2) = layer.data[-1][1] (x2,y2,w2,h2) = layer.bounds[-1]
if self.contoursOverlay((x-tol,y+h+tol), (x+w+tol,y-tol), (x2,y2+h2), (x2+w2,y2)): if self.contoursOverlay((x-tol,y+h+tol), (x+w+tol,y-tol), (x2,y2+h2), (x2+w2,y2)):
foundLayer = True foundLayer = True
layer.add(frameNumber, (contour, (x,y,w,h))) layer.add(frameNumber, (x,y,w,h))
break break
layers[i] = layer layers[i] = layer
if not foundLayer: if not foundLayer:
layers.append(Layer(frameNumber, (contour, (x,y,w,h)))) layers.append(Layer(frameNumber, (x,y,w,h)))
self.layers = layers self.layers = layers
@ -66,6 +73,15 @@ class LayerFactory:
return True return True
def fillLayers(self, footagePath):
for i in range(len(self.layers)):
self.layers[i].fill(footagePath)
def sortLayers(self):
# straight bubble
self.layers.sort(key = lambda c:c.lastFrame)

Binary file not shown.

Binary file not shown.

View File

@ -15,7 +15,7 @@ xmax = 1920
ymax = 1080 ymax = 1080
# in minutes # in minutes
length = 1 length = 1
numberOfEvents = 3 numberOfEvents = 4
dirname = os.path.dirname(__file__) dirname = os.path.dirname(__file__)
imageType = ".png" imageType = ".png"

Binary file not shown.

16
main.py
View File

@ -3,22 +3,26 @@ import time
from ContourExctractor import ContourExtractor from ContourExctractor import ContourExtractor
from Exporter import Exporter from Exporter import Exporter
from LayerFactory import LayerFactory from LayerFactory import LayerFactory
import cv2
#TODO #TODO
# finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder # finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder
# X diff zu den ref bildern aufnehmen
# zeichen von contour inhalt
# langes video
def demo(): def demo():
print("startup") print("startup")
resizeWidth = 512
maxLayerLength = 5*50*30
start = time.time() start = time.time()
footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/3.MP4") footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/3.MP4")
contours = ContourExtractor().extractContours(footagePath) contours = ContourExtractor().extractContours(footagePath, resizeWidth)
print("Time consumed in working: ", time.time() - start) print("Time consumed in working: ", time.time() - start)
layerFactory = LayerFactory(contours) layerFactory = LayerFactory(contours)
Exporter().exportOverlayed(layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4")) layerFactory.freeData(maxLayerLength)
layerFactory.sortLayers()
layerFactory.fillLayers(footagePath)
underlay = cv2.VideoCapture(footagePath).read()[1]
Exporter().exportOverlayed(underlay, layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4"), resizeWidth)
print("Total time: ", time.time() - start)
def init(): def init():
print("not needed yet") print("not needed yet")

BIN
out.mp4

Binary file not shown.

BIN
short.mp4

Binary file not shown.

BIN
short2.mp4 Normal file

Binary file not shown.