Outils pour utilisateurs

Outils du site


detection_de_mouvement_avec_opencv_en_python

Ceci est une ancienne révision du document !


Détection de mouvement avec opencv en python

Un exemple de script valable pour OpenCV 3.4.5

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
 
import cv2
print("OpenCV version :  {0}".format(cv2.__version__))
 
firstFrame = None
cap = cv2.VideoCapture(-1)
 
while True:
        rval, frame = cap.read()
        if rval:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            gray = cv2.GaussianBlur(gray, (21, 21), 0)
 
            if firstFrame is None:
                firstFrame = gray
            else:
                frameDelta = cv2.absdiff(firstFrame, gray)
                thresh = cv2.threshold( frameDelta, 50, 255, cv2.THRESH_BINARY)[1]
                thresh = cv2.dilate(thresh, None, iterations=2)
                cnts, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                                             cv2.CHAIN_APPROX_SIMPLE)
                for c in contours:
                    if cv2.contourArea(c) > 5000:
                        (x, y, w, h) = cv2.boundingRect(c)
                        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
 
                        cv2.imshow("Security Feed", frame)
                        cv2.imshow("Thresh", thresh)
                        cv2.imshow("Frame Delta", frameDelta)
 
                        if cv2.waitKey(30) & 0xFF == 27:
                            loop = 0
cv2.destroyAllWindows()
detection_de_mouvement_avec_opencv_en_python.1550078844.txt.gz · Dernière modification : 2019/02/13 17:27 de serge