diff --git a/ContourExctractor.py b/ContourExctractor.py index 8e16bc1..acb3fc9 100644 --- a/ContourExctractor.py +++ b/ContourExctractor.py @@ -15,7 +15,7 @@ class ContourExtractor: #X = {frame_number: [(contour, (x,y,w,h)), ...], } extractedContours = dict() - min_area = 0 + min_area = 200 max_area = 30000 threashold = 25 xDim = 0 @@ -24,9 +24,10 @@ class ContourExtractor: def getextractedContours(self): return self.extractedContours - def __init__(self, videoPath): + def __init__(self): print("ContourExtractor initiated") + def extractContours(self, videoPath): min_area = self.min_area max_area = self.max_area threashold = self.threashold @@ -40,15 +41,15 @@ class ContourExtractor: firstFrame = None # loop over the frames of the video frameCount = 0 + extractedContours = dict() while res: res, frame = vs.read() # resize the frame, convert it to grayscale, and blur it if frame is None: print("ContourExtractor: frame was None") - return + break frame = imutils.resize(frame, width=500) - cv2.imshow( "frame", frame) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) @@ -66,18 +67,22 @@ class ContourExtractor: contours = [] for c in cnts: - if cv2.contourArea(c) < min_area or cv2.contourArea(c) > max_area: + ca = cv2.contourArea(c) + if ca < min_area or ca > max_area: continue - (x, y, w, h) = cv2.boundingRect(c) + #print((x, y, w, h)) contours.append((frame[y:y+h, x:x+w], (x, y, w, h))) + #cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) - self.extractedContours[frameCount] = contours + if len(contours) != 0: + extractedContours[frameCount] = contours frameCount += 1 #cv2.imshow( "annotated", frame ) - #cv2.waitKey(10) & 0XFF + self.extractedContours = extractedContours + def displayContours(self): values = self.extractedContours.values() diff --git a/Exporter.py b/Exporter.py index 82e54d9..2b8306a 100644 --- a/Exporter.py +++ b/Exporter.py @@ -17,21 +17,19 @@ class Exporter: writer.close() def exportLayers(self, layers, outputPath): + fps = self.fps + writer = imageio.get_writer(outputPath, fps=fps) for layer in layers: data = layer.data - fps = self.fps - writer = imageio.get_writer(outputPath, fps=fps) for frame in data: - - (x, y, w, h) = frame[1] frame = frame[0] frame1 = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8) frame1 = imutils.resize(frame1, width=512) frame1[y:y+frame.shape[0], x:x+frame.shape[1]] = frame writer.append_data(np.array(frame1)) - cv2.imshow("changes overlayed", frame) - cv2.waitKey(10) & 0XFF + #cv2.imshow("changes overlayed", frame) + #cv2.waitKey(10) & 0XFF - writer.close() - cv2.destroyAllWindows() + writer.close() + #cv2.destroyAllWindows() diff --git a/Layer.py b/Layer.py index 8de2e3a..75b0f62 100644 --- a/Layer.py +++ b/Layer.py @@ -5,12 +5,12 @@ class Layer: lastFrame = 0 backgroundImage = [] - def __init__(self, startFrame, data, backgroundImage): + def __init__(self, startFrame, data): self.startFrame = startFrame + self.lastFrame = startFrame self.data.append(data) - self.backgroundImage = backgroundImage - print("Layer constructed") + #print("Layer constructed") def add(self, frameNumber, data): self.lastFrame = frameNumber diff --git a/LayerFactory.py b/LayerFactory.py index e5cdba8..7c2c4ab 100644 --- a/LayerFactory.py +++ b/LayerFactory.py @@ -22,26 +22,24 @@ class LayerFactory: contours = data[frameNumber] for contour in contours: - layers.append(Layer(frameNumber, contour, None)) + layers.append(Layer(frameNumber, contour)) + # inserts all the fucking contours as layers? for frameNumber, contours in data.items(): - layerNum = len(layers) - i = 0 - while i < layerNum: - layer = layers[i] - if frameNumber - layer.lastFrame < 5: - for contour, (x,y,w,h) in contours: + for contour, (x,y,w,h) in contours: + for layer in layers: + if frameNumber - layer.lastFrame <= 5: if len(layer.data[-1][1]) != 4: print("LayerFactory: Layer knew no bounds") continue + (x2,y2,w2,h2) = layer.data[-1][1] - - if self.contoursOverlay((x,y+h), (x+w,y), (x2,y2+h2), (x2+w2,y2)): + tol = 10 + if self.contoursOverlay((x-tol,y+h+tol), (x+w+tol,y-tol), (x2,y2+h2), (x2+w2,y2)): layer.add(frameNumber, (contour, (x,y,w,h))) - else: - layers.append(Layer(frameNumber, contour, None)) - layerNum = len(layers) - i+=1 + break + + layers.append(Layer(frameNumber, (contour, (x,y,w,h)))) self.layers = layers diff --git a/__pycache__/ContourExctractor.cpython-37.pyc b/__pycache__/ContourExctractor.cpython-37.pyc index a7f602b..2d4c7b6 100644 Binary files a/__pycache__/ContourExctractor.cpython-37.pyc and b/__pycache__/ContourExctractor.cpython-37.pyc differ diff --git a/__pycache__/Exporter.cpython-37.pyc b/__pycache__/Exporter.cpython-37.pyc index b8adc05..04af32c 100644 Binary files a/__pycache__/Exporter.cpython-37.pyc and b/__pycache__/Exporter.cpython-37.pyc differ diff --git a/__pycache__/Layer.cpython-37.pyc b/__pycache__/Layer.cpython-37.pyc index d147627..acbf747 100644 Binary files a/__pycache__/Layer.cpython-37.pyc and b/__pycache__/Layer.cpython-37.pyc differ diff --git a/__pycache__/LayerFactory.cpython-37.pyc b/__pycache__/LayerFactory.cpython-37.pyc index c21679b..35bf1b1 100644 Binary files a/__pycache__/LayerFactory.cpython-37.pyc and b/__pycache__/LayerFactory.cpython-37.pyc differ diff --git a/main.py b/main.py index f0bdff3..6ca0693 100644 --- a/main.py +++ b/main.py @@ -11,17 +11,19 @@ from LayerFactory import LayerFactory def demo(): print("startup") - footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/2.mp4") + footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/out.mp4") start = time.time() - contourExtractor = ContourExtractor(footagePath) + contourExtractor = ContourExtractor() + contourExtractor.extractContours(footagePath) print("Time consumed in working: ",time.time() - start) - frames = contourExtractor.exportContours() + #frames = contourExtractor.exportContours() #Exporter().export(frames,os.path.join(os.path.dirname(__file__), "./short.mp4")) + contours = contourExtractor.getextractedContours() - layerFactory = LayerFactory(contourExtractor.extractedContours) + layerFactory = LayerFactory(contours) Exporter().exportLayers(layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4")) def init():