import numpy as np
import cv2
cap = cv2.VideoCapture(\'vtest.avi\')
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
while(1):
ret, frame = cap.read()
fgmask =
Most probably this issue arises when someone tries to install multiple packages of OpenCV or even tries to install multiple packages of OpenCV in a different order.
If you read the official opencv-contrib-python
installation docs there is written that you only need to install one package of OpenCV.
For example, if you need a base OpenCV module with extra contrib modules, you just need to install opencv-contrib-python
with the appropriate version you want:
$ pip install opencv-contrib-python
And this will install both OpenCV and OpenCV contrib modules.
The reason for that is all the OpenCV modules use the same namespace which is cv2
and do not use the plugin architecture. So, when you install the other module, it will rewrite the existing namespace with a new module library that is being installed.
Installing contrib dependencies will fix your problem:
pip install opencv-contrib-python
Alternatively, you can use the newer createBackgroundSubtractorMOG2()
, which is directly available in cv2:
fgbg = cv2.createBackgroundSubtractorMOG2()
I'm using 3.4.2.16 Version of OpencV, you can try this version of OpencV
You need to install the contrib
dependencies to get this working as it isn't part of the standard build.
pip install opencv-contrib-python
If you are using python3, write:
fgbg =cv2.createBackgroundSubtractorMOG2() #For python3
And not:
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()