AttributeError: module 'cv2.cv2' has no attribute 'bgsegm
Asked Answered
B

7

15
import numpy as np
import cv2
cap = cv2.VideoCapture('vtest.avi')
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
while(1):
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    cv2.imshow('frame',fgmask)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
cv2.destroyAllWindows()

I am getting the following error: AttributeError: module 'cv2.cv2' has no attribute 'bgsegm'.

I am using Python 3.6 with OpenCV 3.6 on windows machine. I tried using the pip install opencv-contrib-python command but still the problem remains same on my windows machine. This command helped be on Ubuntu system, but not on windows. I searched similar problems on stack but couldn't solve this issue. Can someone help me out on this? Thanks!

Brita answered 4/6, 2018 at 5:46 Comment(2)
Have you included contrib modules in your OpenCV build?Psychotic
@Psychotic Are you talking about pip install opencv-contrib-python ? If no, then can you please elaborate more on this?Brita
P
25

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
Pepperandsalt answered 4/6, 2018 at 7:27 Comment(8)
I have already installed it.(I have mentioned this above as well.)But still the error is popping up.Brita
Did you check pip --version to make sure it's using the correct python version?Pepperandsalt
In my understanding you have to compile cv2 contrib library (you can not use pip) in certain environments (e.g. in Raspberry Pi).Psychotic
He was mentioning that he is using it on a windows system. I've tested it on a windows system, which works completely fine.Pepperandsalt
I reinstalled opencv and it's working now. Thanks everyoneBrita
didn't solve my problem even after reinstalling the opencvCrotchet
Works now. Had to use "python -m pip install opencv-contrib-python"Sunward
I had the same issue. I solved it by pip uninstall opencv-contrib-python and pip install it.Century
H
6

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()
Holbert answered 20/5, 2020 at 11:26 Comment(1)
Switching to cv2.createBackgroundSubtractorMOG2() worked for me. ThanksNickles
C
2

If you are using python3, write:

fgbg =cv2.createBackgroundSubtractorMOG2() #For python3

And not:

fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
Crinkumcrankum answered 13/11, 2019 at 14:56 Comment(1)
cv2.createBackgroundSubtractorMOG2() doesnt seem to work for python3Satiated
M
1

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.

Monoxide answered 8/9, 2020 at 10:15 Comment(0)
L
0

I'm using 3.4.2.16 Version of OpencV, you can try this version of OpencV

Livelong answered 27/9, 2020 at 8:26 Comment(0)
I
0

Write

fgbg = cv2.createBackgroundSubtractorMOG()

instead of

fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()

you need not use bgsegm .

Isolation answered 4/10, 2022 at 4:58 Comment(0)
Q
0

I have the same issue, just replace:

fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()

With this line:

fgbg = cv2.createBackgroundSubtractorMOG2()

My opencv versions:

opencv-contrib-python            4.9.0.80
opencv-python                    4.9.0.80
Quantum answered 2/5 at 0:29 Comment(1)
Please only provide new answers, especially to old questions. The same answer was already posted 5 years ago.Minyan

© 2022 - 2024 — McMap. All rights reserved.