From 90c07016fe5c7a6242ee3a0e72b7e20fdc8f6a50 Mon Sep 17 00:00:00 2001 From: Askill Date: Sat, 28 Nov 2020 11:03:24 +0100 Subject: [PATCH] continued contoured extraction TODO: fix rendering --- Application/ContourExctractor.py | 10 ++++++---- Application/Exporter.py | 18 ++++++++++++------ main.py | 6 +++--- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Application/ContourExctractor.py b/Application/ContourExctractor.py index 3e0aa69..560b089 100644 --- a/Application/ContourExctractor.py +++ b/Application/ContourExctractor.py @@ -63,9 +63,9 @@ class ContourExtractor: tmpData = [videoReader.pop() for i in range(0, videoReader.buffer.qsize())] self.computeMovingAverage(tmpData) - pool.map(self.getContours, tmpData) - #for data in tmpData: - # self.getContours(data) + #pool.map(self.getContours, tmpData) + for data in tmpData: + self.getContours(data) frameCount = tmpData[-1][0] videoReader.thread.join() @@ -105,6 +105,8 @@ class ContourExtractor: contours.append((x, y, w, h)) masks.append(np.packbits(np.copy(thresh[y:y+h,x:x+w]), axis=0)) + + if len(contours) != 0 and contours is not None: # this should be thread safe self.extractedContours[frameCount] = contours @@ -113,7 +115,7 @@ class ContourExtractor: def prepareFrame(self, frame): frame = imutils.resize(frame, width=self.resizeWidth) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - gray = cv2.GaussianBlur(gray, (5, 5), 0) + gray = cv2.GaussianBlur(gray, (3, 3), 0) return gray def computeMovingAverage(self, frames): diff --git a/Application/Exporter.py b/Application/Exporter.py index df2ae7a..f66a90c 100644 --- a/Application/Exporter.py +++ b/Application/Exporter.py @@ -17,9 +17,9 @@ class Exporter: self.config = config print("Exporter initiated") - def export(self, layers, contours, raw = True, overlayed = True): + def export(self, layers, contours, masks, raw = True, overlayed = True): if raw: - self.exportRawData(layers, contours) + self.exportRawData(layers, contours, masks) if overlayed: self.exportOverlayed(layers) else: @@ -72,6 +72,7 @@ class Exporter: h = 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) writer.append_data(frame2) @@ -115,13 +116,18 @@ class Exporter: y = int(y * factor) w = int(w * factor) h = int(h * factor) - mask = imutils.resize(mask, width=w, height=h+1)*255 + mask = imutils.resize(mask, width=w, height=h+1) mask = np.resize(mask, (h,w)) - # if exportFrame as index instead of frameCount - layer.startFrame then we have layer after layer + frame2 = frames[frameCount - layer.startFrame] - frame2[y:y+h, x:x+w] = cv2.bitwise_and(frame2[y:y+h, x:x+w],frame2[y:y+h, x:x+w], mask)# + frame2[y:y+h, x:x+w]/2 + #frame2[y:y+h, x:x+w] = cv2.bitwise_or(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] = np.zeros((h, w, 3)) + frame2[y:y+h, x:x+w] = np.copy(cv2.bitwise_and(underlay[y:y+h, x:x+w], underlay[y:y+h, x:x+w], mask=cv2.bitwise_not(mask))) + frame2[y:y+h, x:x+w] = cv2.addWeighted(np.copy(frame2[y:y+h, x:x+w]),1, np.copy(cv2.bitwise_and(frame[y:y+h, x:x+w], frame[y:y+h, x:x+w], mask=mask)),.5,0) 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) videoReader.thread.join() @@ -135,7 +141,7 @@ class Exporter: writer.close() - def exportRawData(self, layers, contours): + def exportRawData(self, layers, contours, masks): with open(self.config["importPath"], "wb+") as file: pickle.dump((layers, contours, masks), file) diff --git a/main.py b/main.py index 150ae8a..fc14953 100644 --- a/main.py +++ b/main.py @@ -32,8 +32,8 @@ def main(): layers = layerFactory.extractLayers(contours, masks) 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() @@ -42,7 +42,7 @@ def main(): layers = layerManager.layers exporter = Exporter(config) print(f"Exporting {len(contours)} Contours and {len(layers)} Layers") - exporter.export(layers, contours, raw=True, overlayed=True) + exporter.export(layers, contours, masks, raw=True, overlayed=True) print("Total time: ", time.time() - start) exit(0)