utilized exporter module
This commit is contained in:
parent
40d6339c3c
commit
b45efb1c86
|
|
@ -15,30 +15,36 @@ class ContourExtractor:
|
||||||
|
|
||||||
#X = {frame_number: [(contour, (x,y,w,h)), ...], }
|
#X = {frame_number: [(contour, (x,y,w,h)), ...], }
|
||||||
extractedContours = dict()
|
extractedContours = dict()
|
||||||
|
min_area = 990
|
||||||
|
max_area = 30000
|
||||||
|
threashold = 25
|
||||||
|
xDim = 0
|
||||||
|
yDim = 0
|
||||||
|
|
||||||
def __init__(self, videoPath):
|
def __init__(self, videoPath):
|
||||||
print("ContourExtractor initiated")
|
print("ContourExtractor initiated")
|
||||||
|
|
||||||
|
|
||||||
min_area = 100
|
min_area = self.min_area
|
||||||
max_area = 30000
|
max_area = self.max_area
|
||||||
|
threashold = self.threashold
|
||||||
threashold = 10
|
|
||||||
|
|
||||||
# initialize the first frame in the video stream
|
# initialize the first frame in the video stream
|
||||||
vs = cv2.VideoCapture(videoPath)
|
vs = cv2.VideoCapture(videoPath)
|
||||||
|
|
||||||
res = vs.read()[0]
|
res, image = vs.read()
|
||||||
|
self.xDim = image.shape[1]
|
||||||
|
self.yDim = image.shape[0]
|
||||||
firstFrame = None
|
firstFrame = None
|
||||||
# loop over the frames of the video
|
# loop over the frames of the video
|
||||||
frameCount = 0
|
frameCount = 0
|
||||||
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")
|
||||||
return
|
return
|
||||||
|
|
||||||
frame = imutils.resize(frame, width=500)
|
frame = imutils.resize(frame, width=500)
|
||||||
cv2.imshow( "frame", frame)
|
cv2.imshow( "frame", frame)
|
||||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
@ -50,16 +56,12 @@ class ContourExtractor:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
frameDelta = cv2.absdiff(gray, firstFrame)
|
frameDelta = cv2.absdiff(gray, firstFrame)
|
||||||
|
|
||||||
thresh = cv2.threshold(frameDelta, threashold, 255, cv2.THRESH_BINARY)[1]
|
thresh = cv2.threshold(frameDelta, threashold, 255, cv2.THRESH_BINARY)[1]
|
||||||
|
|
||||||
|
|
||||||
# dilate the thresholded image to fill in holes, then find contours
|
# dilate the thresholded image to fill in holes, then find contours
|
||||||
thresh = cv2.dilate(thresh, None, iterations=3)
|
thresh = cv2.dilate(thresh, None, iterations=3)
|
||||||
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
cnts = imutils.grab_contours(cnts)
|
cnts = imutils.grab_contours(cnts)
|
||||||
|
|
||||||
# loop over the contours
|
|
||||||
contours = []
|
contours = []
|
||||||
for c in cnts:
|
for c in cnts:
|
||||||
if cv2.contourArea(c) < min_area or cv2.contourArea(c) > max_area:
|
if cv2.contourArea(c) < min_area or cv2.contourArea(c) > max_area:
|
||||||
|
|
@ -68,8 +70,6 @@ class ContourExtractor:
|
||||||
(x, y, w, h) = cv2.boundingRect(c)
|
(x, y, w, h) = cv2.boundingRect(c)
|
||||||
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)
|
||||||
text = "Occupied"
|
|
||||||
|
|
||||||
self.extractedContours[frameCount] = contours
|
self.extractedContours[frameCount] = contours
|
||||||
frameCount += 1
|
frameCount += 1
|
||||||
|
|
||||||
|
|
@ -78,27 +78,22 @@ class ContourExtractor:
|
||||||
#cv2.waitKey(10) & 0XFF
|
#cv2.waitKey(10) & 0XFF
|
||||||
|
|
||||||
def displayContours(self):
|
def displayContours(self):
|
||||||
|
|
||||||
values = self.extractedContours.values()
|
values = self.extractedContours.values()
|
||||||
frame = np.zeros(shape=[1080, 1920, 3], dtype=np.uint8)
|
|
||||||
frame = imutils.resize(frame, width=512)
|
|
||||||
frames = []
|
frames = []
|
||||||
writer = imageio.get_writer(os.path.join(os.path.dirname(__file__), "./short.mp4"), fps=30)
|
|
||||||
for xx in values:
|
for xx in values:
|
||||||
for v1 in xx:
|
for v1 in xx:
|
||||||
(x, y, w, h) = v1[1]
|
(x, y, w, h) = v1[1]
|
||||||
v = v1[0]
|
v = v1[0]
|
||||||
|
frame = np.zeros(shape=[self.yDim, self.xDim, 3], dtype=np.uint8)
|
||||||
|
frame = imutils.resize(frame, width=512)
|
||||||
#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)
|
||||||
|
|
||||||
frame[y:y+v.shape[0], x:x+v.shape[1]] = v
|
frame[y:y+v.shape[0], x:x+v.shape[1]] = v
|
||||||
frames.append(frame)
|
frames.append(frame)
|
||||||
writer.append_data(np.array(frame))
|
|
||||||
#cv2.imshow("changes overlayed", frame)
|
#cv2.imshow("changes overlayed", frame)
|
||||||
#cv2.waitKey(10) & 0XFF
|
#cv2.waitKey(10) & 0XFF
|
||||||
#cv2.waitKey(0)
|
#cv2.waitKey(0)
|
||||||
#cv2.destroyAllWindows()
|
#cv2.destroyAllWindows()
|
||||||
|
|
||||||
writer.close()
|
|
||||||
return frames
|
return frames
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,14 @@ import imageio
|
||||||
import numpy as np
|
import numpy as np
|
||||||
class Exporter:
|
class Exporter:
|
||||||
fps = 30
|
fps = 30
|
||||||
def __init__(self, data, outputPath):
|
|
||||||
|
def __init__(self):
|
||||||
print("Exporter initiated")
|
print("Exporter initiated")
|
||||||
|
|
||||||
|
def export(self, frames, outputPath):
|
||||||
fps = self.fps
|
fps = self.fps
|
||||||
writer = imageio.get_writer(outputPath, fps=fps)
|
writer = imageio.get_writer(outputPath, fps=fps)
|
||||||
for frame in data:
|
for frame in frames:
|
||||||
writer.append_data(np.array(frame))
|
writer.append_data(np.array(frame))
|
||||||
|
|
||||||
writer.close()
|
writer.close()
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
9
main.py
9
main.py
|
|
@ -4,20 +4,21 @@ from ContourExctractor import ContourExtractor
|
||||||
from Exporter import Exporter
|
from Exporter import Exporter
|
||||||
#TODO
|
#TODO
|
||||||
# finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder
|
# finden von relevanten Stellen anhand von zu findenen metriken für vergleichsbilder
|
||||||
# diff zu den ref bildern aufnehmen
|
# X diff zu den ref bildern aufnehmen
|
||||||
# zeichenn on contour inhalt
|
# zeichen von contour inhalt
|
||||||
# langes video
|
# langes video
|
||||||
|
|
||||||
def demo():
|
def demo():
|
||||||
print("startup")
|
print("startup")
|
||||||
footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/out.mp4")
|
footagePath = os.path.join(os.path.dirname(__file__), "./generate test footage/2.mp4")
|
||||||
|
|
||||||
start = time.time()
|
start = time.time()
|
||||||
contourExtractor = ContourExtractor(footagePath)
|
contourExtractor = ContourExtractor(footagePath)
|
||||||
print("Time consumed in working: ",time.time() - start)
|
print("Time consumed in working: ",time.time() - start)
|
||||||
|
|
||||||
frames = contourExtractor.displayContours()
|
frames = contourExtractor.displayContours()
|
||||||
#exporter = Exporter(frames,os.path.join(os.path.dirname(__file__), "./short.mp4"))
|
Exporter().export(frames,os.path.join(os.path.dirname(__file__), "./short.mp4"))
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
print("not needed yet")
|
print("not needed yet")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue