Video-Summary/LayerFactory.py

90 lines
2.5 KiB
Python
Raw Normal View History

2020-09-24 20:48:04 +00:00
from Layer import Layer
2020-09-20 20:01:54 +00:00
class LayerFactory:
2020-09-24 20:48:04 +00:00
data = {}
layers = []
2020-09-30 17:22:10 +00:00
tolerance = 5
2020-09-24 20:48:04 +00:00
def __init__(self, data=None):
print("LayerFactory constructed")
self.data = data
if data is not None:
self.extractLayers(data)
2020-10-03 22:27:36 +00:00
def freeData(self, maxLayerLength):
self.data.clear()
for i in range(len(self.layers)):
if self.layers[i].getLength() > maxLayerLength:
del self.layers[i]
2020-09-24 20:48:04 +00:00
def extractLayers(self, data = None):
tol = self.tolerance
2020-09-24 20:48:04 +00:00
if self.data is None:
if data is None:
print("LayerFactory data was none")
return None
else:
self.data = data
layers = []
frameNumber = min(data)
contours = data[frameNumber]
for contour in contours:
2020-09-28 20:28:23 +00:00
layers.append(Layer(frameNumber, contour))
2020-09-24 20:48:04 +00:00
2020-09-28 20:28:23 +00:00
# inserts all the fucking contours as layers?
2020-09-24 20:48:04 +00:00
for frameNumber, contours in data.items():
2020-10-03 22:27:36 +00:00
for (x,y,w,h) in contours:
foundLayer = False
i = 0
for i in range(0, len(layers)):
layer = layers[i]
2020-09-29 20:23:04 +00:00
2020-10-03 22:27:36 +00:00
if len(layer.bounds[-1]) != 4:
# should never be called, hints at problem in ContourExtractor
print("LayerFactory: Layer knew no bounds")
continue
2020-09-28 20:28:23 +00:00
2020-10-04 12:51:16 +00:00
if frameNumber - layer.lastFrame <= 5:
2020-10-03 22:27:36 +00:00
(x2,y2,w2,h2) = layer.bounds[-1]
2020-09-28 20:28:23 +00:00
if self.contoursOverlay((x-tol,y+h+tol), (x+w+tol,y-tol), (x2,y2+h2), (x2+w2,y2)):
foundLayer = True
2020-10-03 22:27:36 +00:00
layer.add(frameNumber, (x,y,w,h))
2020-09-29 20:52:36 +00:00
break
layers[i] = layer
if not foundLayer:
2020-10-03 22:27:36 +00:00
layers.append(Layer(frameNumber, (x,y,w,h)))
2020-09-24 20:48:04 +00:00
self.layers = layers
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]):
return False
# If one rectangle is above other
if(l1[1] <= r2[1] or l2[1] <= r1[1]):
return False
return True
2020-10-03 22:27:36 +00:00
def fillLayers(self, footagePath):
for i in range(len(self.layers)):
self.layers[i].fill(footagePath)
def sortLayers(self):
# straight bubble
self.layers.sort(key = lambda c:c.lastFrame)
2020-09-24 20:48:04 +00:00