still shit, but better
This commit is contained in:
parent
2ae7520d2a
commit
efa299a9f5
|
|
@ -15,7 +15,7 @@ class ContourExtractor:
|
|||
|
||||
#X = {frame_number: [(contour, (x,y,w,h)), ...], }
|
||||
extractedContours = dict()
|
||||
min_area = 0
|
||||
min_area = 200
|
||||
max_area = 30000
|
||||
threashold = 25
|
||||
xDim = 0
|
||||
|
|
@ -24,9 +24,10 @@ class ContourExtractor:
|
|||
def getextractedContours(self):
|
||||
return self.extractedContours
|
||||
|
||||
def __init__(self, videoPath):
|
||||
def __init__(self):
|
||||
print("ContourExtractor initiated")
|
||||
|
||||
def extractContours(self, videoPath):
|
||||
min_area = self.min_area
|
||||
max_area = self.max_area
|
||||
threashold = self.threashold
|
||||
|
|
@ -40,15 +41,15 @@ class ContourExtractor:
|
|||
firstFrame = None
|
||||
# loop over the frames of the video
|
||||
frameCount = 0
|
||||
extractedContours = dict()
|
||||
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
|
||||
break
|
||||
|
||||
frame = imutils.resize(frame, width=500)
|
||||
cv2.imshow( "frame", frame)
|
||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||
gray = cv2.GaussianBlur(gray, (5, 5), 0)
|
||||
|
||||
|
|
@ -66,18 +67,22 @@ class ContourExtractor:
|
|||
|
||||
contours = []
|
||||
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
|
||||
|
||||
(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)))
|
||||
|
||||
#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
|
||||
|
||||
#cv2.imshow( "annotated", frame )
|
||||
|
||||
#cv2.waitKey(10) & 0XFF
|
||||
self.extractedContours = extractedContours
|
||||
|
||||
|
||||
def displayContours(self):
|
||||
values = self.extractedContours.values()
|
||||
|
|
|
|||
12
Exporter.py
12
Exporter.py
|
|
@ -17,21 +17,19 @@ class Exporter:
|
|||
writer.close()
|
||||
|
||||
def exportLayers(self, layers, outputPath):
|
||||
for layer in layers:
|
||||
data = layer.data
|
||||
fps = self.fps
|
||||
writer = imageio.get_writer(outputPath, fps=fps)
|
||||
for layer in layers:
|
||||
data = layer.data
|
||||
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)
|
||||
frame1[y:y+frame.shape[0], x:x+frame.shape[1]] = frame
|
||||
writer.append_data(np.array(frame1))
|
||||
cv2.imshow("changes overlayed", frame)
|
||||
cv2.waitKey(10) & 0XFF
|
||||
#cv2.imshow("changes overlayed", frame)
|
||||
#cv2.waitKey(10) & 0XFF
|
||||
|
||||
writer.close()
|
||||
cv2.destroyAllWindows()
|
||||
#cv2.destroyAllWindows()
|
||||
|
|
|
|||
6
Layer.py
6
Layer.py
|
|
@ -5,12 +5,12 @@ class Layer:
|
|||
lastFrame = 0
|
||||
backgroundImage = []
|
||||
|
||||
def __init__(self, startFrame, data, backgroundImage):
|
||||
def __init__(self, startFrame, data):
|
||||
self.startFrame = startFrame
|
||||
self.lastFrame = startFrame
|
||||
self.data.append(data)
|
||||
self.backgroundImage = backgroundImage
|
||||
|
||||
print("Layer constructed")
|
||||
#print("Layer constructed")
|
||||
|
||||
def add(self, frameNumber, data):
|
||||
self.lastFrame = frameNumber
|
||||
|
|
|
|||
|
|
@ -22,26 +22,24 @@ class LayerFactory:
|
|||
contours = data[frameNumber]
|
||||
|
||||
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():
|
||||
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 layer in layers:
|
||||
if frameNumber - layer.lastFrame <= 5:
|
||||
if len(layer.data[-1][1]) != 4:
|
||||
print("LayerFactory: Layer knew no bounds")
|
||||
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)))
|
||||
else:
|
||||
layers.append(Layer(frameNumber, contour, None))
|
||||
layerNum = len(layers)
|
||||
i+=1
|
||||
break
|
||||
|
||||
layers.append(Layer(frameNumber, (contour, (x,y,w,h))))
|
||||
|
||||
self.layers = layers
|
||||
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10
main.py
10
main.py
|
|
@ -11,17 +11,19 @@ from LayerFactory import LayerFactory
|
|||
|
||||
def demo():
|
||||
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()
|
||||
contourExtractor = ContourExtractor(footagePath)
|
||||
contourExtractor = ContourExtractor()
|
||||
contourExtractor.extractContours(footagePath)
|
||||
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"))
|
||||
|
||||
contours = contourExtractor.getextractedContours()
|
||||
|
||||
layerFactory = LayerFactory(contourExtractor.extractedContours)
|
||||
layerFactory = LayerFactory(contours)
|
||||
Exporter().exportLayers(layerFactory.layers, os.path.join(os.path.dirname(__file__), "./short.mp4"))
|
||||
|
||||
def init():
|
||||
|
|
|
|||
Loading…
Reference in New Issue