diff --git a/Application/Config.py b/Application/Config.py index 027d313..044fe01 100644 --- a/Application/Config.py +++ b/Application/Config.py @@ -1,18 +1,18 @@ class Config: c = { - "min_area" : 50, - "max_area" : 40000, + "min_area" : 100, + "max_area" : 900000, "threashold" : 7, - "resizeWidth" : 512, + "resizeWidth" : 500, "inputPath" : None, "outputPath": None, - "maxLayerLength": 1500, + "maxLayerLength": 5000, "minLayerLength": 40, "tolerance": 20, "maxLength": None, "ttolerance": 60, - "videoBufferLength": 500, + "videoBufferLength": 450, "LayersPerContour": 220, "avgNum":10 } diff --git a/Application/ContourExctractor.py b/Application/ContourExctractor.py index 7679e13..314aa66 100644 --- a/Application/ContourExctractor.py +++ b/Application/ContourExctractor.py @@ -1,7 +1,7 @@ from Application.VideoReader import VideoReader from Application.Config import Config -from threading import Thread +from threading import Thread, activeCount from multiprocessing import Queue, Process, Pool from multiprocessing.pool import ThreadPool from queue import Queue @@ -9,6 +9,7 @@ import imutils import time import cv2 import numpy as np +import os class ContourExtractor: @@ -64,7 +65,7 @@ class ContourExtractor: return self.extractedContours, self.extractedMasks def async2(self, tmpData): - with ThreadPool(16) as pool2: + with ThreadPool(os.cpu_count()) as pool2: pool2.map(self.getContours, tmpData) def getContours(self, data): @@ -76,7 +77,7 @@ class ContourExtractor: if frameCount % (10*self.fps) == 1: print( - f" \r {round((frameCount/self.fps)/self.length, 4)*100} % processed in {round(time.time() - self.start, 2)}s", end='\r') + f" \r \033[K {round((frameCount/self.fps)*100/self.length, 2)} % processed in {round(time.time() - self.start, 2)}s", end='\r') gray = self.prepareFrame(frame) frameDelta = cv2.absdiff(gray, firstFrame) @@ -132,7 +133,7 @@ class ContourExtractor: tmp = [[j, frames, averageFrames] for j in range(averageFrames, len(frames))] - with ThreadPool(16) as pool: + with ThreadPool(os.cpu_count()) as pool: pool.map(self.averageDaFrames, tmp) self.lastFrames = frames[-averageFrames:] diff --git a/Application/Exporter.py b/Application/Exporter.py index b740c39..70551b5 100644 --- a/Application/Exporter.py +++ b/Application/Exporter.py @@ -6,7 +6,7 @@ import imutils import numpy as np import cv2 import pickle -import time +import time class Exporter: @@ -28,7 +28,7 @@ class Exporter: self.exportLayers(layers) def exportLayers(self, layers): - + # TODO: fix videoreader instanciation listOfFrames = self.makeListOfFrames(layers) videoReader = VideoReader(self.config, listOfFrames) videoReader.fillBuffer() @@ -62,9 +62,9 @@ class Exporter: frame2[y:y+h, x:x+w] = np.copy(frame[y:y+h, x:x+w]) - time = datetime.fromtimestamp( + timestr = datetime.fromtimestamp( int(frameCount/self.fps) + videoReader.getStartTime()) - cv2.putText(frame2, str(i) + " " + f"{time.hour}:{time.minute}:{time.second}", (int( + cv2.putText(frame2, str(i) + " " + f"{timestr.hour}:{timestr.minute}:{timestr.second}", (int( x+w/2), int(y+h/2)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) #cv2.putText(frame2, str(layer.stats["avg"]) + " " + str(layer.stats["var"]) + " " + str(layer.stats["dev"]), (int(500), int(500)), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,0,255), 2) writer.append_data(frame2) diff --git a/Application/Layer.py b/Application/Layer.py index c02910c..79b3540 100644 --- a/Application/Layer.py +++ b/Application/Layer.py @@ -81,4 +81,38 @@ class Layer: def __len__(self): self.length = len(self.bounds) return self.length + + def spaceOverlaps(self, layer2): + '''Checks if there is an overlap in the bounds of current layer with given layer''' + overlap = False + maxLen = min(len(layer2.bounds), len(self.bounds)) + bounds = self.bounds[:maxLen] + for b1s, b2s in zip(bounds, layer2.bounds[:maxLen]): + for b1 in b1s: + for b2 in b2s: + if self.contoursOverlay((b1[0], b1[1]+b1[3]), (b1[0]+b1[2], b1[1]), (b2[0], b2[1]+b2[3]), (b2[0]+b2[2], b2[1])): + overlap = True + break + return overlap + + def timeOverlaps(self, layer2): + '''Checks for overlap in time between current and given layer''' + s1 = self.startFrame + e1 = self.lastFrame + s2 = layer2.startFrame + e2 = layer2.lastFrame + + if s2 >= s1 and s2 <= e1: + return True + elif s1 >= s2 and s1 <= e2: + return True + else: + return False + + def contoursOverlay(self, l1, r1, l2, r2): + if(l1[0] >= r2[0] or l2[0] >= r1[0]): + return False + if(l1[1] <= r2[1] or l2[1] <= r1[1]): + return False + return True diff --git a/Application/LayerFactory.py b/Application/LayerFactory.py index 0a9dda0..089323d 100644 --- a/Application/LayerFactory.py +++ b/Application/LayerFactory.py @@ -5,7 +5,7 @@ from Application.Exporter import Exporter from multiprocessing.pool import ThreadPool import numpy as np - +import os class LayerFactory: def __init__(self, config, data=None): @@ -36,7 +36,7 @@ class LayerFactory: self.oldLayerIDs = [] - with ThreadPool(16) as pool: + with ThreadPool(os.cpu_count()) as pool: for frameNumber in sorted(data.keys()): contours = data[frameNumber] masks = maskArr[frameNumber] diff --git a/Application/LayerManager.py b/Application/LayerManager.py index bfc0e0f..a8c51c0 100644 --- a/Application/LayerManager.py +++ b/Application/LayerManager.py @@ -31,6 +31,7 @@ class LayerManager: self.freeMax() self.sortLayers() self.calcStats() + print("Before deleting sparse layers ", len(self.layers)) self.deleteSparse() print("after deleting sparse layers ", len(self.layers)) @@ -38,7 +39,7 @@ class LayerManager: toDelete = [] for i, l in enumerate(self.layers): empty = l.bounds.count([]) - if empty / len(l) > 0.2: + if empty / len(l) > 0.5: toDelete.append(i) for i, id in enumerate(toDelete): diff --git a/main.py b/main.py index 6e3a63a..f67a23f 100644 --- a/main.py +++ b/main.py @@ -15,7 +15,7 @@ def main(): startTotal = time.time() config = Config() - fileName = "out.mp4" + fileName = "x23-1.mp4" outputPath = os.path.join(os.path.dirname(__file__), "output") dirName = os.path.join(os.path.dirname(__file__), "generate test footage")