/
Alx89
/
OpenCV
Обзор
Документация
Войти
/
Alx89
/
OpenCV
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
4.x
samples/python/tutorial_code/video/background_subtraction/bg_sub.py
51 строка
2 KB
Alexander Reynolds
hotfix: call isOpened() in python bg sub tutorial
12 янв 2021, 01:04
12 янв 2021, 01:04
e3856df
Код
Авторство
О чём код?
from __future__ import print_function import cv2 as cv import argparse parser = argparse.ArgumentParser(description='This program shows how to use background subtraction methods provided by \ OpenCV. You can process both videos and images.') parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.', default='vtest.avi') parser.add_argument('--algo', type=str, help='Background subtraction method (KNN, MOG2).', default='MOG2') args = parser.parse_args() ## [create] #create Background Subtractor objects if args.algo == 'MOG2': backSub = cv.createBackgroundSubtractorMOG2() else: backSub = cv.createBackgroundSubtractorKNN() ## [create] ## [capture] capture = cv.VideoCapture(cv.samples.findFileOrKeep(args.input)) if not capture.isOpened(): print('Unable to open: ' + args.input) exit(0) ## [capture] while True: ret, frame = capture.read() if frame is None: break ## [apply] #update the background model fgMask = backSub.apply(frame) ## [apply] ## [display_frame_number] #get the frame number and write it on the current frame cv.rectangle(frame, (10, 2), (100,20), (255,255,255), -1) cv.putText(frame, str(capture.get(cv.CAP_PROP_POS_FRAMES)), (15, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0)) ## [display_frame_number] ## [show] #show the current frame and the fg masks cv.imshow('Frame', frame) cv.imshow('FG Mask', fgMask) ## [show] keyboard = cv.waitKey(30) if keyboard == 'q' or keyboard == 27: break