diff --git a/Analyzer.py b/Analyzer.py index 1e5d111..d6f936b 100644 --- a/Analyzer.py +++ b/Analyzer.py @@ -1,3 +1,44 @@ +from imutils.video import VideoStream +import argparse +import datetime +import imutils +import time +import cv2 +import os +import traceback +import _thread +import imageio +import numpy as np +import matplotlib.pyplot as plt + class Analyzer: - def __init__(self): - print("Analyzer constructed") \ No newline at end of file + def __init__(self, videoPath): + print("Analyzer constructed") + data = self.readIntoMem(videoPath) + + vs = cv2.VideoCapture(videoPath) + threashold = 13 + res, image = vs.read() + firstFrame = None + i = 0 + diff = [] + while res: + res, frame = vs.read() + if not res: + break + frame = imutils.resize(frame, width=500) + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB) + if firstFrame is None: + firstFrame = gray + continue + frameDelta = cv2.absdiff(gray, firstFrame) + thresh = cv2.threshold(frameDelta, threashold, 255, cv2.THRESH_BINARY)[1] + diff.append(np.count_nonzero(thresh)) + i+=1 + if i % (60*30) == 0: + print("Minutes processed: ", i/(60*30)) + #print(diff) + + plt.plot(diff) + plt.ylabel('some numbers') + plt.show() \ No newline at end of file diff --git a/ContourExctractor.py b/ContourExctractor.py index 01f3b05..2aa5de2 100644 --- a/ContourExctractor.py +++ b/ContourExctractor.py @@ -5,7 +5,6 @@ import imutils import time import cv2 import os -import numpy as np import traceback import _thread import imageio @@ -16,7 +15,7 @@ class ContourExtractor: #X = {frame_number: [(contour, (x,y,w,h)), ...], } extractedContours = dict() min_area = 500 - max_area = 7000 + max_area = 28000 threashold = 13 xDim = 0 yDim = 0 @@ -33,7 +32,7 @@ class ContourExtractor: threashold = self.threashold # initialize the first frame in the video stream - vs = cv2.VideoCapture(videoPath) + vs = VideoCapture(filename) res, image = vs.read() self.xDim = image.shape[1] @@ -53,10 +52,10 @@ class ContourExtractor: frame = imutils.resize(frame, width=resizeWidth) - gray = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB) - gray = np.asarray(gray[:,:,1]/2 + gray[:,:,2]/2).astype(np.uint8) + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + #gray = np.asarray(gray[:,:,1]/3 + gray[:,:,2]/3 + gray[:,:,0]/6).astype(np.uint8) - #gray = cv2.GaussianBlur(gray, (5, 5), 0) + gray = cv2.GaussianBlur(gray, (5, 5), 0) # if the first frame is None, initialize it if firstFrame is None: diff --git a/Layer.py b/Layer.py index 5a569f0..a4013dd 100644 --- a/Layer.py +++ b/Layer.py @@ -28,7 +28,7 @@ class Layer: self.length = len(self.bounds) return self.length - def fill(self, inputPath): + def fill(self, inputPath, resizeWidth): '''reads in the contour data, needed for export''' cap = cv2.VideoCapture(inputPath) @@ -39,7 +39,7 @@ class Layer: ret, frame = cap.read() if ret: - frame = imutils.resize(frame, width=512) + frame = imutils.resize(frame, width=resizeWidth) (x, y, w, h) = self.bounds[i] self.data[i] = frame[y:y+h, x:x+w] i+=1 diff --git a/LayerFactory.py b/LayerFactory.py index a5e4a6e..808c5c6 100644 --- a/LayerFactory.py +++ b/LayerFactory.py @@ -10,11 +10,28 @@ class LayerFactory: if data is not None: self.extractLayers(data) - def freeData(self, maxLayerLength): + def removeStaticLayers(self): + '''Removes Layers with little to no movement''' + layers = [] + for i, layer in enumerate(self.layers): + checks = 0 + if abs(self.layers[i].bounds[0][0] - self.layers[i].bounds[-1][0]) < 5: + checks += 1 + if abs(self.layers[i].bounds[0][1] - self.layers[i].bounds[-1][1]) < 5: + checks += 1 + if checks <= 2: + layers.append(layer) + self.layers = layers + + + def freeData(self, maxLayerLength, minLayerLength): self.data.clear() - #for i in range(len(self.layers) - 2): - #if self.layers[i].getLength() > maxLayerLength: - #del self.layers[i] + layers = [] + for l in self.layers: + if l.getLength() < maxLayerLength and l.getLength() > minLayerLength: + layers.append(l) + self.layers = layers + self.removeStaticLayers() def extractLayers(self, data = None): @@ -55,20 +72,18 @@ class LayerFactory: self.layers.append(Layer(frameNumber, (x,y,w,h))) 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 - def fillLayers(self, footagePath): + def fillLayers(self, footagePath, resizeWidth): for i in range(len(self.layers)): - self.layers[i].fill(footagePath) + self.layers[i].fill(footagePath, resizeWidth) def sortLayers(self): # straight bubble diff --git a/README.md b/README.md index 6949d81..466951c 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ time compression + + +Time consumed reading video: 369.0188868045807s 3.06GB 26min 1080p downscaled 500p 30fps \ No newline at end of file diff --git a/main.py b/main.py index c1ac414..95a3cdd 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import time from ContourExctractor import ContourExtractor from Exporter import Exporter from LayerFactory import LayerFactory +from Analyzer import Analyzer import cv2 #TODO # finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder @@ -11,18 +12,21 @@ def demo(): print("startup") resizeWidth = 512 maxLayerLength = 1*60*30 + minLayerLength = 3 start = time.time() footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/3.mp4") + #analyzer = Analyzer(footagePath) + print("Time consumed reading video: ", time.time() - start) contours = ContourExtractor().extractContours(footagePath, resizeWidth) print("Time consumed in working: ", time.time() - start) layerFactory = LayerFactory(contours) print("freeing Data", time.time() - start) - layerFactory.freeData(maxLayerLength) + layerFactory.freeData(maxLayerLength, minLayerLength) print("sort Layers") layerFactory.sortLayers() print("fill Layers") - layerFactory.fillLayers(footagePath) + layerFactory.fillLayers(footagePath, resizeWidth) underlay = cv2.VideoCapture(footagePath).read()[1] Exporter().exportOverlayed(underlay, layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4"), resizeWidth) print("Total time: ", time.time() - start) diff --git a/short.mp4 b/short.mp4 index 596dd78..3eab831 100644 Binary files a/short.mp4 and b/short.mp4 differ diff --git a/short3.mp4 b/short3.mp4 new file mode 100644 index 0000000..f77ada2 Binary files /dev/null and b/short3.mp4 differ