diff --git a/ContourExctractor.py b/ContourExctractor.py index 410784a..0cf783d 100644 --- a/ContourExctractor.py +++ b/ContourExctractor.py @@ -15,9 +15,9 @@ class ContourExtractor: #X = {frame_number: [(contour, (x,y,w,h)), ...], } extractedContours = dict() - min_area = 100 - max_area = 50000 - threashold = 12 + min_area = 500 + max_area = 5000 + threashold = 20 xDim = 0 yDim = 0 @@ -27,7 +27,7 @@ class ContourExtractor: def __init__(self): print("ContourExtractor initiated") - def extractContours(self, videoPath): + def extractContours(self, videoPath, resizeWidth): min_area = self.min_area max_area = self.max_area threashold = self.threashold @@ -49,8 +49,8 @@ class ContourExtractor: print("ContourExtractor: frame was None") break - frame = imutils.resize(frame, width=500) - frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + frame = imutils.resize(frame, width=resizeWidth) + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) @@ -73,7 +73,7 @@ class ContourExtractor: continue (x, y, w, h) = cv2.boundingRect(c) #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) if len(contours) != 0: diff --git a/Exporter.py b/Exporter.py index 683c1e5..36f406e 100644 --- a/Exporter.py +++ b/Exporter.py @@ -39,24 +39,24 @@ class Exporter: writer.close() #cv2.destroyAllWindows() - def exportOverlayed(self, layers, outputPath): + def exportOverlayed(self, underlay, layers, outputPath, resizeWidth): fps = self.fps writer = imageio.get_writer(outputPath, fps=fps) maxLength = self.getMaxLengthOfLayers(layers) for i in range(maxLength): - frame1 = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8) - frame1 = imutils.resize(frame1, width=512) - + frame1 = underlay + frame1 = imutils.resize(frame1, width=resizeWidth) + frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB) for layer in layers: data = layer.data if len(layer.data) > i: + (x, y, w, h) = layer.bounds[i] frame = layer.data[i] - (x, y, w, h) = frame[1] - frame = frame[0] - - frame1[y:y+h, x:x+w] = frame + if frame is not None: + frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + frame1[y:y+h, x:x+w] = frame writer.append_data(np.array(frame1)) writer.close() diff --git a/Layer.py b/Layer.py index 81be920..f30c3a7 100644 --- a/Layer.py +++ b/Layer.py @@ -1,3 +1,6 @@ +import numpy as np +import cv2 +import imutils class Layer: #data = [(contour, (x,y,w,h)),] @@ -10,16 +13,38 @@ class Layer: self.lastFrame = startFrame self.data = [] - self.data.append(data) + self.bounds = [] + self.bounds.append(data) + #print("Layer constructed") 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.data.append(data) + + self.bounds.append(data) def getLength(self): self.length = len(self.data) 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() + + diff --git a/LayerFactory.py b/LayerFactory.py index 7b0e808..cb074ba 100644 --- a/LayerFactory.py +++ b/LayerFactory.py @@ -10,6 +10,13 @@ class LayerFactory: if data is not None: 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): tol = self.tolerance @@ -29,27 +36,27 @@ class LayerFactory: # inserts all the fucking contours as layers? for frameNumber, contours in data.items(): - for contour, (x,y,w,h) in contours: + for (x,y,w,h) in contours: foundLayer = False i = 0 for i in range(0, len(layers)): 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 print("LayerFactory: Layer knew no bounds") continue 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)): foundLayer = True - layer.add(frameNumber, (contour, (x,y,w,h))) + layer.add(frameNumber, (x,y,w,h)) break layers[i] = layer if not foundLayer: - layers.append(Layer(frameNumber, (contour, (x,y,w,h)))) + layers.append(Layer(frameNumber, (x,y,w,h))) self.layers = layers @@ -66,6 +73,15 @@ class LayerFactory: 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) + + diff --git a/__pycache__/ContourExctractor.cpython-37.pyc b/__pycache__/ContourExctractor.cpython-37.pyc index 6be29d4..5039fb1 100644 Binary files a/__pycache__/ContourExctractor.cpython-37.pyc and b/__pycache__/ContourExctractor.cpython-37.pyc differ diff --git a/__pycache__/Exporter.cpython-37.pyc b/__pycache__/Exporter.cpython-37.pyc index 3505af8..79a35c2 100644 Binary files a/__pycache__/Exporter.cpython-37.pyc and b/__pycache__/Exporter.cpython-37.pyc differ diff --git a/__pycache__/Layer.cpython-37.pyc b/__pycache__/Layer.cpython-37.pyc index 2f83e59..4001efc 100644 Binary files a/__pycache__/Layer.cpython-37.pyc and b/__pycache__/Layer.cpython-37.pyc differ diff --git a/__pycache__/LayerFactory.cpython-37.pyc b/__pycache__/LayerFactory.cpython-37.pyc index 8ba4258..bfde8e3 100644 Binary files a/__pycache__/LayerFactory.cpython-37.pyc and b/__pycache__/LayerFactory.cpython-37.pyc differ diff --git a/generate test footage/gen.py b/generate test footage/gen.py index a2b924d..04fa9d1 100644 --- a/generate test footage/gen.py +++ b/generate test footage/gen.py @@ -15,7 +15,7 @@ xmax = 1920 ymax = 1080 # in minutes length = 1 -numberOfEvents = 3 +numberOfEvents = 4 dirname = os.path.dirname(__file__) imageType = ".png" diff --git a/generate test footage/out.mp4 b/generate test footage/out.mp4 index 992380d..518905a 100644 Binary files a/generate test footage/out.mp4 and b/generate test footage/out.mp4 differ diff --git a/main.py b/main.py index 2190b28..c945e94 100644 --- a/main.py +++ b/main.py @@ -3,22 +3,26 @@ import time from ContourExctractor import ContourExtractor from Exporter import Exporter from LayerFactory import LayerFactory +import cv2 #TODO # 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(): print("startup") + resizeWidth = 512 + maxLayerLength = 5*50*30 start = time.time() footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/3.MP4") - contours = ContourExtractor().extractContours(footagePath) - print("Time consumed in working: ",time.time() - start) + contours = ContourExtractor().extractContours(footagePath, resizeWidth) + print("Time consumed in working: ", time.time() - start) 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(): print("not needed yet") diff --git a/out.mp4 b/out.mp4 deleted file mode 100644 index 992380d..0000000 Binary files a/out.mp4 and /dev/null differ diff --git a/short.mp4 b/short.mp4 index faa60bd..76ba390 100644 Binary files a/short.mp4 and b/short.mp4 differ diff --git a/short2.mp4 b/short2.mp4 new file mode 100644 index 0000000..1bdf41d Binary files /dev/null and b/short2.mp4 differ