detection_de_mouvement_avec_opencv_en_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédenteDernière révisionLes deux révisions suivantes | ||
detection_de_mouvement_avec_opencv_en_python [2019/02/13 17:20] – créée serge | detection_de_mouvement_avec_opencv_en_python [2019/03/04 12:48] – [Ressources à étudier] 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: | ||
- | # resize the frame, convert it to grayscale, and blur it | + | # Conversion en gris |
gray = cv2.cvtColor(frame, | gray = cv2.cvtColor(frame, | ||
- | | + | |
- | gray = cv2.GaussianBlur(gray, | + | gray = cv2.GaussianBlur(gray, |
- | # if the first frame is None, initialize it | + | # 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] | |
- | # dilate the thresholded image to fill in holes, | + | # 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, |
- | | + | |
- | cv2.CHAIN_APPROX_SIMPLE) | + | # Affichage des contours |
- | + | ||
- | # loop over the contours | + | |
for c in contours: | for c in contours: | ||
- | | + | 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() | ||
+ | |||
+ | if __name__ == " | ||
+ | # Numero de webcam | ||
+ | CAM = 0 | ||
+ | | ||
+ | # Valeur de flou, impair | ||
+ | FLOU = 41 | ||
- | cv2.destroyAllWindows() | + | # Seuils sur le gris |
+ | SEUIL_0, SEUIL_1 = 60, 255 | ||
- | </code> | + | # 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.txt · Dernière modification : 2019/05/25 09:45 de serge