multiple contours

This commit is contained in:
Askill 2020-10-11 22:54:12 +02:00
parent 04187cf9ac
commit ec4812bee1
4 changed files with 40 additions and 32 deletions

View File

@ -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()

View File

@ -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):

View File

@ -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)))

View File

@ -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()