diff --git a/ContourExctractor.py b/ContourExctractor.py index 0cf783d..f143ef2 100644 --- a/ContourExctractor.py +++ b/ContourExctractor.py @@ -16,8 +16,8 @@ class ContourExtractor: #X = {frame_number: [(contour, (x,y,w,h)), ...], } extractedContours = dict() min_area = 500 - max_area = 5000 - threashold = 20 + max_area = 7000 + threashold = 13 xDim = 0 yDim = 0 @@ -51,8 +51,10 @@ class ContourExtractor: frame = imutils.resize(frame, width=resizeWidth) - gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - gray = cv2.GaussianBlur(gray, (5, 5), 0) + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB) + gray = np.asarray(gray[:,:,1]/2 + gray[:,:,2]/2).astype(np.uint8) + + #gray = cv2.GaussianBlur(gray, (5, 5), 0) # if the first frame is None, initialize it if firstFrame is None: @@ -60,6 +62,7 @@ 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) @@ -78,9 +81,12 @@ class ContourExtractor: #cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) if len(contours) != 0: extractedContours[frameCount] = contours + + if frameCount % (60*30) == 0: + print("Minutes processed: ", frameCount/(60*30)) frameCount += 1 - #cv2.imshow( "annotated", frame ) + #cv2.imshow( "annotated", thresh ) #cv2.waitKey(10) & 0XFF self.extractedContours = extractedContours return extractedContours diff --git a/Exporter.py b/Exporter.py index 36f406e..bcdca3c 100644 --- a/Exporter.py +++ b/Exporter.py @@ -16,19 +16,21 @@ class Exporter: writer.append_data(np.array(frame)) writer.close() - def exportLayers(self, layers, outputPath): + def exportLayers(self,underlay, layers, outputPath, resizeWidth): fps = self.fps writer = imageio.get_writer(outputPath, fps=fps) i=0 for layer in layers: data = layer.data + contours = layer.bounds if len(data) < 10: continue - for frame in data: - (x, y, w, h) = frame[1] - frame = frame[0] - frame1 = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8) - frame1 = imutils.resize(frame1, width=512) + for frame, contour in zip(data, contours): + (x, y, w, h) = contour + frame = frame + frame1 = underlay + frame1 = imutils.resize(frame1, width=resizeWidth) + frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB) frame1[y:y+frame.shape[0], x:x+frame.shape[1]] = frame cv2.putText(frame1, str(i), (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1,(255,255,255), 2) writer.append_data(np.array(frame1)) diff --git a/LayerFactory.py b/LayerFactory.py index cb074ba..77a1a55 100644 --- a/LayerFactory.py +++ b/LayerFactory.py @@ -47,7 +47,7 @@ class LayerFactory: print("LayerFactory: Layer knew no bounds") continue - if frameNumber - layer.lastFrame <= 20: + if frameNumber - layer.lastFrame <= 5: (x2,y2,w2,h2) = layer.bounds[-1] if self.contoursOverlay((x-tol,y+h+tol), (x+w+tol,y-tol), (x2,y2+h2), (x2+w2,y2)): foundLayer = True diff --git a/__pycache__/ContourExctractor.cpython-37.pyc b/__pycache__/ContourExctractor.cpython-37.pyc index 5039fb1..44b2a71 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 79a35c2..c0c2bcf 100644 Binary files a/__pycache__/Exporter.cpython-37.pyc and b/__pycache__/Exporter.cpython-37.pyc differ diff --git a/__pycache__/LayerFactory.cpython-37.pyc b/__pycache__/LayerFactory.cpython-37.pyc index bfde8e3..4c526cf 100644 Binary files a/__pycache__/LayerFactory.cpython-37.pyc and b/__pycache__/LayerFactory.cpython-37.pyc differ diff --git a/main.py b/main.py index c945e94..ffeeab3 100644 --- a/main.py +++ b/main.py @@ -10,10 +10,10 @@ import cv2 def demo(): print("startup") resizeWidth = 512 - maxLayerLength = 5*50*30 + maxLayerLength = 1*60*30 start = time.time() - footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/3.MP4") + footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/out.mp4") contours = ContourExtractor().extractContours(footagePath, resizeWidth) print("Time consumed in working: ", time.time() - start) layerFactory = LayerFactory(contours) @@ -21,7 +21,7 @@ def demo(): layerFactory.sortLayers() layerFactory.fillLayers(footagePath) underlay = cv2.VideoCapture(footagePath).read()[1] - Exporter().exportOverlayed(underlay, layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4"), resizeWidth) + Exporter().exportLayers(underlay, layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4"), resizeWidth) print("Total time: ", time.time() - start) def init(): print("not needed yet") diff --git a/short.mp4 b/short.mp4 index 76ba390..0fc15fd 100644 Binary files a/short.mp4 and b/short.mp4 differ