-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectionDeMouvement.py
54 lines (51 loc) · 1.91 KB
/
detectionDeMouvement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import sys
import time
import numpy as np
from cv2 import cv2
cap=cv2.VideoCapture("http://192.168.43.1:4747/video")#Adresse IP de la camera, pour tester, installer une applis qui transforme le tel en camera IP droidCAM pour android
kernel_blur=5
seuil=15
surface=1000
ret, originale=cap.read()
originale=cv2.cvtColor(originale, cv2.COLOR_BGR2GRAY)
originale=cv2.GaussianBlur(originale, (kernel_blur, kernel_blur), 0)
kernel_dilate=np.ones((5, 5), np.uint8)
while True:
ret, frame=cap.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray, (kernel_blur, kernel_blur), 0)
mask=cv2.absdiff(originale, gray)
mask=cv2.threshold(mask, seuil, 255, cv2.THRESH_BINARY)[1]
mask=cv2.dilate(mask, kernel_dilate, iterations=3)
contours, nada=cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
frame_contour=frame.copy()
for c in contours:
cv2.drawContours(frame_contour, [c], 0, (0, 255, 0), 5)
if cv2.contourArea(c)<surface:
continue
x, y, w, h=cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
originale=gray
cv2.putText(frame, "[o|l]seuil: {:d} [p|m]blur: {:d} [i|k]surface: {:d}".format(seuil, kernel_blur, surface), (10, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 255), 2)
cv2.imshow("frame", frame)
cv2.imshow("contour", frame_contour)
cv2.imshow("mask", mask)
intrus=0
key=cv2.waitKey(30)&0xFF
if key==ord('q'):
break
if key==ord('p'):
kernel_blur=min(43, kernel_blur+2)
if key==ord('m'):
kernel_blur=max(1, kernel_blur-2)
if key==ord('i'):
surface+=1000
if key==ord('k'):
surface=max(1000, surface-1000)
if key==ord('o'):
seuil=min(255, seuil+1)
if key==ord('l'):
seuil=max(1, seuil-1)
cap.release()
cv2.destroyAllWindows()