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 traceback
|
|
|
|
|
import _thread
|
2020-09-23 20:02:46 +00:00
|
|
|
import imageio
|
|
|
|
|
import numpy as np
|
2020-10-05 07:43:27 +00:00
|
|
|
from threading import Thread
|
|
|
|
|
from multiprocessing import Queue, Process, Pool
|
|
|
|
|
from multiprocessing.pool import ThreadPool
|
|
|
|
|
import concurrent.futures
|
2020-10-18 15:36:34 +00:00
|
|
|
from Application.VideoReader import VideoReader
|
2020-10-11 12:13:27 +00:00
|
|
|
from queue import Queue
|
|
|
|
|
import threading
|
2020-10-17 22:02:05 +00:00
|
|
|
|
2020-10-18 15:36:34 +00:00
|
|
|
from Application.Config import Config
|
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
|
|
|
|
2020-09-23 20:02:46 +00:00
|
|
|
#X = {frame_number: [(contour, (x,y,w,h)), ...], }
|
2020-10-11 12:13:27 +00:00
|
|
|
|
2020-09-22 18:25:06 +00:00
|
|
|
|
2020-09-24 20:48:04 +00:00
|
|
|
def getextractedContours(self):
|
|
|
|
|
return self.extractedContours
|
|
|
|
|
|
2020-10-11 15:09:49 +00:00
|
|
|
def __init__(self, config):
|
2020-10-11 12:13:27 +00:00
|
|
|
self.frameBuffer = Queue(16)
|
|
|
|
|
self.extractedContours = dict()
|
2020-10-11 15:09:49 +00:00
|
|
|
self.min_area = config["min_area"]
|
|
|
|
|
self.max_area = config["max_area"]
|
|
|
|
|
self.threashold = config["threashold"]
|
|
|
|
|
self.resizeWidth = config["resizeWidth"]
|
|
|
|
|
self.videoPath = config["inputPath"]
|
2020-10-11 12:13:27 +00:00
|
|
|
self.xDim = 0
|
|
|
|
|
self.yDim = 0
|
2020-10-11 15:09:49 +00:00
|
|
|
self.config = config
|
2020-10-17 22:02:05 +00:00
|
|
|
self.diff = []
|
2020-10-11 12:13:27 +00:00
|
|
|
|
2020-09-23 20:02:46 +00:00
|
|
|
print("ContourExtractor initiated")
|
|
|
|
|
|
2020-10-11 15:09:49 +00:00
|
|
|
def extractContours(self):
|
2020-10-08 20:26:29 +00:00
|
|
|
extractedContours = dict()
|
2020-10-11 15:09:49 +00:00
|
|
|
videoReader = VideoReader(self.config)
|
2020-10-08 20:26:29 +00:00
|
|
|
self.xDim = videoReader.w
|
|
|
|
|
self.yDim = videoReader.h
|
2020-10-11 15:09:49 +00:00
|
|
|
|
2020-10-08 20:26:29 +00:00
|
|
|
videoReader.fillBuffer()
|
2020-10-11 12:13:27 +00:00
|
|
|
frameCount, frame = videoReader.pop()
|
|
|
|
|
|
2020-10-17 22:02:05 +00:00
|
|
|
|
2020-10-11 12:13:27 +00:00
|
|
|
#init compare image
|
2020-10-11 15:09:49 +00:00
|
|
|
frame = imutils.resize(frame, width=self.resizeWidth)
|
2020-10-11 12:13:27 +00:00
|
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
#gray = np.asarray(gray[:,:,1]/2 + gray[:,:,2]/2).astype(np.uint8)
|
|
|
|
|
gray = cv2.GaussianBlur(gray, (5, 5), 0)
|
|
|
|
|
self.firstFrame = gray
|
2020-10-08 20:26:29 +00:00
|
|
|
|
2020-10-17 22:02:05 +00:00
|
|
|
threads = self.config["videoBufferLength"]
|
|
|
|
|
self.start = time.time()
|
2020-10-11 12:13:27 +00:00
|
|
|
with ThreadPool(threads) as pool:
|
|
|
|
|
while not videoReader.videoEnded():
|
|
|
|
|
#FrameCount, frame = videoReader.pop()
|
2020-10-17 22:02:05 +00:00
|
|
|
|
2020-10-04 12:51:16 +00:00
|
|
|
|
2020-10-11 12:13:27 +00:00
|
|
|
if videoReader.buffer.qsize() == 0:
|
2020-10-11 15:09:49 +00:00
|
|
|
time.sleep(.5)
|
2020-10-05 07:43:27 +00:00
|
|
|
|
2020-10-11 12:13:27 +00:00
|
|
|
tmpData = [videoReader.pop() for i in range(0, videoReader.buffer.qsize())]
|
|
|
|
|
pool.map(self.getContours, tmpData)
|
2020-10-17 22:02:05 +00:00
|
|
|
#for data in tmpData:
|
|
|
|
|
#self.getContours(data)
|
2020-09-22 18:25:06 +00:00
|
|
|
|
2020-10-17 22:02:05 +00:00
|
|
|
frameCount = tmpData[-1][0]
|
2020-10-08 20:26:29 +00:00
|
|
|
videoReader.thread.join()
|
2020-10-11 12:13:27 +00:00
|
|
|
return self.extractedContours
|
2020-09-28 20:28:23 +00:00
|
|
|
|
2020-10-11 12:13:27 +00:00
|
|
|
def getContours(self, data):
|
|
|
|
|
frameCount, frame = data
|
|
|
|
|
firstFrame = self.firstFrame
|
2020-10-17 22:02:05 +00:00
|
|
|
if frameCount % (60*30) == 0:
|
|
|
|
|
print(f"{frameCount/(60*30)} Minutes processed in {round((time.time() - self.start), 2)} each")
|
|
|
|
|
self.start = time.time()
|
2020-10-11 12:13:27 +00:00
|
|
|
frame = imutils.resize(frame, width=self.resizeWidth)
|
|
|
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
2020-10-05 07:43:27 +00:00
|
|
|
gray = cv2.GaussianBlur(gray, (5, 5), 0)
|
|
|
|
|
frameDelta = cv2.absdiff(gray, firstFrame)
|
|
|
|
|
thresh = cv2.threshold(frameDelta, self.threashold, 255, cv2.THRESH_BINARY)[1]
|
|
|
|
|
# dilate the thresholded image to fill in holes, then find contours
|
2020-10-17 22:02:05 +00:00
|
|
|
thresh = cv2.dilate(thresh, None, iterations=10)
|
|
|
|
|
#cv2.imshow("changes x", thresh)
|
|
|
|
|
#cv2.waitKey(10) & 0XFF
|
2020-10-05 07:43:27 +00:00
|
|
|
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
2020-10-17 22:02:05 +00:00
|
|
|
self.diff.append(np.count_nonzero(thresh))
|
2020-10-05 07:43:27 +00:00
|
|
|
cnts = imutils.grab_contours(cnts)
|
|
|
|
|
|
|
|
|
|
contours = []
|
|
|
|
|
for c in cnts:
|
|
|
|
|
ca = cv2.contourArea(c)
|
|
|
|
|
if ca < self.min_area or ca > self.max_area:
|
|
|
|
|
continue
|
|
|
|
|
(x, y, w, h) = cv2.boundingRect(c)
|
|
|
|
|
|
2020-10-11 12:13:27 +00:00
|
|
|
contours.append((x, y, w, h))
|
|
|
|
|
|
2020-10-08 20:26:29 +00:00
|
|
|
if len(contours) != 0 and contours is not None:
|
2020-10-11 12:13:27 +00:00
|
|
|
# this should be thread safe
|
|
|
|
|
self.extractedContours[frameCount] = contours
|
|
|
|
|
|
2020-09-22 18:25:06 +00:00
|
|
|
def displayContours(self):
|
|
|
|
|
values = self.extractedContours.values()
|
2020-09-23 20:02:46 +00:00
|
|
|
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
|
2020-10-17 22:02:05 +00:00
|
|
|
|
2020-09-24 15:14:59 +00:00
|
|
|
cv2.destroyAllWindows()
|
2020-09-23 20:02:46 +00:00
|
|
|
|
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)
|
2020-09-23 20:02:46 +00:00
|
|
|
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
|
|
|
|