├── MovementDetection.py └── README.md /MovementDetection.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import winsound 3 | import time 4 | camera = cv2.VideoCapture(0) 5 | output = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc(*'MJPG'),20,(500,500)) 6 | while camera.isOpened(): 7 | text = time.ctime() 8 | itr, frame1= camera.read() 9 | itr, frame2= camera.read() 10 | cv2.putText(frame1,text, (10,15),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,255,56)) 11 | difference = cv2.absdiff(frame1,frame2) 12 | grey = cv2.cvtColor(difference,cv2.COLOR_RGB2GRAY) 13 | blur = cv2.GaussianBlur(grey,(5,5),0) 14 | _,thresh = cv2.threshold(blur,30,255,cv2.THRESH_BINARY) 15 | dilate = cv2.dilate(thresh,None,iterations= 4) 16 | controus, _ = cv2.findContours(dilate,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 17 | for n in controus: 18 | if cv2.contourArea(n)<6000: 19 | continue 20 | x,y,w,h = cv2.boundingRect(n) 21 | winsound.Beep(1000,400) 22 | cv2.rectangle(frame1,(x,y),(x+w,y+h),(255,0,0),1,4) 23 | if cv2.waitKey(10)== ord('n'): 24 | break 25 | 26 | cv2.imshow('Detection',frame1) 27 | output.write(frame1) 28 | camera.release() 29 | output.release() 30 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MovementDetection 2 | Movement Detection using OpenCV 3 | --------------------------------------------------------------------------------