From c828e2e0d2d67f4adff00f34eb39a05861380c67 Mon Sep 17 00:00:00 2001 From: Askill Date: Sun, 20 Dec 2020 15:49:43 +0100 Subject: [PATCH] added timestamps to video out fxed fps in overlayed --- Application/Exporter.py | 12 ++++++++---- Application/VideoReader.py | 21 +++++++++++++++++++-- main.py | 19 ++++++++++--------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/Application/Exporter.py b/Application/Exporter.py index d67c89a..f9650de 100644 --- a/Application/Exporter.py +++ b/Application/Exporter.py @@ -6,6 +6,7 @@ import cv2 from Application.VideoReader import VideoReader import pickle import time +from datetime import datetime class Exporter: fps = 30 @@ -58,8 +59,10 @@ class Exporter: 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]) - cv2.putText(frame2, str(i) + " " + str(int(frameCount/self.fps)), (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) + + time = datetime.fromtimestamp(int(frameCount/self.fps) + videoReader.getStartTime()) + cv2.putText(frame2, str(i) + " " + f"{time.hour}:{time.minute}:{time.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() @@ -81,7 +84,7 @@ class Exporter: frames.append(np.copy(underlay)) exportFrame = 0 - + while not videoReader.videoEnded(): frameCount, frame = videoReader.pop() if frameCount % (60*self.fps) == 0: @@ -112,7 +115,8 @@ class Exporter: frames[frameCount - layer.startFrame] = np.copy(frame2) #cv2.imshow("changes x", frame2) #cv2.waitKey(10) & 0XFF - cv2.putText(frames[frameCount - layer.startFrame], str(int(frameCount/self.fps)), (int(x+w/2), int(y+h/2)), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255), 2) + time = datetime.fromtimestamp(int(frameCount/self.fps) + videoReader.getStartTime()) + cv2.putText(frames[frameCount - layer.startFrame], f"{time.hour}:{time.minute}:{time.second}", (int(x+w/2), int(y+h/2)), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255), 2) videoReader.thread.join() videoReader.vc.release() diff --git a/Application/VideoReader.py b/Application/VideoReader.py index 83424f6..23a49dc 100644 --- a/Application/VideoReader.py +++ b/Application/VideoReader.py @@ -4,8 +4,10 @@ import cv2 from time import sleep from queue import Queue import threading +import pathlib from Application.Config import Config - +import os +from datetime import datetime class VideoReader: listOfFrames = None @@ -25,6 +27,8 @@ class VideoReader: self.vc = cv2.VideoCapture(videoPath) self.stopped = False self.getWH() + self.calcFPS() + self.calcStartTime() if setOfFrames is not None: self.listOfFrames = sorted(setOfFrames) @@ -95,5 +99,18 @@ class VideoReader: else: return False + def calcFPS(self): + self.fps = self.vc.get(cv2.CAP_PROP_FPS) + def getFPS(self): - return self.vc.get(cv2.CAP_PROP_FPS) + return self.fps + + def calcStartTime(self): + starttime = os.stat(self.videoPath).st_mtime + fc = int(self.vc.get(cv2.CAP_PROP_FRAME_COUNT)) + length = fc / self.getFPS() + starttime = starttime - length + self.starttime = starttime + + def getStartTime(self): + return self.starttime diff --git a/main.py b/main.py index 7f22c6f..cbe1a92 100644 --- a/main.py +++ b/main.py @@ -25,38 +25,39 @@ def main(): config["importPath"] = os.path.join(outputPath, fileName.split(".")[0] + ".txt") config["w"], config["h"] = VideoReader(config).getWH() - stats = dict() + stats = [] if not os.path.exists(config["importPath"]): contours, masks = ContourExtractor(config).extractContours() - stats["Contour Extractor"] = time.time() - start + stats.append(time.time() - start) start = time.time() print("Time consumed extracting contours: ", stats["Contour Extractor"]) layerFactory = LayerFactory(config) layers = layerFactory.extractLayers(contours, masks) - stats["Layer Factory"] = time.time() - start + stats.append(time.time() - start) start = time.time() else: layers, contours, masks = Importer(config).importRawData() - layerFactory = LayerFactory(config) - layers = layerFactory.extractLayers(contours, masks) + #layerFactory = LayerFactory(config) + #layers = layerFactory.extractLayers(contours, masks) layerManager = LayerManager(config, layers) layerManager.transformLayers() - stats["Layer Manager"] = time.time() - start + stats.append(time.time() - start) start = time.time() #layerManager.tagLayers() layers = layerManager.layers - print([len(l) for l in sorted(layers, key = lambda c:len(c), reverse=True)[:20]]) + #print([len(l) for l in sorted(layers, key = lambda c:len(c), reverse=True)[:20]]) if len(layers) == 0: exit(1) exporter = Exporter(config) print(f"Exporting {len(contours)} Contours and {len(layers)} Layers") - exporter.export(layers, contours, masks, raw=True, overlayed=False) - stats["Exporter"] = time.time() - start + exporter.export(layers, contours, masks, raw=True, overlayed=True) + stats.append(time.time() - start) print("Total time: ", time.time() - startTotal) + stats.append(time.time() - startTotal) print(stats) exit(0)