motion_detection.py 3.17 KB
Newer Older
1 2 3 4 5
'''
pour lancer un programme dans le server X (interface graphique)
$ export DISPLAY=:0
source: https://superuser.com/questions/1035787/open-a-movie-with-vlc-from-command-line-via-ssh
'''
6
# sudo -H python3 -m pip install opencv-python
7
# sudo apt install python3-opencv
8 9 10 11 12 13 14 15 16
# https://www.geeksforgeeks.org/webcam-motion-detector-python/
# Pyhton program to implement 
# WebCam Motion Detector 

# importing OpenCV, time and Pandas library 
import cv2, time 
# importing datetime class from datetime library 
from datetime import datetime 

17 18 19 20
BLUR = 21
THRESHOLD = 90
MIN_AREA = 4000
REFRESH_BACK_RATE = -1
21
SHOW_IMAGES = True
22 23
UNPACK_COUNT = 2 # for rapsberry, 2 for regular linux distro
ANALYSIS_RESOLUTION = (320,240)
24

25 26 27 28
# Assigning our static_back to None 
static_back = None

# Capturing video 
29 30 31
video = cv2.VideoCapture(0)

frame_count = 0
32 33 34 35 36

# Infinite while loop to treat stack of image as video 
while True: 
	# Reading frame(image) from video 
	check, frame = video.read() 
37
	
38 39
	low_res = cv2.resize(frame, ANALYSIS_RESOLUTION)
	
40 41
	frame_count += 1
	
42
	# Converting color image to gray_scale image 
43
	gray = cv2.cvtColor(low_res, cv2.COLOR_BGR2GRAY) 
44 45 46

	# Converting gray scale image to GaussianBlur 
	# so that change can be find easily 
47
	gray = cv2.GaussianBlur(gray, (BLUR, BLUR), 0) 
48 49 50

	# In first iteration we assign the value 
	# of static_back to our first frame 
51 52
	if static_back is None or ( REFRESH_BACK_RATE != -1 and frame_count % REFRESH_BACK_RATE == 0): 
		static_back = gray
53 54 55 56 57 58 59 60
		continue

	# Difference between static background 
	# and current frame(which is GaussianBlur) 
	diff_frame = cv2.absdiff(static_back, gray) 

	# If change in between static background and 
	# current frame is greater than 30 it will show white color(255) 
61
	thresh_frame = cv2.threshold(diff_frame, THRESHOLD, 255, cv2.THRESH_BINARY)[1] 
62 63
	thresh_frame = cv2.dilate(thresh_frame, None, iterations = 2) 

64 65 66 67 68 69
	# Finding contour of moving object
	cnts = None
	if UNPACK_COUNT == 3:
		(_, cnts, _) = cv2.findContours( thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE )
	elif UNPACK_COUNT == 2:
		(cnts, _) = cv2.findContours( thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE )
70
	
71
	contour_num = 0
72
	for contour in cnts: 
73
		if cv2.contourArea(contour) < MIN_AREA: 
74 75 76
			continue
		(x, y, w, h) = cv2.boundingRect(contour) 
		# making green rectangle arround the moving object 
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		cv2.rectangle(low_res, (x, y), (x + w, y + h), (0, 255, 0), 3) 
		contour_num += 1
	print( contour_num )

	if SHOW_IMAGES:
		# Displaying image in gray_scale 
		cv2.imshow("Gray Frame", gray) 
		# Displaying the difference in currentframe to 
		# the staticframe(very first_frame) 
		cv2.imshow("Difference Frame", diff_frame) 
		# Displaying the black and white image in which if 
		# intencity difference greater than 30 it will appear white 
		cv2.imshow("Threshold Frame", thresh_frame) 
		# Displaying color frame with contour of motion of object 
		cv2.imshow("Color Frame", low_res)
	
93 94 95 96 97 98 99 100 101 102 103 104
	key = cv2.waitKey(1) 
	# if q entered whole process will stop 
	if key == ord('q'): 
		# if something is movingthen it append the end time of movement 
		if motion == 1: 
			time.append(datetime.now()) 
		break

video.release()

# Destroying all the windows 
cv2.destroyAllWindows()