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/images/
generate test footage/3.MP4 generate test footage/3.MP4
input/*
short.mp4 short.mp4
__pycache__/ __pycache__/

View File

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

View File

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

View File

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

View File

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

View File

@ -1,13 +1,13 @@
from Application.Config import Config import pickle
from Application.Layer import Layer import time
from Application.VideoReader import VideoReader
from datetime import datetime from datetime import datetime
import cv2
import imageio import imageio
import imutils import imutils
import numpy as np import numpy as np
import cv2
import pickle from Application.VideoReader import VideoReader
import time
class Exporter: class Exporter:
@ -20,144 +20,139 @@ class Exporter:
self.config = config self.config = config
print("Exporter initiated") 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: if raw:
self.exportRawData(layers, contours, masks) self.exportRawData(layers, contours, masks)
if overlayed: if overlayed:
self.exportOverlayed(layers) self.exportOverlayed(layers, blackBackground, showProgress)
else: else:
self.exportLayers(layers) self.exportLayers(layers)
def exportLayers(self, layers): def exportLayers(self, layers):
# TODO: fix videoreader instanciation
listOfFrames = self.makeListOfFrames(layers) listOfFrames = self.makeListOfFrames(layers)
videoReader = VideoReader(self.config, listOfFrames) with VideoReader(self.config, listOfFrames) as videoReader:
videoReader.fillBuffer()
maxLength = self.getMaxLengthOfLayers(layers) underlay = cv2.VideoCapture(self.footagePath).read()[1]
underlay = cv2.VideoCapture(self.footagePath).read()[1] underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB)
exportFrame = 0
self.fps = videoReader.getFPS() fps = videoReader.getFPS()
writer = imageio.get_writer(self.outputPath, fps=self.fps) writer = imageio.get_writer(self.outputPath, fps=fps)
start = time.time() start = time.time()
for i, layer in enumerate(layers): 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") 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: if len(layer.bounds[0]) == 0:
continue continue
videoReader = VideoReader(self.config) videoReader = VideoReader(self.config)
listOfFrames = self.makeListOfFrames([layer]) listOfFrames = self.makeListOfFrames([layer])
videoReader.fillBuffer(listOfFrames) videoReader.fillBuffer(listOfFrames)
while not videoReader.videoEnded(): while not videoReader.videoEnded():
frameCount, frame = videoReader.pop() frameCount, frame = videoReader.pop()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame2 = np.copy(underlay) frame2 = np.copy(underlay)
for (x, y, w, h) in layer.bounds[frameCount - layer.startFrame]: for (x, y, w, h) in layer.bounds[frameCount - layer.startFrame]:
if x is None: if x is None:
continue continue
factor = videoReader.w / self.resizeWidth factor = videoReader.w / self.resizeWidth
x, y, w, h = (int(x * factor), int(y * factor), int(w * factor), int(h * factor)) 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()) self.addTimestamp(frame2, videoReader, frameCount, layer, x, y, w, h)
cv2.putText( writer.append_data(frame2)
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()
def exportOverlayed(self, layers): writer.close()
def exportOverlayed(self, layers, blackBackground=False, showProgress=False):
listOfFrames = self.makeListOfFrames(layers) listOfFrames = self.makeListOfFrames(layers)
videoReader = VideoReader(self.config, listOfFrames)
videoReader.fillBuffer()
maxLength = self.getMaxLengthOfLayers(layers) maxLength = self.getMaxLengthOfLayers(layers)
underlay = cv2.VideoCapture(self.footagePath).read()[1]
underlay = cv2.cvtColor(underlay, cv2.COLOR_BGR2RGB) if blackBackground:
# underlay = np.zeros(shape=[videoReader.h, videoReader.w, 3], dtype=np.uint8) 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 = [] frames = []
for i in range(maxLength): for i in range(maxLength):
frames.append(np.copy(underlay)) frames.append(np.copy(underlay))
exportFrame = 0
while not videoReader.videoEnded(): with VideoReader(self.config, listOfFrames) as videoReader:
frameCount, frame = videoReader.pop() while not videoReader.videoEnded():
if frameCount % (60 * self.fps) == 0: frameCount, frame = videoReader.pop()
print("Minutes processed: ", frameCount / (60 * self.fps), end="\r") if frameCount % (60 * self.fps) == 0:
if frame is None: print("Minutes processed: ", frameCount / (60 * self.fps), end="\r")
print("ContourExtractor: frame was None") if frame is None:
continue print("ContourExtractor: frame was None")
continue
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
for layer in layers: for layer in layers:
if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount: if layer.startFrame <= frameCount and layer.startFrame + len(layer.bounds) > frameCount:
for i in range(0, len(layer.bounds[frameCount - layer.startFrame])): for i in range(0, len(layer.bounds[frameCount - layer.startFrame])):
try: try:
underlay1 = underlay x, y, w, h = layer.bounds[frameCount - layer.startFrame][i]
(x, y, w, h) = layer.bounds[frameCount - layer.startFrame][i] if None in (x, y, w, h):
mask = layer.masks[frameCount - layer.startFrame][i] break
if x is None: factor = videoReader.w / self.resizeWidth
break x, y, w, h = (int(x * factor), int(y * factor), int(w * factor), int(h * factor))
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 = self.getMask(i, frameCount, layer, w, h)
mask = np.resize(mask, (h, w)) background = frames[frameCount - layer.startFrame + layer.exportOffset]
mask = cv2.erode(mask, None, iterations=10) self.addMaskedContent(frame, x, y, w, h, mask, background)
mask *= 255 frames[frameCount - layer.startFrame + layer.exportOffset] = np.copy(background)
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()
self.fps = videoReader.getFPS() if showProgress:
fps = self.fps cv2.imshow("changes x", background)
writer = imageio.get_writer(self.outputPath, fps=fps) 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: for frame in frames:
writer.append_data(frame) writer.append_data(frame)
writer.close() 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): def exportRawData(self, layers, contours, masks):
with open(self.config["importPath"], "wb+") as file: with open(self.config["importPath"], "wb+") as file:
pickle.dump((layers, contours, masks), file) pickle.dump((layers, contours, masks), file)

View File

@ -1,6 +1,6 @@
import numpy as np
import cv2 import cv2
import imutils import imutils
import numpy as np
class Layer: 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 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: class LayerFactory:
@ -88,16 +89,16 @@ class LayerFactory:
def mergeLayers(self, foundLayerIDs): def mergeLayers(self, foundLayerIDs):
layers = self.getLayersByID(foundLayerIDs) layers = self.getLayersByID(foundLayerIDs)
layer1 = layers[0] mergedLayers = layers[0]
for layer in layers[1:]: for layer in layers[1:]:
for i, (contours, masks) in enumerate(zip(layer.bounds, layer.masks)): for i, (contours, masks) in enumerate(zip(layer.bounds, layer.masks)):
for contour, mask in zip(contours, 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): for i, id in enumerate(foundLayerIDs):
del self.layers[id - i] del self.layers[id - i]
self.layers.append(layer1) self.layers.append(mergedLayers)
def joinLayers(self): def joinLayers(self):
self.layers.sort(key=lambda c: c.startFrame) self.layers.sort(key=lambda c: c.startFrame)

View File

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

View File

@ -1,8 +1,8 @@
import multiprocessing import multiprocessing
import os
import threading
import cv2 import cv2
import threading
import os
class VideoReader: class VideoReader:
@ -37,7 +37,6 @@ class VideoReader:
def stop(self): def stop(self):
self.thread.join() self.thread.join()
self.vc.release()
def pop(self): def pop(self):
frameNumber, frame = self.buffer.get(block=True) 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" fileName = "x23-1.mp4"
outputPath = os.path.join(os.path.dirname(__file__), "output") 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["inputPath"] = os.path.join(inputDirPath, fileName)
config["outputPath"] = os.path.join(outputPath, fileName) config["outputPath"] = os.path.join(outputPath, fileName)