Video-Summary/ContourExctractor.py

113 lines
3.6 KiB
Python
Raw Normal View History

2020-09-22 18:25:06 +00:00
from imutils.video import VideoStream
import argparse
import datetime
import imutils
import time
import cv2
import os
import numpy as np
import traceback
import _thread
import imageio
import numpy as np
2020-09-22 18:25:06 +00:00
2020-09-20 20:01:54 +00:00
class ContourExtractor:
2020-09-22 18:25:06 +00:00
#X = {frame_number: [(contour, (x,y,w,h)), ...], }
2020-09-22 18:25:06 +00:00
extractedContours = dict()
2020-09-28 20:28:23 +00:00
min_area = 200
2020-09-24 13:47:49 +00:00
max_area = 30000
2020-09-29 20:52:36 +00:00
threashold = 35
2020-09-24 13:47:49 +00:00
xDim = 0
yDim = 0
2020-09-22 18:25:06 +00:00
2020-09-24 20:48:04 +00:00
def getextractedContours(self):
return self.extractedContours
2020-09-28 20:28:23 +00:00
def __init__(self):
print("ContourExtractor initiated")
2020-09-28 20:28:23 +00:00
def extractContours(self, videoPath):
2020-09-24 13:47:49 +00:00
min_area = self.min_area
max_area = self.max_area
threashold = self.threashold
2020-09-22 18:25:06 +00:00
# initialize the first frame in the video stream
vs = cv2.VideoCapture(videoPath)
2020-09-24 13:47:49 +00:00
res, image = vs.read()
self.xDim = image.shape[1]
self.yDim = image.shape[0]
2020-09-22 18:25:06 +00:00
firstFrame = None
# loop over the frames of the video
frameCount = 0
2020-09-28 20:28:23 +00:00
extractedContours = dict()
2020-09-22 18:25:06 +00:00
while res:
res, frame = vs.read()
# resize the frame, convert it to grayscale, and blur it
if frame is None:
2020-09-24 13:47:49 +00:00
print("ContourExtractor: frame was None")
2020-09-28 20:28:23 +00:00
break
2020-09-24 13:47:49 +00:00
frame = imutils.resize(frame, width=500)
2020-09-22 18:25:06 +00:00
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
2020-09-22 18:25:06 +00:00
# if the first frame is None, initialize it
if firstFrame is None:
firstFrame = gray
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)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
contours = []
2020-09-22 18:25:06 +00:00
for c in cnts:
2020-09-28 20:28:23 +00:00
ca = cv2.contourArea(c)
if ca < min_area or ca > max_area:
2020-09-22 18:25:06 +00:00
continue
(x, y, w, h) = cv2.boundingRect(c)
2020-09-28 20:28:23 +00:00
#print((x, y, w, h))
contours.append((frame[y:y+h, x:x+w], (x, y, w, h)))
2020-09-28 20:28:23 +00:00
#cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
2020-09-28 20:28:23 +00:00
if len(contours) != 0:
extractedContours[frameCount] = contours
2020-09-22 18:25:06 +00:00
frameCount += 1
#cv2.imshow( "annotated", frame )
#cv2.waitKey(10) & 0XFF
2020-09-28 20:28:23 +00:00
self.extractedContours = extractedContours
2020-09-22 18:25:06 +00:00
def displayContours(self):
values = self.extractedContours.values()
for xx in values:
for v1 in xx:
(x, y, w, h) = v1[1]
v = v1[0]
2020-09-24 13:47:49 +00:00
frame = np.zeros(shape=[self.yDim, self.xDim, 3], dtype=np.uint8)
frame = imutils.resize(frame, width=512)
2020-09-24 15:14:59 +00:00
frame[y:y+v.shape[0], x:x+v.shape[1]] = v
cv2.imshow("changes overlayed", frame)
cv2.waitKey(10) & 0XFF
cv2.destroyAllWindows()
2020-09-24 15:14:59 +00:00
def exportContours(self):
values = self.extractedContours.values()
frames = []
for xx in values:
for v1 in xx:
(x, y, w, h) = v1[1]
v = v1[0]
frame = np.zeros(shape=[self.yDim, self.xDim, 3], dtype=np.uint8)
frame = imutils.resize(frame, width=512)
frame[y:y+v.shape[0], x:x+v.shape[1]] = v
frames.append(frame)
return frames
2020-09-22 18:25:06 +00:00