diff --git a/ContourExctractor.py b/ContourExctractor.py index fd0edbf..880a9a4 100644 --- a/ContourExctractor.py +++ b/ContourExctractor.py @@ -8,15 +8,17 @@ import os import numpy as np import traceback import _thread +import imageio +import numpy as np class ContourExtractor: - #X = {frame_number: contours, } + #X = {frame_number: [(contour, (x,y,w,h)), ...], } extractedContours = dict() def __init__(self, videoPath): - print("ContourExtractror initiated") - print(videoPath) + print("ContourExtractor initiated") + min_area = 100 max_area = 30000 @@ -37,10 +39,10 @@ class ContourExtractor: # resize the frame, convert it to grayscale, and blur it if frame is None: return - #frame = imutils.resize(frame, width=500) + frame = imutils.resize(frame, width=500) cv2.imshow( "frame", frame ) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - gray = cv2.GaussianBlur(gray, (31, 31), 0) + gray = cv2.GaussianBlur(gray, (5, 5), 0) # if the first frame is None, initialize it if firstFrame is None: @@ -58,15 +60,17 @@ class ContourExtractor: cnts = imutils.grab_contours(cnts) # loop over the contours + contours = [] for c in cnts: if cv2.contourArea(c) < min_area or cv2.contourArea(c) > max_area: continue (x, y, w, h) = cv2.boundingRect(c) - cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) + contours.append((frame[y:y+h, x:x+w], (x, y, w, h))) + #cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) text = "Occupied" - self.extractedContours[frameCount] = cnts + self.extractedContours[frameCount] = contours frameCount += 1 #cv2.imshow( "annotated", frame ) @@ -77,14 +81,24 @@ class ContourExtractor: values = self.extractedContours.values() frame = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8) - #frame = imutils.resize(frame, width=500) + frame = imutils.resize(frame, width=512) + frames = [] + writer = imageio.get_writer(os.path.join(os.path.dirname(__file__), "./short.mp4"), fps=30) + for xx in values: + for v1 in xx: + (x, y, w, h) = v1[1] + v = v1[0] + + #cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) - for x in values: - for v in x: - (x, y, w, h) = cv2.boundingRect(v) - cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) + frame[y:y+v.shape[0], x:x+v.shape[1]] = v + frames.append(frame) + writer.append_data(np.array(frame)) + #cv2.imshow("changes overlayed", frame) + #cv2.waitKey(10) & 0XFF + #cv2.waitKey(0) + #cv2.destroyAllWindows() - cv2.imshow("changes overlayed", frame) - cv2.waitKey(0) - cv2.destroyAllWindows() + writer.close() + return frames diff --git a/Exporter.py b/Exporter.py index 5d61c84..93ac311 100644 --- a/Exporter.py +++ b/Exporter.py @@ -1,3 +1,12 @@ +import imageio +import numpy as np class Exporter: - def __init__(self): - print("Layer constructed") \ No newline at end of file + fps = 30 + def __init__(self, data, outputPath): + print("Exporter initiated") + fps = self.fps + writer = imageio.get_writer(outputPath, fps=fps) + for frame in data: + writer.append_data(np.array(frame)) + + writer.close() \ No newline at end of file diff --git a/__pycache__/ContourExctractor.cpython-37.pyc b/__pycache__/ContourExctractor.cpython-37.pyc index eea2bae..be4952a 100644 Binary files a/__pycache__/ContourExctractor.cpython-37.pyc and b/__pycache__/ContourExctractor.cpython-37.pyc differ diff --git a/__pycache__/Exporter.cpython-37.pyc b/__pycache__/Exporter.cpython-37.pyc new file mode 100644 index 0000000..3d1e35f Binary files /dev/null and b/__pycache__/Exporter.cpython-37.pyc differ diff --git a/main.py b/main.py index a1206a8..e4ae856 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,14 @@ import os import time from ContourExctractor import ContourExtractor - +from Exporter import Exporter #TODO -# finden von relevanten Stellen anhand von zu findenen metriken +# finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder +# diff zu den ref bildern aufnehmen +# zeichenn on contour inhalt +# langes video -def init(): +def demo(): print("startup") footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/out.mp4") @@ -13,11 +16,13 @@ def init(): contourExtractor = ContourExtractor(footagePath) print("Time consumed in working: ",time.time() - start) - contourExtractor.displayContours() - + frames = contourExtractor.displayContours() + #exporter = Exporter(frames,os.path.join(os.path.dirname(__file__), "./short.mp4")) +def init(): + print("not needed yet") if __name__ == "__main__": - init() + demo() diff --git a/short.mp4 b/short.mp4 new file mode 100644 index 0000000..ff567ec Binary files /dev/null and b/short.mp4 differ