removed unsued imports, sorted imports, formatted and refactor

This commit is contained in:
Askill 2022-08-16 22:50:00 +02:00
parent 90ecff91f4
commit c2b1b74735
13 changed files with 157 additions and 244 deletions

2
.gitignore vendored
View File

@ -2,7 +2,7 @@
generate test footage/images/
generate test footage/3.MP4
input/*
short.mp4
__pycache__/

View File

@ -2,12 +2,14 @@
# https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
# Tensorflow Object Detection Detector
import json
import os
import cv2
import imutils
import numpy as np
import tensorflow as tf
import cv2
import os
import json
import imutils
from Application.Classifiers.ClassifierInterface import ClassifierInterface

View File

@ -1,8 +1,9 @@
import json
import os
import cv2
import numpy as np
import tensorflow as tf
import cv2
import os
import json
from Application.Classifiers.ClassifierInterface import ClassifierInterface

View File

@ -1,9 +1,9 @@
class Config:
c = {
"min_area": 100,
"min_area": 300,
"max_area": 900000,
"threashold": 7,
"resizeWidth": 500,
"resizeWidth": 700,
"inputPath": None,
"outputPath": None,
"maxLayerLength": 5000,

View File

@ -1,15 +1,16 @@
from Application.VideoReader import VideoReader
from Application.Config import Config
from threading import Thread, activeCount
from multiprocessing import Queue, Process, Pool
import os
import time
from multiprocessing import Pool, Process, Queue
from multiprocessing.pool import ThreadPool
from queue import Queue
import imutils
import time
from threading import Thread, activeCount
import cv2
import imutils
import numpy as np
import os
from Application.Config import Config
from Application.VideoReader import VideoReader
class ContourExtractor:

View File

@ -1,13 +1,13 @@
from Application.Config import Config
from Application.Layer import Layer
from Application.VideoReader import VideoReader
import pickle
import time
from datetime import datetime
import cv2
import imageio
import imutils
import numpy as np
import cv2
import pickle
import time
from Application.VideoReader import VideoReader
class Exporter:
@ -20,144 +20,139 @@ class Exporter:
self.config = config
print("Exporter initiated")
def export(self, layers, contours, masks, raw=True, overlayed=True):
def export(self, layers, contours, masks, raw=True, overlayed=True, blackBackground=False, showProgress=False):
if raw:
self.exportRawData(layers, contours, masks)
if overlayed:
self.exportOverlayed(layers)
self.exportOverlayed(layers, blackBackground, showProgress)
else:
self.exportLayers(layers)
def exportLayers(self, layers):
# TODO: fix videoreader instanciation
listOfFrames = self.makeListOfFrames(layers)
videoReader = VideoReader(self.config, listOfFrames)
videoReader.fillBuffer()
maxLength = self.getMaxLengthOfLayers(layers)
underlay = cv2.VideoCapture(self.footagePath).read()[1]
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
exportFrame = 0
with VideoReader(self.config, listOfFrames) as videoReader:
underlay = cv2.VideoCapture(self.footagePath).read()[1]
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
self.fps = videoReader.getFPS()
writer = imageio.get_writer(self.outputPath, fps=self.fps)
fps = videoReader.getFPS()
writer = imageio.get_writer(self.outputPath, fps=fps)
start = time.time()
for i, layer in enumerate(layers):
print(f"\r {i}/{len(layers)} {round(i/len(layers)*100,2)}% {round((time.time() - start), 2)}s", end="\r")
if len(layer.bounds[0]) == 0:
continue
videoReader = VideoReader(self.config)
listOfFrames = self.makeListOfFrames([layer])
videoReader.fillBuffer(listOfFrames)
while not videoReader.videoEnded():
frameCount, frame = videoReader.pop()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame2 = np.copy(underlay)
for (x, y, w, h) in layer.bounds[frameCount - layer.startFrame]:
if x is None:
continue
factor = videoReader.w / self.resizeWidth
x, y, w, h = (int(x * factor), int(y * factor), int(w * factor), int(h * factor))
start = time.time()
for i, layer in enumerate(layers):
print(f"\r {i}/{len(layers)} {round(i/len(layers)*100,2)}% {round((time.time() - start), 2)}s", end="\r")
if len(layer.bounds[0]) == 0:
continue
videoReader = VideoReader(self.config)
listOfFrames = self.makeListOfFrames([layer])
videoReader.fillBuffer(listOfFrames)
while not videoReader.videoEnded():
frameCount, frame = videoReader.pop()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame2 = np.copy(underlay)
for (x, y, w, h) in layer.bounds[frameCount - layer.startFrame]:
if x is None:
continue
factor = videoReader.w / self.resizeWidth
x, y, w, h = (int(x * factor), int(y * factor), int(w * factor), int(h * factor))
frame2[y : y + h, x : x + w] = np.copy(frame[y : y + h, x : x + w])
frame2[y : y + h, x : x + w] = np.copy(frame[y : y + h, x : x + w])
timestr = datetime.fromtimestamp(int(frameCount / self.fps) + videoReader.getStartTime())
cv2.putText(
frame2,
str(i) + " " + f"{timestr.hour}:{timestr.minute}:{timestr.second}",
(int(x + w / 2), int(y + h / 2)),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 255, 255),
2,
)
# cv2.putText(frame2, str(layer.stats["avg"]) + " " + str(layer.stats["var"]) + " " + str(layer.stats["dev"]), (int(500), int(500)), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,0,255), 2)
writer.append_data(frame2)
videoReader.vc.release()
videoReader.thread.join()
videoReader.vc.release()
videoReader.thread.join()
writer.close()
self.addTimestamp(frame2, videoReader, frameCount, layer, x, y, w, h)
writer.append_data(frame2)
def exportOverlayed(self, layers):
writer.close()
def exportOverlayed(self, layers, blackBackground=False, showProgress=False):
listOfFrames = self.makeListOfFrames(layers)
videoReader = VideoReader(self.config, listOfFrames)
videoReader.fillBuffer()
maxLength = self.getMaxLengthOfLayers(layers)
underlay = cv2.VideoCapture(self.footagePath).read()[1]
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
# underlay = np.zeros(shape=[videoReader.h, videoReader.w, 3], dtype=np.uint8)
if blackBackground:
underlay = np.zeros(shape=[videoReader.h, videoReader.w, 3], dtype=np.uint8)
else:
underlay = cv2.VideoCapture(self.footagePath).read()[1]
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
frames = []
for i in range(maxLength):
frames.append(np.copy(underlay))
exportFrame = 0
while not videoReader.videoEnded():
frameCount, frame = videoReader.pop()
if frameCount % (60 * self.fps) == 0:
print("Minutes processed: ", frameCount / (60 * self.fps), end="\r")
if frame is None:
print("ContourExtractor: frame was None")
continue
with VideoReader(self.config, listOfFrames) as videoReader:
while not videoReader.videoEnded():
frameCount, frame = videoReader.pop()
if frameCount % (60 * self.fps) == 0:
print("Minutes processed: ", frameCount / (60 * self.fps), end="\r")
if frame is None:
print("ContourExtractor: frame was None")
continue
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
for layer in layers:
if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount:
for i in range(0, len(layer.bounds[frameCount - layer.startFrame])):
try:
underlay1 = underlay
(x, y, w, h) = layer.bounds[frameCount - layer.startFrame][i]
mask = layer.masks[frameCount - layer.startFrame][i]
if x is None:
break
factor = videoReader.w / self.resizeWidth
x, y, w, h = (int(x * factor), int(y * factor), int(w * factor), int(h * factor))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
for layer in layers:
if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount:
for i in range(0, len(layer.bounds[frameCount - layer.startFrame])):
try:
x, y, w, h = layer.bounds[frameCount - layer.startFrame][i]
if None in (x, y, w, h):
break
factor = videoReader.w / self.resizeWidth
x, y, w, h = (int(x * factor), int(y * factor), int(w * factor), int(h * factor))
mask = imutils.resize(mask, width=w, height=h + 1)
mask = np.resize(mask, (h, w))
mask = cv2.erode(mask, None, iterations=10)
mask *= 255
frame2 = frames[frameCount - layer.startFrame + layer.exportOffset]
xx = np.copy(
cv2.bitwise_and(
frame2[y : y + h, x : x + w],
frame2[y : y + h, x : x + w],
mask=cv2.bitwise_not(mask),
)
)
frame2[y : y + h, x : x + w] = cv2.addWeighted(
xx,
1,
np.copy(cv2.bitwise_and(frame[y : y + h, x : x + w], frame[y : y + h, x : x + w], mask=mask)),
1,
0,
)
frames[frameCount - layer.startFrame + layer.exportOffset] = np.copy(frame2)
# cv2.imshow("changes x", frame2)
# cv2.waitKey(10) & 0XFF
time = datetime.fromtimestamp(int(frameCount / self.fps) + videoReader.getStartTime())
cv2.putText(
frames[frameCount - layer.startFrame + layer.exportOffset],
f"{time.hour}:{time.minute}:{time.second}",
(int(x + w / 2), int(y + h / 2)),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 255, 255),
2,
)
except:
continue
videoReader.thread.join()
mask = self.getMask(i, frameCount, layer, w, h)
background = frames[frameCount - layer.startFrame + layer.exportOffset]
self.addMaskedContent(frame, x, y, w, h, mask, background)
frames[frameCount - layer.startFrame + layer.exportOffset] = np.copy(background)
self.fps = videoReader.getFPS()
fps = self.fps
writer = imageio.get_writer(self.outputPath, fps=fps)
if showProgress:
cv2.imshow("changes x", background)
cv2.waitKey(10) & 0xFF
self.addTimestamp(frames[frameCount - layer.startFrame + layer.exportOffset], videoReader, frameCount, layer, x, y, w, h)
except:
continue
writer = imageio.get_writer(self.outputPath, fps=videoReader.getFPS())
for frame in frames:
writer.append_data(frame)
writer.close()
def addMaskedContent(self, frame, x, y, w, h, mask, background):
maskedFrame = np.copy(
cv2.bitwise_and(
background[y : y + h, x : x + w],
background[y : y + h, x : x + w],
mask=cv2.bitwise_not(mask),
)
)
background[y : y + h, x : x + w] = cv2.addWeighted(
maskedFrame,
1,
np.copy(cv2.bitwise_and(frame[y : y + h, x : x + w], frame[y : y + h, x : x + w], mask=mask)),
1,
0,
)
def addTimestamp(self, frame, videoReader, frameCount, layer, x, y, w, h):
time = datetime.fromtimestamp(int(frameCount / self.fps) + videoReader.getStartTime())
cv2.putText(
frame,
f"{time.hour}:{time.minute}:{time.second}",
(int(x + w / 2), int(y + h / 2)),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 255, 255),
2,
)
def getMask(self, i, frameCount, layer, w, h):
mask = layer.masks[frameCount - layer.startFrame][i]
mask = imutils.resize(mask, width=w, height=h + 1)
mask = np.resize(mask, (h, w))
mask = cv2.erode(mask, None, iterations=10)
mask *= 255
return mask
def exportRawData(self, layers, contours, masks):
with open(self.config["importPath"], "wb+") as file:
pickle.dump((layers, contours, masks), file)

View File

@ -1,6 +1,6 @@
import numpy as np
import cv2
import imutils
import numpy as np
class Layer:

View File

@ -1,11 +1,12 @@
from Application.Layer import Layer
from Application.Config import Config
from Application.VideoReader import VideoReader
from Application.Exporter import Exporter
from multiprocessing.pool import ThreadPool
import numpy as np
import os
from multiprocessing.pool import ThreadPool
import numpy as np
from Application.Config import Config
from Application.Exporter import Exporter
from Application.Layer import Layer
from Application.VideoReader import VideoReader
class LayerFactory:
@ -88,16 +89,16 @@ class LayerFactory:
def mergeLayers(self, foundLayerIDs):
layers = self.getLayersByID(foundLayerIDs)
layer1 = layers[0]
mergedLayers = layers[0]
for layer in layers[1:]:
for i, (contours, masks) in enumerate(zip(layer.bounds, layer.masks)):
for contour, mask in zip(contours, masks):
layer1.add(layer.startFrame + i, contour, mask)
mergedLayers.add(layer.startFrame + i, contour, mask)
for i, id in enumerate(foundLayerIDs):
del self.layers[id - i]
self.layers.append(layer1)
self.layers.append(mergedLayers)
def joinLayers(self):
self.layers.sort(key=lambda c: c.startFrame)

View File

@ -1,12 +1,14 @@
from Application.Layer import Layer
from Application.Config import Config
from Application.VideoReader import VideoReader
from Application.Exporter import Exporter
import time
from multiprocessing.pool import ThreadPool
from Application.Classifiers.Classifier import Classifier
import cv2
import numpy as np
import time
from Application.Classifiers.Classifier import Classifier
from Application.Config import Config
from Application.Exporter import Exporter
from Application.Layer import Layer
from Application.VideoReader import VideoReader
class LayerManager:
@ -34,7 +36,7 @@ class LayerManager:
print("Before deleting sparse layers ", len(self.layers))
self.deleteSparse()
print("after deleting sparse layers ", len(self.layers))
self.calcTimeOffset()
#self.calcTimeOffset()
def deleteSparse(self):
toDelete = []

View File

@ -1,8 +1,8 @@
import multiprocessing
import os
import threading
import cv2
import threading
import os
class VideoReader:
@ -37,7 +37,6 @@ class VideoReader:
def stop(self):
self.thread.join()
self.vc.release()
def pop(self):
frameNumber, frame = self.buffer.get(block=True)

View File

@ -1,54 +0,0 @@
import math
from PIL import Image, ImageDraw
import random
import imageio
import glob
import os
import numpy as np
fps = 30
xmax = 1920
ymax = 1080
length = 1 # in minutes
numberOfEvents = 4
dirname = os.path.dirname(__file__)
outputPath = os.path.join(dirname, "out.mp4")
def getRandomColorString():
return "#{:06x}".format(random.randint(0, 256 ** 3))
def genVideo():
writer = imageio.get_writer(outputPath, fps=fps)
writer.append_data(np.zeros(shape=[1080, 1920, 3], dtype=np.uint8))
writer.append_data(np.zeros(shape=[1080, 1920, 3], dtype=np.uint8))
for i in range(numberOfEvents):
objectWidth = (5 + random.randint(0, 5)) * xmax / 100
objectHeight = (10 + random.randint(-5, 5)) * ymax / 100
objectX = random.randint(0, xmax)
objectY = random.randint(0, ymax)
objectSpeedX = random.randint(1, 5)
objectSpeedY = random.randint(1, 5)
color = getRandomColorString()
for j in range(int(fps * length * 60 / numberOfEvents)):
objectX -= objectSpeedX
objectY -= objectSpeedY
objectShape = [(objectX, objectY), (objectX + objectWidth, objectY + objectHeight)]
img = Image.new("RGB", (xmax, ymax))
img1 = ImageDraw.Draw(img)
img1.rectangle(objectShape, fill=color)
writer.append_data(np.array(img))
writer.close()
genVideo()

View File

@ -1,34 +0,0 @@
# python
import cv2
import imageio
import time
writer = imageio.get_writer("./x23.mp4", fps=15)
url = "http://50.227.41.1/mjpg/video.mjpg"
i = 0
cap = cv2.VideoCapture(url)
while True:
try:
if i < 10:
i += 1
continue
result, frame = cap.read()
if result == False:
print("Error in cap.read()") # this is for preventing a breaking error
# break;
time.sleep(1)
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
writer.append_data(frame)
i += 1
if i > 20 * 60 * 60 * 2:
break
except Exception as e:
print("meh")
cap.release()
cv2.destroyAllWindows()
writer.close()

View File

@ -45,7 +45,7 @@ if __name__ == "__main__":
fileName = "x23-1.mp4"
outputPath = os.path.join(os.path.dirname(__file__), "output")
inputDirPath = os.path.join(os.path.dirname(__file__), "generate test footage")
inputDirPath = os.path.join(os.path.dirname(__file__), "input")
config["inputPath"] = os.path.join(inputDirPath, fileName)
config["outputPath"] = os.path.join(outputPath, fileName)