Created
May 14, 2018 17:04
-
-
Save TonsOfFun/da7ecee963005d9e31207d0f903e4db3 to your computer and use it in GitHub Desktop.
Crypto Smart Camera: Basic Motion
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
import imutils | |
def motion(current_frame=None, previous_frame=None, avg=None, min_area=2000): | |
motion = False | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
gray = cv2.GaussianBlur(gray, (21, 21), 0) | |
# if the average frame is None, initialize it | |
if avg is None: | |
log.info("Initialising average frame") | |
avg = gray.copy().astype("float") | |
raw_capture.truncate(0) | |
continue | |
# accumulate the weighted average between the current frame and | |
# previous frames, then compute the difference between the current | |
# frame and running average | |
cv2.accumulateWeighted(gray, avg, 0.5) | |
frame_delta = cv2.absdiff(gray, cv2.convertScaleAbs(avg)) | |
# threshold the delta image, dilate the thresholded image to fill | |
# in holes, then find contours on thresholded image | |
thresh = cv2.threshold(frame_delta, args.delta_threshold, 255, cv2.THRESH_BINARY)[1] | |
thresh = cv2.dilate(thresh, None, iterations=2) | |
(contours, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
for c in contours: | |
# if the contour is too small, ignore it | |
if cv2.contourArea(c) < min_area: | |
continue | |
motion = True | |
log.info("Motion detected") | |
return motion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by this post on pyimagesearch.com