detection_de_mouvement_avec_opencv_en_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
detection_de_mouvement_avec_opencv_en_python [2019/02/13 17:27] – serge | detection_de_mouvement_avec_opencv_en_python [2019/05/25 09:45] (Version actuelle) – [Un script simple mais pas efficace] serge | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Détection de mouvement avec opencv en python ====== | ====== Détection de mouvement avec opencv en python ====== | ||
- | =====Un | + | =====Ressources à étudier===== |
- | <code python> | + | * https:// |
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | =====Dépendances===== | ||
+ | Pour installer cv2.bgsegm et cv2.createBackgroundSubtractorGMG | ||
+ | sudo pip3 install opencv-contrib-python | ||
+ | =====Un script | ||
+ | Sans imutils ! et valable pour OpenCV 3.4.5 | ||
+ | <file python | ||
# | # | ||
# -*- coding: UTF-8 -*- | # -*- coding: UTF-8 -*- | ||
Ligne 9: | Ligne 18: | ||
print(" | print(" | ||
- | firstFrame = None | + | def motion_detector(cam, |
- | cap = cv2.VideoCapture(-1) | + | # Création des fenêtres |
- | + | cv2.namedWindow(' | |
- | while True: | + | cv2.namedWindow(' |
+ | cv2.namedWindow(' | ||
+ | |||
+ | | ||
+ | cap = cv2.VideoCapture(cam) | ||
+ | loop = 1 | ||
+ | while loop: | ||
rval, frame = cap.read() | rval, frame = cap.read() | ||
+ | # Si la webcam à une image | ||
if rval: | if rval: | ||
+ | # Conversion en gris | ||
gray = cv2.cvtColor(frame, | gray = cv2.cvtColor(frame, | ||
- | gray = cv2.GaussianBlur(gray, | + | |
+ | | ||
+ | # Enregistrement d'une 1ère frame | ||
if firstFrame is None: | if firstFrame is None: | ||
firstFrame = gray | firstFrame = gray | ||
else: | else: | ||
- | | + | |
- | thresh = cv2.threshold( | + | delta = cv2.absdiff(firstFrame, |
+ | # Seuil | ||
+ | thresh = cv2.threshold(delta, seuil_0, seuil_1, cv2.THRESH_BINARY)[1] | ||
+ | # Dilatation des zones | ||
thresh = cv2.dilate(thresh, | thresh = cv2.dilate(thresh, | ||
+ | # Contours des zones | ||
cnts, contours, hierarchy = cv2.findContours(thresh, | cnts, contours, hierarchy = cv2.findContours(thresh, | ||
| | ||
+ | # Affichage des contours | ||
for c in contours: | for c in contours: | ||
- | if cv2.contourArea(c) > 5000: | + | if cv2.contourArea(c) > area: |
+ | # Un rectangle incluant la zone | ||
(x, y, w, h) = cv2.boundingRect(c) | (x, y, w, h) = cv2.boundingRect(c) | ||
cv2.rectangle(frame, | cv2.rectangle(frame, | ||
- | cv2.imshow(" | + | cv2.imshow(" |
cv2.imshow(" | cv2.imshow(" | ||
- | cv2.imshow(" | + | cv2.imshow(" |
- | if cv2.waitKey(30) & 0xFF == 27: | + | |
+ | | ||
loop = 0 | loop = 0 | ||
- | cv2.destroyAllWindows() | + | |
- | </code> | + | |
+ | if __name__ == " | ||
+ | # Numero de webcam | ||
+ | CAM = 0 | ||
+ | |||
+ | # Valeur de flou, impair | ||
+ | FLOU = 41 | ||
+ | |||
+ | # Seuils sur le gris | ||
+ | SEUIL_0, SEUIL_1 = 60, 255 | ||
+ | |||
+ | # Aire minimal avec différence de pixels | ||
+ | AREA = 5000 | ||
+ | |||
+ | # Attente en ms entre 2 capture | ||
+ | TEMPO = 30 | ||
+ | |||
+ | motion_detector(CAM, | ||
+ | </file> | ||
{{tag> | {{tag> |
detection_de_mouvement_avec_opencv_en_python.1550078844.txt.gz · Dernière modification : 2019/02/13 17:27 de serge