/
sxfour
/
python_all_projects
Обзор
Документация
Войти
/
sxfour
/
python_all_projects
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
python_opencv_highway/main.py
35 строк
1016 B
Egor Levashov
Add files via upload
27 апр 2024, 10:25
Не верифицирован
27 апр 2024, 10:25
b4dd71e
Код
Авторство
О чём код?
import cv2 cap = cv2.VideoCapture('highway.mp4') mog = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=40) if not cap.isOpened(): print("Error opening video file") while cap.isOpened(): ret, frame = cap.read() height, weight, _ = frame.shape roi = frame[300:1000, 400:800] mask = mog.apply(roi) _, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: area = cv2.contourArea(cnt) if area > 20: # cv2.drawContours(roi, [cnt], -1, (0, 255, 0), 2) x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 1) if ret: cv2.imshow('Frame', frame) cv2.imshow('Mask', mask) cv2.imshow('Roi', roi) if cv2.waitKey(25) & 0xFF == ord('q'): break else: break cap.release() cv2.destroyAllWindows()