This commit is contained in:
Askill 2020-10-05 09:43:27 +02:00
parent 39635ded4c
commit c1bd5bb5d0
9 changed files with 74 additions and 52 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
generate test footage/images/ generate test footage/images/
generate test footage/3.MP4 generate test footage/3.MP4
short.mp4

View File

@ -1,3 +1,3 @@
class Analyzer: class Analyzer:
def __init__(self): def __init__(self, footage):
print("Analyzer constructed") print("Analyzer constructed")

View File

@ -10,6 +10,10 @@ import traceback
import _thread import _thread
import imageio import imageio
import numpy as np import numpy as np
from threading import Thread
from multiprocessing import Queue, Process, Pool
from multiprocessing.pool import ThreadPool
import concurrent.futures
class ContourExtractor: class ContourExtractor:
@ -28,9 +32,7 @@ class ContourExtractor:
print("ContourExtractor initiated") print("ContourExtractor initiated")
def extractContours(self, videoPath, resizeWidth): def extractContours(self, videoPath, resizeWidth):
min_area = self.min_area
max_area = self.max_area
threashold = self.threashold
# initialize the first frame in the video stream # initialize the first frame in the video stream
vs = cv2.VideoCapture(videoPath) vs = cv2.VideoCapture(videoPath)
@ -40,58 +42,73 @@ class ContourExtractor:
self.yDim = image.shape[0] self.yDim = image.shape[0]
firstFrame = None firstFrame = None
# loop over the frames of the video # loop over the frames of the video
frameCount = 0 frameCount = -1
extractedContours = dict()
results = []
extractedContours = dict() 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")
break
frame = imutils.resize(frame, width=resizeWidth) imageBuffer = []
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB) with concurrent.futures.ProcessPoolExecutor() as executor:
gray = np.asarray(gray[:,:,1]/2 + gray[:,:,2]/2).astype(np.uint8) while res:
frameCount += 1
#gray = cv2.GaussianBlur(gray, (5, 5), 0) if frameCount % (60*30) == 0:
print("Minutes processed: ", frameCount/(60*30))
# if the first frame is None, initialize it
if firstFrame is None:
firstFrame = gray
continue
frameDelta = cv2.absdiff(gray, firstFrame)
thresh = cv2.threshold(frameDelta, threashold, 255, cv2.THRESH_BINARY)[1]
# dilate the thresholded image to fill in holes, then find contours
thresh = cv2.dilate(thresh, None, iterations=3)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
contours = []
for c in cnts:
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((x, y, w, h))
#cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if len(contours) != 0:
extractedContours[frameCount] = contours
if frameCount % (60*30) == 0: res, frame = vs.read()
print("Minutes processed: ", frameCount/(60*30)) # resize the frame, convert it to grayscale, and blur it
frameCount += 1 if frame is None:
print("ContourExtractor: frame was None")
break
#cv2.imshow( "annotated", thresh ) frame = imutils.resize(frame, width=resizeWidth)
#cv2.waitKey(10) & 0XFF gray = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
# if the first frame is None, initialize it
if firstFrame is None:
gray = np.asarray(gray[:,:,1]/2 + gray[:,:,2]/2).astype(np.uint8)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
firstFrame = gray
continue
results.append(executor.submit(self.getContours, frameCount, gray, firstFrame))
#contours = self.getContours(frameCount, gray, firstFrame)
for f in concurrent.futures.as_completed(results):
x=f.result()
if x is not None:
extractedContours = {**extractedContours, **x}
self.extractedContours = extractedContours self.extractedContours = extractedContours
return extractedContours return extractedContours
def getContours(self, frameCount, gray, firstFrame):
gray = np.asarray(gray[:,:,1]/2 + gray[:,:,2]/2).astype(np.uint8)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
frameDelta = cv2.absdiff(gray, firstFrame)
thresh = cv2.threshold(frameDelta, self.threashold, 255, cv2.THRESH_BINARY)[1]
# dilate the thresholded image to fill in holes, then find contours
thresh = cv2.dilate(thresh, None, iterations=3)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
contours = []
for c in cnts:
ca = cv2.contourArea(c)
if ca < self.min_area or ca > self.max_area:
continue
(x, y, w, h) = cv2.boundingRect(c)
#print((x, y, w, h))
contours.append((x, y, w, h))
if len(contours) != 0:
return {frameCount: contours}
else:
return None
def displayContours(self): def displayContours(self):
values = self.extractedContours.values() values = self.extractedContours.values()
for xx in values: for xx in values:

View File

@ -35,7 +35,8 @@ class LayerFactory:
layers.append(Layer(frameNumber, contour)) layers.append(Layer(frameNumber, contour))
# inserts all the fucking contours as layers? # inserts all the fucking contours as layers?
for frameNumber, contours in data.items(): for frameNumber in sorted(data):
contours = data[frameNumber]
for (x,y,w,h) in contours: for (x,y,w,h) in contours:
foundLayer = False foundLayer = False
i = 0 i = 0

Binary file not shown.

View File

@ -3,17 +3,19 @@ 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
def demo(): def demo():
print("startup") print("startup")
resizeWidth = 512 resizeWidth = 1024
maxLayerLength = 1*60*30 maxLayerLength = 1*60*30
start = time.time() start = time.time()
footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/out.mp4") footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/3.MP4")
analyzer = Analyzer(footagePath)
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)
@ -21,7 +23,7 @@ def demo():
layerFactory.sortLayers() layerFactory.sortLayers()
layerFactory.fillLayers(footagePath) layerFactory.fillLayers(footagePath)
underlay = cv2.VideoCapture(footagePath).read()[1] underlay = cv2.VideoCapture(footagePath).read()[1]
Exporter().exportLayers(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)
def init(): def init():
print("not needed yet") print("not needed yet")

BIN
short.mp4

Binary file not shown.