started on no overlap overlay

This commit is contained in:
Askill 2021-02-05 20:04:10 +01:00
parent cfcf0daed4
commit fb043a61d7
7 changed files with 53 additions and 17 deletions

View File

@ -1,18 +1,18 @@
class Config: class Config:
c = { c = {
"min_area" : 50, "min_area" : 100,
"max_area" : 40000, "max_area" : 900000,
"threashold" : 7, "threashold" : 7,
"resizeWidth" : 512, "resizeWidth" : 500,
"inputPath" : None, "inputPath" : None,
"outputPath": None, "outputPath": None,
"maxLayerLength": 1500, "maxLayerLength": 5000,
"minLayerLength": 40, "minLayerLength": 40,
"tolerance": 20, "tolerance": 20,
"maxLength": None, "maxLength": None,
"ttolerance": 60, "ttolerance": 60,
"videoBufferLength": 500, "videoBufferLength": 450,
"LayersPerContour": 220, "LayersPerContour": 220,
"avgNum":10 "avgNum":10
} }

View File

@ -1,7 +1,7 @@
from Application.VideoReader import VideoReader from Application.VideoReader import VideoReader
from Application.Config import Config from Application.Config import Config
from threading import Thread from threading import Thread, activeCount
from multiprocessing import Queue, Process, Pool from multiprocessing import Queue, Process, Pool
from multiprocessing.pool import ThreadPool from multiprocessing.pool import ThreadPool
from queue import Queue from queue import Queue
@ -9,6 +9,7 @@ import imutils
import time import time
import cv2 import cv2
import numpy as np import numpy as np
import os
class ContourExtractor: class ContourExtractor:
@ -64,7 +65,7 @@ class ContourExtractor:
return self.extractedContours, self.extractedMasks return self.extractedContours, self.extractedMasks
def async2(self, tmpData): def async2(self, tmpData):
with ThreadPool(16) as pool2: with ThreadPool(os.cpu_count()) as pool2:
pool2.map(self.getContours, tmpData) pool2.map(self.getContours, tmpData)
def getContours(self, data): def getContours(self, data):
@ -76,7 +77,7 @@ class ContourExtractor:
if frameCount % (10*self.fps) == 1: if frameCount % (10*self.fps) == 1:
print( 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) gray = self.prepareFrame(frame)
frameDelta = cv2.absdiff(gray, firstFrame) frameDelta = cv2.absdiff(gray, firstFrame)
@ -132,7 +133,7 @@ class ContourExtractor:
tmp = [[j, frames, averageFrames] tmp = [[j, frames, averageFrames]
for j in range(averageFrames, len(frames))] for j in range(averageFrames, len(frames))]
with ThreadPool(16) as pool: with ThreadPool(os.cpu_count()) as pool:
pool.map(self.averageDaFrames, tmp) pool.map(self.averageDaFrames, tmp)
self.lastFrames = frames[-averageFrames:] self.lastFrames = frames[-averageFrames:]

View File

@ -28,7 +28,7 @@ class Exporter:
self.exportLayers(layers) self.exportLayers(layers)
def exportLayers(self, layers): def exportLayers(self, layers):
# TODO: fix videoreader instanciation
listOfFrames = self.makeListOfFrames(layers) listOfFrames = self.makeListOfFrames(layers)
videoReader = VideoReader(self.config, listOfFrames) videoReader = VideoReader(self.config, listOfFrames)
videoReader.fillBuffer() videoReader.fillBuffer()
@ -62,9 +62,9 @@ class Exporter:
frame2[y:y+h, x:x+w] = np.copy(frame[y:y+h, x:x+w]) 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()) 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) 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) #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) writer.append_data(frame2)

View File

@ -82,3 +82,37 @@ class Layer:
self.length = len(self.bounds) self.length = len(self.bounds)
return self.length 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

View File

@ -5,7 +5,7 @@ from Application.Exporter import Exporter
from multiprocessing.pool import ThreadPool from multiprocessing.pool import ThreadPool
import numpy as np import numpy as np
import os
class LayerFactory: class LayerFactory:
def __init__(self, config, data=None): def __init__(self, config, data=None):
@ -36,7 +36,7 @@ class LayerFactory:
self.oldLayerIDs = [] self.oldLayerIDs = []
with ThreadPool(16) as pool: with ThreadPool(os.cpu_count()) as pool:
for frameNumber in sorted(data.keys()): for frameNumber in sorted(data.keys()):
contours = data[frameNumber] contours = data[frameNumber]
masks = maskArr[frameNumber] masks = maskArr[frameNumber]

View File

@ -31,6 +31,7 @@ class LayerManager:
self.freeMax() self.freeMax()
self.sortLayers() self.sortLayers()
self.calcStats() self.calcStats()
print("Before deleting sparse layers ", len(self.layers))
self.deleteSparse() self.deleteSparse()
print("after deleting sparse layers ", len(self.layers)) print("after deleting sparse layers ", len(self.layers))
@ -38,7 +39,7 @@ class LayerManager:
toDelete = [] toDelete = []
for i, l in enumerate(self.layers): for i, l in enumerate(self.layers):
empty = l.bounds.count([]) empty = l.bounds.count([])
if empty / len(l) > 0.2: if empty / len(l) > 0.5:
toDelete.append(i) toDelete.append(i)
for i, id in enumerate(toDelete): for i, id in enumerate(toDelete):

View File

@ -15,7 +15,7 @@ def main():
startTotal = time.time() startTotal = time.time()
config = Config() config = Config()
fileName = "out.mp4" fileName = "x23-1.mp4"
outputPath = os.path.join(os.path.dirname(__file__), "output") outputPath = os.path.join(os.path.dirname(__file__), "output")
dirName = os.path.join(os.path.dirname(__file__), "generate test footage") dirName = os.path.join(os.path.dirname(__file__), "generate test footage")