diff --git a/.gitignore b/.gitignore index e270726..ac54c08 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ __pycache__/ *.weights *.m4v + +*.txt diff --git a/Application/Config.py b/Application/Config.py index 2a6feec..4aebf2a 100644 --- a/Application/Config.py +++ b/Application/Config.py @@ -15,7 +15,7 @@ class Config: "videoBufferLength": 500, "noiseThreashold": 0.25, "noiseSensitivity": 3/4, - "LayersPerContour": 2, + "LayersPerContour": 20, "avgNum":10 } diff --git a/Application/Layer.py b/Application/Layer.py index 1369e3e..7c8c4d4 100644 --- a/Application/Layer.py +++ b/Application/Layer.py @@ -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): diff --git a/Application/LayerFactory.py b/Application/LayerFactory.py index 90522fc..d1d3aeb 100644 --- a/Application/LayerFactory.py +++ b/Application/LayerFactory.py @@ -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 \ No newline at end of file diff --git a/main.py b/main.py index b8aa6e7..47aa2ab 100644 --- a/main.py +++ b/main.py @@ -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() diff --git a/output/3.txt b/output/3.txt deleted file mode 100644 index fcac7c2..0000000 Binary files a/output/3.txt and /dev/null differ diff --git a/output/out.txt b/output/out.txt deleted file mode 100644 index 5b0e54c..0000000 Binary files a/output/out.txt and /dev/null differ diff --git a/output/x23.txt b/output/x23.txt deleted file mode 100644 index 86e1a40..0000000 Binary files a/output/x23.txt and /dev/null differ