Created
February 21, 2017 03:50
-
-
Save DustinAlandzes/4f68a3a517f169a27184f7973d18f610 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env python | |
| """ | |
| Found on | |
| http://www.steinm.com/blog/motion-detection-webcam-python-opencv-differential-images/ | |
| """ | |
| import cv2 | |
| def diffImg(t0, t1, t2): | |
| d1 = cv2.absdiff(t2, t1) | |
| d2 = cv2.absdiff(t1, t0) | |
| return cv2.bitwise_and(d1, d2) | |
| cam = cv2.VideoCapture(0) | |
| winName = "Movement Indicator" | |
| cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE) | |
| # Read three images first: | |
| t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
| t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
| t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
| while True: | |
| cv2.imshow( winName, diffImg(t_minus, t, t_plus) ) | |
| # Read next image | |
| t_minus = t | |
| t = t_plus | |
| t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
| key = cv2.waitKey(10) | |
| if key == 27: | |
| cv2.destroyWindow(winName) | |
| break | |
| print("Goodbye") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment