merging layers

This commit is contained in:
Askill 2020-11-12 18:59:57 +01:00
parent 50dc1ef560
commit d0ec1f4612
8 changed files with 58 additions and 6 deletions

2
.gitignore vendored
View File

@ -11,3 +11,5 @@ __pycache__/
*.weights
*.m4v
*.txt

View File

@ -15,7 +15,7 @@ class Config:
"videoBufferLength": 500,
"noiseThreashold": 0.25,
"noiseSensitivity": 3/4,
"LayersPerContour": 2,
"LayersPerContour": 20,
"avgNum":10
}

View File

@ -62,7 +62,7 @@ class Layer:
avgx += float(middles[i][0]-middles[i-1][0])/len(middles)
avgy += float(middles[i][1]-middles[i-1][1])/len(middles)
self.stats = dict()
self.stats["avg"] = [round(avgx,2), round(avgy, 2)]
self.stats["avg"] = round((avgx**2 + avgy**2)**(1/2),2)
x=0
y=0
@ -73,8 +73,8 @@ class Layer:
x /= (len(middles)-1)
y /= (len(middles)-1)
self.stats["var"] = [round(x,2),round(y, 2)]
self.stats["dev"] = [round(x**(1/2), 2), round(y**(1/2),2)]
self.stats["var"] = round((x**2 + y**2)**(1/2),2)
self.stats["dev"] = round((x + y)**(1/2), 2)
def getLength(self):

View File

@ -5,6 +5,7 @@ from Application.Exporter import Exporter
from multiprocessing.pool import ThreadPool
import cv2
import numpy as np
import copy
class LayerFactory:
def __init__(self, config, data=None):
@ -57,6 +58,8 @@ class LayerFactory:
(x,y,w,h) = bounds
tol = self.tolerance
foundLayer = 0
#to merge layers
foundLayerIDs = []
for i in range(0, len(self.layers)):
if foundLayer >= self.config["LayersPerContour"]:
@ -80,11 +83,50 @@ class LayerFactory:
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 += 1
foundLayerIDs.append(i)
break
if foundLayer == 0:
self.layers.append(Layer(frameNumber, (x,y,w,h), self.config))
if len(foundLayerIDs) > 1:
self.mergeLayers(foundLayerIDs)
def mergeLayers(self, foundLayerIDs):
layers = self.sortLayers(foundLayerIDs)
layer1 = layers[0]
for layerID in range(1, len(layers)):
layer2 = layers[layerID]
layer1 = self.merge2Layers(layer1, layer2)
self.layers[foundLayerIDs[0]] = layer1
layers = []
for i, layer in enumerate(self.layers):
if i not in foundLayerIDs[1:]:
layers.append(layer)
self.layers = layers
def merge2Layers(self, layer1, layer2):
"""Merges 2 Layers, Layer1 must start before Layer2"""
dSF = layer2.startFrame - layer1.startFrame
l1bounds = copy.deepcopy(layer1.bounds)
for i in range(len(layer2.bounds)):
bounds = layer2.bounds[i]
while dSF + i >= len(l1bounds):
l1bounds.append([])
for bound in bounds:
if bound not in l1bounds[dSF + i]:
l1bounds[dSF + i].append(bound)
layer1.bounds = l1bounds
return layer1
def contoursOverlay(self, l1, r1, l2, r2):
# If one rectangle is on left side of other
if(l1[0] >= r2[0] or l2[0] >= r1[0]):
@ -93,3 +135,11 @@ class LayerFactory:
if(l1[1] <= r2[1] or l2[1] <= r1[1]):
return False
return True
def sortLayers(self, foundLayerIDs):
layers = []
for layerID in foundLayerIDs:
layers.append(self.layers[layerID])
layers.sort(key = lambda c:c.startFrame)
return layers

View File

@ -32,8 +32,8 @@ def main():
layers = layerFactory.extractLayers(contours)
else:
layers, contours = Importer(config).importRawData()
#layerFactory = LayerFactory(config)
#layers = layerFactory.extractLayers(contours)
layerFactory = LayerFactory(config)
layers = layerFactory.extractLayers(contours)
layerManager = LayerManager(config, layers)
layerManager.transformLayers()

Binary file not shown.

Binary file not shown.

Binary file not shown.