still shit, but better

This commit is contained in:
Askill 2020-09-28 22:28:23 +02:00
parent 2ae7520d2a
commit efa299a9f5
9 changed files with 39 additions and 36 deletions

View File

@ -15,7 +15,7 @@ class ContourExtractor:
#X = {frame_number: [(contour, (x,y,w,h)), ...], } #X = {frame_number: [(contour, (x,y,w,h)), ...], }
extractedContours = dict() extractedContours = dict()
min_area = 0 min_area = 200
max_area = 30000 max_area = 30000
threashold = 25 threashold = 25
xDim = 0 xDim = 0
@ -24,9 +24,10 @@ class ContourExtractor:
def getextractedContours(self): def getextractedContours(self):
return self.extractedContours return self.extractedContours
def __init__(self, videoPath): def __init__(self):
print("ContourExtractor initiated") print("ContourExtractor initiated")
def extractContours(self, videoPath):
min_area = self.min_area min_area = self.min_area
max_area = self.max_area max_area = self.max_area
threashold = self.threashold threashold = self.threashold
@ -40,15 +41,15 @@ class ContourExtractor:
firstFrame = None firstFrame = None
# loop over the frames of the video # loop over the frames of the video
frameCount = 0 frameCount = 0
extractedContours = dict()
while res: while res:
res, frame = vs.read() res, frame = vs.read()
# resize the frame, convert it to grayscale, and blur it # resize the frame, convert it to grayscale, and blur it
if frame is None: if frame is None:
print("ContourExtractor: frame was None") print("ContourExtractor: frame was None")
return break
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.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0) gray = cv2.GaussianBlur(gray, (5, 5), 0)
@ -66,18 +67,22 @@ class ContourExtractor:
contours = [] contours = []
for c in cnts: for c in cnts:
if cv2.contourArea(c) < min_area or cv2.contourArea(c) > max_area: ca = cv2.contourArea(c)
if ca < min_area or ca > max_area:
continue continue
(x, y, w, h) = cv2.boundingRect(c) (x, y, w, h) = cv2.boundingRect(c)
#print((x, y, w, h))
contours.append((frame[y:y+h, x:x+w], (x, y, w, h))) 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) #cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
self.extractedContours[frameCount] = contours if len(contours) != 0:
extractedContours[frameCount] = contours
frameCount += 1 frameCount += 1
#cv2.imshow( "annotated", frame ) #cv2.imshow( "annotated", frame )
#cv2.waitKey(10) & 0XFF #cv2.waitKey(10) & 0XFF
self.extractedContours = extractedContours
def displayContours(self): def displayContours(self):
values = self.extractedContours.values() values = self.extractedContours.values()

View File

@ -17,21 +17,19 @@ class Exporter:
writer.close() writer.close()
def exportLayers(self, layers, outputPath): def exportLayers(self, layers, outputPath):
for layer in layers:
data = layer.data
fps = self.fps fps = self.fps
writer = imageio.get_writer(outputPath, fps=fps) writer = imageio.get_writer(outputPath, fps=fps)
for layer in layers:
data = layer.data
for frame in data: for frame in data:
(x, y, w, h) = frame[1] (x, y, w, h) = frame[1]
frame = frame[0] frame = frame[0]
frame1 = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8) frame1 = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8)
frame1 = imutils.resize(frame1, width=512) frame1 = imutils.resize(frame1, width=512)
frame1[y:y+frame.shape[0], x:x+frame.shape[1]] = frame frame1[y:y+frame.shape[0], x:x+frame.shape[1]] = frame
writer.append_data(np.array(frame1)) writer.append_data(np.array(frame1))
cv2.imshow("changes overlayed", frame) #cv2.imshow("changes overlayed", frame)
cv2.waitKey(10) & 0XFF #cv2.waitKey(10) & 0XFF
writer.close() writer.close()
cv2.destroyAllWindows() #cv2.destroyAllWindows()

View File

@ -5,12 +5,12 @@ class Layer:
lastFrame = 0 lastFrame = 0
backgroundImage = [] backgroundImage = []
def __init__(self, startFrame, data, backgroundImage): def __init__(self, startFrame, data):
self.startFrame = startFrame self.startFrame = startFrame
self.lastFrame = startFrame
self.data.append(data) self.data.append(data)
self.backgroundImage = backgroundImage
print("Layer constructed") #print("Layer constructed")
def add(self, frameNumber, data): def add(self, frameNumber, data):
self.lastFrame = frameNumber self.lastFrame = frameNumber

View File

@ -22,26 +22,24 @@ class LayerFactory:
contours = data[frameNumber] contours = data[frameNumber]
for contour in contours: for contour in contours:
layers.append(Layer(frameNumber, contour, None)) layers.append(Layer(frameNumber, contour))
# inserts all the fucking contours as layers?
for frameNumber, contours in data.items(): for frameNumber, contours in data.items():
layerNum = len(layers)
i = 0
while i < layerNum:
layer = layers[i]
if frameNumber - layer.lastFrame < 5:
for contour, (x,y,w,h) in contours: for contour, (x,y,w,h) in contours:
for layer in layers:
if frameNumber - layer.lastFrame <= 5:
if len(layer.data[-1][1]) != 4: if len(layer.data[-1][1]) != 4:
print("LayerFactory: Layer knew no bounds") print("LayerFactory: Layer knew no bounds")
continue continue
(x2,y2,w2,h2) = layer.data[-1][1]
if self.contoursOverlay((x,y+h), (x+w,y), (x2,y2+h2), (x2+w2,y2)): (x2,y2,w2,h2) = layer.data[-1][1]
tol = 10
if self.contoursOverlay((x-tol,y+h+tol), (x+w+tol,y-tol), (x2,y2+h2), (x2+w2,y2)):
layer.add(frameNumber, (contour, (x,y,w,h))) layer.add(frameNumber, (contour, (x,y,w,h)))
else: break
layers.append(Layer(frameNumber, contour, None))
layerNum = len(layers) layers.append(Layer(frameNumber, (contour, (x,y,w,h))))
i+=1
self.layers = layers self.layers = layers

Binary file not shown.

Binary file not shown.

10
main.py
View File

@ -11,17 +11,19 @@ from LayerFactory import LayerFactory
def demo(): def demo():
print("startup") print("startup")
footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/2.mp4") footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/out.mp4")
start = time.time() start = time.time()
contourExtractor = ContourExtractor(footagePath) contourExtractor = ContourExtractor()
contourExtractor.extractContours(footagePath)
print("Time consumed in working: ",time.time() - start) print("Time consumed in working: ",time.time() - start)
frames = contourExtractor.exportContours() #frames = contourExtractor.exportContours()
#Exporter().export(frames,os.path.join(os.path.dirname(__file__), "./short.mp4")) #Exporter().export(frames,os.path.join(os.path.dirname(__file__), "./short.mp4"))
contours = contourExtractor.getextractedContours() contours = contourExtractor.getextractedContours()
layerFactory = LayerFactory(contourExtractor.extractedContours) layerFactory = LayerFactory(contours)
Exporter().exportLayers(layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4")) Exporter().exportLayers(layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4"))
def init(): def init():