Created
October 22, 2017 10:29
-
-
Save Sulter/11e2e6f6e681e8daa1e2f8349169eec1 to your computer and use it in GitHub Desktop.
Designed for surveillance over long periods of time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
TODO: | |
* only do caclulation if cooldown is finished | |
* make directory (opencv doesn't cast exceptions on write errors) | |
* sort from biggest to smallest area, and save the image with the biggest detection area of that frame shown. | |
""" | |
import argparse | |
import datetime | |
import time | |
import cv2 | |
# construct the argument parser and parse the arguments | |
ap = argparse.ArgumentParser(description="When movement is detected, an image is save (with indication of the movement area). ") | |
ap.add_argument("-a", "--min-area", type=int, default = 200, help="minimum area size") | |
ap.add_argument("-c", "--cool-down", type=int, default = 10, help="the lowest time interval (in sec) between two images") | |
args = vars(ap.parse_args()) | |
#load camera | |
# initialize the camera and grab a reference to the raw camera capture | |
#camera = PiCamera() | |
camera = cv2.VideoCapture(0) | |
#init the background substractor | |
fgbg = cv2.BackgroundSubtractorMOG2(history = 1000, varThreshold = 100, bShadowDetection = False) | |
lastDetection = time.time() | |
# loop over the frames of the video | |
#for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): | |
while True: | |
currentTime = time.time() | |
ret, frame = camera.read() | |
#resize, grayscale and blur | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
gray = cv2.GaussianBlur(gray, (11, 11), 0) | |
#bg substract method | |
bg = fgbg.apply(gray, learningRate = 1.0/150) | |
#dialate some more | |
bg = cv2.dilate(bg, None, iterations=2) | |
(cnts, _) = cv2.findContours(bg.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
# loop over the contours | |
for c in cnts: | |
# if the contour is too small, ignore it | |
if cv2.contourArea(c) < args["min_area"]: | |
continue | |
# compute the bounding box for the contour, draw it on the frame, | |
(x, y, w, h) = cv2.boundingRect(c) | |
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) | |
# sace image if no cooldown | |
if lastDetection + args["cool_down"] < currentTime: | |
lastDetection = currentTime | |
nameString = "detected/" + time.ctime() + ".jpg" | |
cv2.imwrite(nameString, frame) | |
print("saving to: " + nameString) | |
cv2.waitKey(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment