This commit is contained in:
parent
56e30be8c2
commit
79cb093df3
45
Analyzer.py
45
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:
|
class Analyzer:
|
||||||
def __init__(self):
|
def __init__(self, videoPath):
|
||||||
print("Analyzer constructed")
|
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()
|
||||||
|
|
@ -5,7 +5,6 @@ import imutils
|
||||||
import time
|
import time
|
||||||
import cv2
|
import cv2
|
||||||
import os
|
import os
|
||||||
import numpy as np
|
|
||||||
import traceback
|
import traceback
|
||||||
import _thread
|
import _thread
|
||||||
import imageio
|
import imageio
|
||||||
|
|
@ -16,7 +15,7 @@ class ContourExtractor:
|
||||||
#X = {frame_number: [(contour, (x,y,w,h)), ...], }
|
#X = {frame_number: [(contour, (x,y,w,h)), ...], }
|
||||||
extractedContours = dict()
|
extractedContours = dict()
|
||||||
min_area = 500
|
min_area = 500
|
||||||
max_area = 7000
|
max_area = 28000
|
||||||
threashold = 13
|
threashold = 13
|
||||||
xDim = 0
|
xDim = 0
|
||||||
yDim = 0
|
yDim = 0
|
||||||
|
|
@ -33,7 +32,7 @@ class ContourExtractor:
|
||||||
threashold = self.threashold
|
threashold = self.threashold
|
||||||
|
|
||||||
# initialize the first frame in the video stream
|
# initialize the first frame in the video stream
|
||||||
vs = cv2.VideoCapture(videoPath)
|
vs = VideoCapture(filename)
|
||||||
|
|
||||||
res, image = vs.read()
|
res, image = vs.read()
|
||||||
self.xDim = image.shape[1]
|
self.xDim = image.shape[1]
|
||||||
|
|
@ -53,10 +52,10 @@ class ContourExtractor:
|
||||||
|
|
||||||
frame = imutils.resize(frame, width=resizeWidth)
|
frame = imutils.resize(frame, width=resizeWidth)
|
||||||
|
|
||||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||||
gray = np.asarray(gray[:,:,1]/2 + gray[:,:,2]/2).astype(np.uint8)
|
#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 the first frame is None, initialize it
|
||||||
if firstFrame is None:
|
if firstFrame is None:
|
||||||
|
|
|
||||||
4
Layer.py
4
Layer.py
|
|
@ -28,7 +28,7 @@ class Layer:
|
||||||
self.length = len(self.bounds)
|
self.length = len(self.bounds)
|
||||||
return self.length
|
return self.length
|
||||||
|
|
||||||
def fill(self, inputPath):
|
def fill(self, inputPath, resizeWidth):
|
||||||
'''reads in the contour data, needed for export'''
|
'''reads in the contour data, needed for export'''
|
||||||
|
|
||||||
cap = cv2.VideoCapture(inputPath)
|
cap = cv2.VideoCapture(inputPath)
|
||||||
|
|
@ -39,7 +39,7 @@ class Layer:
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
|
|
||||||
if ret:
|
if ret:
|
||||||
frame = imutils.resize(frame, width=512)
|
frame = imutils.resize(frame, width=resizeWidth)
|
||||||
(x, y, w, h) = self.bounds[i]
|
(x, y, w, h) = self.bounds[i]
|
||||||
self.data[i] = frame[y:y+h, x:x+w]
|
self.data[i] = frame[y:y+h, x:x+w]
|
||||||
i+=1
|
i+=1
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,28 @@ class LayerFactory:
|
||||||
if data is not None:
|
if data is not None:
|
||||||
self.extractLayers(data)
|
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()
|
self.data.clear()
|
||||||
#for i in range(len(self.layers) - 2):
|
layers = []
|
||||||
#if self.layers[i].getLength() > maxLayerLength:
|
for l in self.layers:
|
||||||
#del self.layers[i]
|
if l.getLength() < maxLayerLength and l.getLength() > minLayerLength:
|
||||||
|
layers.append(l)
|
||||||
|
self.layers = layers
|
||||||
|
self.removeStaticLayers()
|
||||||
|
|
||||||
|
|
||||||
def extractLayers(self, data = None):
|
def extractLayers(self, data = None):
|
||||||
|
|
@ -55,20 +72,18 @@ class LayerFactory:
|
||||||
self.layers.append(Layer(frameNumber, (x,y,w,h)))
|
self.layers.append(Layer(frameNumber, (x,y,w,h)))
|
||||||
|
|
||||||
def contoursOverlay(self, l1, r1, l2, r2):
|
def contoursOverlay(self, l1, r1, l2, r2):
|
||||||
|
|
||||||
# If one rectangle is on left side of other
|
# If one rectangle is on left side of other
|
||||||
if(l1[0] >= r2[0] or l2[0] >= r1[0]):
|
if(l1[0] >= r2[0] or l2[0] >= r1[0]):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# If one rectangle is above other
|
# If one rectangle is above other
|
||||||
if(l1[1] <= r2[1] or l2[1] <= r1[1]):
|
if(l1[1] <= r2[1] or l2[1] <= r1[1]):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def fillLayers(self, footagePath):
|
def fillLayers(self, footagePath, resizeWidth):
|
||||||
for i in range(len(self.layers)):
|
for i in range(len(self.layers)):
|
||||||
self.layers[i].fill(footagePath)
|
self.layers[i].fill(footagePath, resizeWidth)
|
||||||
|
|
||||||
def sortLayers(self):
|
def sortLayers(self):
|
||||||
# straight bubble
|
# straight bubble
|
||||||
|
|
|
||||||
|
|
@ -1 +1,4 @@
|
||||||
time compression
|
time compression
|
||||||
|
|
||||||
|
|
||||||
|
Time consumed reading video: 369.0188868045807s 3.06GB 26min 1080p downscaled 500p 30fps
|
||||||
8
main.py
8
main.py
|
|
@ -3,6 +3,7 @@ import time
|
||||||
from ContourExctractor import ContourExtractor
|
from ContourExctractor import ContourExtractor
|
||||||
from Exporter import Exporter
|
from Exporter import Exporter
|
||||||
from LayerFactory import LayerFactory
|
from LayerFactory import LayerFactory
|
||||||
|
from Analyzer import Analyzer
|
||||||
import cv2
|
import cv2
|
||||||
#TODO
|
#TODO
|
||||||
# finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder
|
# finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder
|
||||||
|
|
@ -11,18 +12,21 @@ def demo():
|
||||||
print("startup")
|
print("startup")
|
||||||
resizeWidth = 512
|
resizeWidth = 512
|
||||||
maxLayerLength = 1*60*30
|
maxLayerLength = 1*60*30
|
||||||
|
minLayerLength = 3
|
||||||
start = time.time()
|
start = time.time()
|
||||||
|
|
||||||
footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/3.mp4")
|
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)
|
contours = ContourExtractor().extractContours(footagePath, resizeWidth)
|
||||||
print("Time consumed in working: ", time.time() - start)
|
print("Time consumed in working: ", time.time() - start)
|
||||||
layerFactory = LayerFactory(contours)
|
layerFactory = LayerFactory(contours)
|
||||||
print("freeing Data", time.time() - start)
|
print("freeing Data", time.time() - start)
|
||||||
layerFactory.freeData(maxLayerLength)
|
layerFactory.freeData(maxLayerLength, minLayerLength)
|
||||||
print("sort Layers")
|
print("sort Layers")
|
||||||
layerFactory.sortLayers()
|
layerFactory.sortLayers()
|
||||||
print("fill Layers")
|
print("fill Layers")
|
||||||
layerFactory.fillLayers(footagePath)
|
layerFactory.fillLayers(footagePath, resizeWidth)
|
||||||
underlay = cv2.VideoCapture(footagePath).read()[1]
|
underlay = cv2.VideoCapture(footagePath).read()[1]
|
||||||
Exporter().exportOverlayed(underlay, layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4"), resizeWidth)
|
Exporter().exportOverlayed(underlay, layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4"), resizeWidth)
|
||||||
print("Total time: ", time.time() - start)
|
print("Total time: ", time.time() - start)
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in New Issue