diff --git a/ContourExctractor.py b/ContourExctractor.py index 880a9a4..57bb7ab 100644 --- a/ContourExctractor.py +++ b/ContourExctractor.py @@ -15,32 +15,38 @@ class ContourExtractor: #X = {frame_number: [(contour, (x,y,w,h)), ...], } extractedContours = dict() + min_area = 990 + max_area = 30000 + threashold = 25 + xDim = 0 + yDim = 0 def __init__(self, videoPath): print("ContourExtractor initiated") - min_area = 100 - max_area = 30000 - - threashold = 10 + min_area = self.min_area + max_area = self.max_area + threashold = self.threashold # initialize the first frame in the video stream vs = cv2.VideoCapture(videoPath) - res = vs.read()[0] + res, image = vs.read() + self.xDim = image.shape[1] + self.yDim = image.shape[0] firstFrame = None # loop over the frames of the video frameCount = 0 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") return + frame = imutils.resize(frame, width=500) - cv2.imshow( "frame", frame ) + cv2.imshow( "frame", frame) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) @@ -50,16 +56,12 @@ class ContourExtractor: 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) - # loop over the contours contours = [] for c in cnts: if cv2.contourArea(c) < min_area or cv2.contourArea(c) > max_area: @@ -68,8 +70,6 @@ class ContourExtractor: (x, y, w, h) = cv2.boundingRect(c) 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] = contours frameCount += 1 @@ -78,27 +78,22 @@ class ContourExtractor: #cv2.waitKey(10) & 0XFF def displayContours(self): - values = self.extractedContours.values() - frame = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8) - 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] - + frame = np.zeros(shape=[self.yDim, self.xDim, 3], dtype=np.uint8) + frame = imutils.resize(frame, width=512) #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() - - writer.close() return frames diff --git a/Exporter.py b/Exporter.py index 93ac311..69dff46 100644 --- a/Exporter.py +++ b/Exporter.py @@ -2,11 +2,14 @@ import imageio import numpy as np class Exporter: fps = 30 - def __init__(self, data, outputPath): + + def __init__(self): print("Exporter initiated") + + def export(self, frames, outputPath): fps = self.fps writer = imageio.get_writer(outputPath, fps=fps) - for frame in data: + for frame in frames: writer.append_data(np.array(frame)) - writer.close() \ No newline at end of file + writer.close() diff --git a/__pycache__/ContourExctractor.cpython-37.pyc b/__pycache__/ContourExctractor.cpython-37.pyc index be4952a..9da8d1d 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 index 3d1e35f..6ff4a19 100644 Binary files a/__pycache__/Exporter.cpython-37.pyc and b/__pycache__/Exporter.cpython-37.pyc differ diff --git a/generate test footage/2.mp4 b/generate test footage/2.mp4 new file mode 100644 index 0000000..b6ec884 Binary files /dev/null and b/generate test footage/2.mp4 differ diff --git a/main.py b/main.py index e4ae856..0f72857 100644 --- a/main.py +++ b/main.py @@ -4,20 +4,21 @@ from ContourExctractor import ContourExtractor from Exporter import Exporter #TODO # finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder -# diff zu den ref bildern aufnehmen -# zeichenn on contour inhalt +# X diff zu den ref bildern aufnehmen +# zeichen von contour inhalt # langes video def demo(): print("startup") - footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/out.mp4") + footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/2.mp4") start = time.time() contourExtractor = ContourExtractor(footagePath) print("Time consumed in working: ",time.time() - start) frames = contourExtractor.displayContours() - #exporter = Exporter(frames,os.path.join(os.path.dirname(__file__), "./short.mp4")) + Exporter().export(frames,os.path.join(os.path.dirname(__file__), "./short.mp4")) + def init(): print("not needed yet") diff --git a/short.mp4 b/short.mp4 index ff567ec..57dcc26 100644 Binary files a/short.mp4 and b/short.mp4 differ