1
+ import cv2
2
+ file_path = input ("Enter the exact file path to the video: " )
3
+ cap = cv2 .VideoCapture (file_path )
4
+
5
+ ret , frame1 = cap .read ()
6
+ ret , frame2 = cap .read ()
7
+ while cap .isOpened ():
8
+ diff = cv2 .absdiff (frame1 , frame2 )
9
+ gray = cv2 .cvtColor (diff , cv2 .COLOR_BGR2GRAY )
10
+ blur = cv2 .GaussianBlur (gray , (5 ,5 ), 0 )
11
+ _ , thresh = cv2 .threshold (blur , 20 , 255 , cv2 .THRESH_BINARY )
12
+ dialated = cv2 .dilate (thresh , None , iterations = 3 )
13
+ contours , _ = cv2 .findContours (dialated , cv2 .RETR_TREE , cv2 .CHAIN_APPROX_SIMPLE )
14
+
15
+ for contour in contours :
16
+ (x , y , w , h ) = cv2 .boundingRect (contour )
17
+ if cv2 .contourArea (contour ) < 1000 :
18
+ continue
19
+ cv2 .rectangle (frame1 , (x ,y ), (x + w , y + h ), (0 , 255 , 0 ), 2 )
20
+ cv2 .putText (frame1 , "Status: {}" .format ('Movement' ), (10 , 20 ), cv2 .FONT_HERSHEY_SIMPLEX , 1 , (0 ,0 ,255 ), 3 )
21
+ #cv2.drawContours(frame1, contours, -1, (0,255,0), 2)
22
+
23
+ cv2 .imshow ("feed" , frame1 )
24
+ frame1 = frame2
25
+ ret , frame2 = cap .read ()
26
+ if cv2 .waitKey (40 ) == 27 :
27
+ break
28
+ cv2 .destroyAllWindows ()
29
+ cap .release ()
0 commit comments