From ec4812bee1b01afd0d18a0da53b005a911deb1c8 Mon Sep 17 00:00:00 2001 From: Askill Date: Sun, 11 Oct 2020 22:54:12 +0200 Subject: [PATCH] multiple contours --- Exporter.py | 39 ++++++++++++++++++++------------------- Layer.py | 13 ++++++++----- LayerFactory.py | 18 +++++++++++------- main.py | 2 +- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/Exporter.py b/Exporter.py index 92f684a..6fad200 100644 --- a/Exporter.py +++ b/Exporter.py @@ -45,17 +45,17 @@ class Exporter: continue frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - + frame2 = underlay for layer in layers: if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount: - (x, y, w, h) = layer.bounds[frameCount - layer.startFrame] - factor = videoReader.w / self.resizeWidth - x = int(x * factor) - y = int(y * factor) - w = int(w * factor) - h = int(h * factor) - frame2 = underlay - frame2[y:y+h, x:x+w] = frame[y:y+h, x:x+w] + for (x, y, w, h) in layer.bounds[frameCount - layer.startFrame]: + 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] = frame[y:y+h, x:x+w] writer.append_data(frame2) @@ -84,16 +84,17 @@ class Exporter: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) for layer in layers: if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount: - (x, y, w, h) = layer.bounds[frameCount - layer.startFrame] - 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] - frame2[y:y+h, x:x+w] = frame[y:y+h, x:x+w] - frames[frameCount - layer.startFrame] = np.copy(frame2) + for (x, y, w, h) in layer.bounds[frameCount - layer.startFrame]: + (x, y, w, h) = layer.bounds[frameCount - layer.startFrame] + 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] + frame2[y:y+h, x:x+w] = frame[y:y+h, x:x+w] + frames[frameCount - layer.startFrame] = np.copy(frame2) videoReader.thread.join() diff --git a/Layer.py b/Layer.py index a4013dd..f9ed0c6 100644 --- a/Layer.py +++ b/Layer.py @@ -2,7 +2,7 @@ import numpy as np import cv2 import imutils class Layer: - #data = [(contour, (x,y,w,h)),] + #bounds = [(contour, (x,y,w,h)),] startFrame = None lastFrame = None @@ -14,14 +14,17 @@ class Layer: self.data = [] self.bounds = [] - self.bounds.append(data) + self.bounds.append([data]) #print("Layer constructed") def add(self, frameNumber, data): - if not (self.startFrame + len(self.bounds) - frameNumber < 0): + if not self.startFrame + len(self.bounds) < frameNumber: + if len(self.bounds[self.startFrame - frameNumber]) >= 1: + self.bounds[self.startFrame - frameNumber].append(data) + else: self.lastFrame = frameNumber - - self.bounds.append(data) + self.bounds.append([data]) + self.getLength() def getLength(self): diff --git a/LayerFactory.py b/LayerFactory.py index 0e8c718..fe3737a 100644 --- a/LayerFactory.py +++ b/LayerFactory.py @@ -23,9 +23,9 @@ class LayerFactory: layers = [] for i, layer in enumerate(self.layers): checks = 0 - if abs(self.layers[i].bounds[0][0] - self.layers[i].bounds[-1][0]) < 5: + if abs(self.layers[i].bounds[0][0][0] - self.layers[i].bounds[-1][0][0]) < 5: checks += 1 - if abs(self.layers[i].bounds[0][1] - self.layers[i].bounds[-1][1]) < 5: + if abs(self.layers[i].bounds[0][0][1] - self.layers[i].bounds[-1][0][1]) < 5: checks += 1 if checks <= 2: layers.append(layer) @@ -71,11 +71,15 @@ class LayerFactory: oldLayerIDs.append(i) continue - (x2,y2,w2,h2) = self.layers[i].bounds[-1] - if self.contoursOverlay((x-tol,y+h+tol), (x+w+tol,y-tol), (x2,y2+h2), (x2+w2,y2)): - self.layers[i].add(frameNumber, (x,y,w,h)) - foundLayer = True - break + for bounds in self.layers[i].bounds[-1]: + if bounds is None: + break + (x2,y2,w2,h2) = bounds + if self.contoursOverlay((x-tol,y+h+tol), (x+w+tol,y-tol), (x2,y2+h2), (x2+w2,y2)): + self.layers[i].add(frameNumber, (x,y,w,h)) + foundLayer = True + #break + if not foundLayer: self.layers.append(Layer(frameNumber, (x,y,w,h))) diff --git a/main.py b/main.py index 52cf811..fa42fc7 100644 --- a/main.py +++ b/main.py @@ -15,7 +15,7 @@ def demo(): start = time.time() config = Config() - config["inputPath"] = os.path.join(os.path.dirname(__file__), "./generate test footage/3.mp4") + config["inputPath"] = os.path.join(os.path.dirname(__file__), "./generate test footage/out.mp4") config["outputPath"] = os.path.join(os.path.dirname(__file__), "./output/short.mp4") contours = ContourExtractor(config).extractContours()