Outils pour utilisateurs

Outils du site


detection_de_mouvement_avec_opencv_en_python

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
detection_de_mouvement_avec_opencv_en_python [2019/02/13 17:27] sergedetection_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 exemple de script valable pour OpenCV 3.4.5===== +=====Ressources à étudier===== 
-<code python>+  * https://github.com/RobinDavid/Motion-detection-OpenCV 
 +  * https://stackoverflow.com/questions/3374828/how-do-i-track-motion-using-opencv-in-python 
 +  * https://stackoverflow.com/questions/40514508/opencv-detect-movement-in-python 
 + 
 +=====Dépendances===== 
 +Pour installer cv2.bgsegm et cv2.createBackgroundSubtractorGMG 
 +  sudo pip3 install opencv-contrib-python 
 +=====Un script simple mais pas efficace===== 
 +Sans imutils ! et valable pour OpenCV 3.4.5 
 +<file python motion_detection.py>
 #!/usr/bin/env python3 #!/usr/bin/env python3
 # -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
Ligne 9: Ligne 18:
 print("OpenCV version :  {0}".format(cv2.__version__)) print("OpenCV version :  {0}".format(cv2.__version__))
  
-firstFrame = None +def motion_detector(cam, flou, seuil_0, seuil_1, area, tempo): 
-cap = cv2.VideoCapture(-1+    # Création des fenêtres 
- +    cv2.namedWindow('Ecart entre frame'
-while True:+    cv2.namedWindow('Thresh'
 +    cv2.namedWindow('Security Feed'
 +     
 +    firstFrame = None 
 +    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, cv2.COLOR_BGR2GRAY)             gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
-            gray = cv2.GaussianBlur(gray, (2121), 0)+            # Application d'un flou 
 +            gray = cv2.GaussianBlur(gray, (flouflou), 0)
  
 +            # Enregistrement d'une 1ère frame
             if firstFrame is None:             if firstFrame is None:
                 firstFrame = gray                 firstFrame = gray
             else:             else:
-                frameDelta = cv2.absdiff(firstFrame, gray) +                # Ecart entre les frames 
-                thresh = cv2.threshold( frameDelta50255, cv2.THRESH_BINARY)[1]+                delta = cv2.absdiff(firstFrame, gray) 
 +                # Seuil 
 +                thresh = cv2.threshold(deltaseuil_0seuil_1, cv2.THRESH_BINARY)[1] 
 +                # Dilatation des zones
                 thresh = cv2.dilate(thresh, None, iterations=2)                 thresh = cv2.dilate(thresh, None, iterations=2)
 +                # Contours des zones
                 cnts, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,                 cnts, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                                              cv2.CHAIN_APPROX_SIMPLE)                                                              cv2.CHAIN_APPROX_SIMPLE)
 +                # 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, (x, y), (x + w, y + h), (0, 255, 0), 2)                         cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  
-                        cv2.imshow("Security Feed", frame)+                        cv2.imshow("Ecart entre frame", delta)
                         cv2.imshow("Thresh", thresh)                         cv2.imshow("Thresh", thresh)
-                        cv2.imshow("Frame Delta", frameDelta)+                        cv2.imshow("Security Feed", frame)
  
-                        if cv2.waitKey(30) & 0xFF == 27:+                        # Echap pour quitter dans une fenêtre 
 +                        if cv2.waitKey(tempo) & 0xFF == 27:
                             loop = 0                             loop = 0
-cv2.destroyAllWindows() +    cv2.destroyAllWindows(
-</code>+ 
 +if __name__ == "__main__": 
 +    # 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, FLOU, SEUIL_0, SEUIL_1, AREA, TEMPO
 +</file>
  
 {{tag>opencv sb python}} {{tag>opencv sb python}}
detection_de_mouvement_avec_opencv_en_python.1550078844.txt.gz · Dernière modification : 2019/02/13 17:27 de serge