import cv2 import numpy as np #cap = cv2.VideoCapture(1) cap = cv2.VideoCapture('OPENCVmotiondetection.avi') object_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=10) while True: ret, frame = cap.read() roi = frame[0:480 , 320:640] # def Region Off Interest mask = object_detector.apply(roi) # definission d'un masque avec la ROI _, mask = cv2.threshold(mask, 254, 255, 0) # ne garde que les pixel blancs _, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: area = cv2.contourArea(cnt) if area > 100 : #cv2.drawContours(roi, [cnt], -1, (0, 255, 0), 2) #dessin contour x, y, w, h =cv2.boundingRect(cnt) # definition rectangle englobant cv2.rectangle(roi, (x, y), (x+w, y+h), (0, 255, 0), 3) #dessin rectangle cv2.imshow("Frame", frame) cv2.imshow("ROI", roi) cv2.imshow("Mask", mask) if cv2.waitKey(40) == 27: break cv2.destroyAllWindows() cap.release()