简体   繁体   English

AttributeError: 模块 'cv2.cv2' 没有属性 'bgsegm

[英]AttributeError: module 'cv2.cv2' has no attribute 'bgsegm

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'.我收到以下错误:AttributeError:模块“cv2.cv2”没有属性“bgsegm”。

I am using Python 3.6 with OpenCV 3.6 on windows machine.我在 windows 机器上使用 Python 3.6 和 OpenCV 3.6。 I tried using the pip install opencv-contrib-python command but still the problem remains same on my windows machine.我尝试使用 pip install opencv-contrib-python 命令,但问题在我的 windows 机器上仍然存在。 This command helped be on Ubuntu system, but not on windows. I searched similar problems on stack but couldn't solve this issue.这个命令在Ubuntu系统上有帮助,但在windows上没有。我在堆栈上搜索了类似的问题,但无法解决这个问题。 Can someone help me out on this?有人可以帮我解决这个问题吗? Thanks!谢谢!

您需要安装contrib依赖项才能使其正常工作,因为它不是标准构建的一部分。

pip install opencv-contrib-python

Installing contrib dependencies will fix your problem:安装 contrib 依赖项将解决您的问题:

pip install opencv-contrib-python

Alternatively, you can use the newer createBackgroundSubtractorMOG2() , which is directly available in cv2:或者,您可以使用较新的createBackgroundSubtractorMOG2() ,它在 cv2 中直接可用:

fgbg = cv2.createBackgroundSubtractorMOG2()

If you are using python3, write:如果您使用的是 python3,请写:

fgbg =cv2.createBackgroundSubtractorMOG2() #For python3

And not:并不是:

fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()

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.当有人尝试安装多个 OpenCV 包甚至尝试以不同顺序安装多个 OpenCV 包时,很可能会出现此问题。

If you read the official opencv-contrib-python installation docs there is written that you only need to install one package of OpenCV.如果您阅读了官方的opencv-contrib-python安装文档,其中写道您只需要安装一个 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:例如,如果你需要一个带有额外 contrib 模块的基础 OpenCV 模块,你只需要安装你想要的合适版本的opencv-contrib-python

$ pip install opencv-contrib-python

And this will install both OpenCV and OpenCV contrib modules.这将安装 OpenCV 和 OpenCV contrib 模块。

The reason for that is all the OpenCV modules use the same namespace which is cv2 and do not use the plugin architecture.原因是所有 OpenCV 模块都使用相同的命名空间,即cv2并且不使用插件架构。 So, when you install the other module, it will rewrite the existing namespace with a new module library that is being installed.因此,当您安装另一个模块时,它将使用正在安装的新模块库重写现有命名空间。

我用的是 3.4.2.16 版本的 OpencV,你可以试试这个版本的 OpencV

Write

fgbg = cv2.createBackgroundSubtractorMOG()

instead of代替

fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()

you need not use bgsegm.你不需要使用 bgsegm。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:模块“cv2.cv2”没有属性“cv” - AttributeError: module 'cv2.cv2' has no attribute 'cv' AttributeError: 模块 'cv2.cv2' 没有属性 'DataFrame' - AttributeError: module 'cv2.cv2' has no attribute 'DataFrame' AttributeError:模块“ cv2.cv2”没有属性面 - AttributeError: module 'cv2.cv2' has no attribute face 属性错误模块 'cv2.cv2' 没有属性 'videocapture' - attributeerror module 'cv2.cv2' has no attribute 'videocapture' 模块“cv2.cv2”没有属性“cv” - module 'cv2.cv2' has no attribute 'cv' AttributeError:模块'cv2.cv2'没有属性'createFisherFaceRecognizer' - AttributeError: module 'cv2.cv2' has no attribute 'createFisherFaceRecognizer' AttributeError: 模块 'cv2.cv2' 没有属性 'samples' - AttributeError: module 'cv2.cv2' has no attribute 'samples' AttributeError: 模块 'cv2.cv2' 在 OpenCV 中没有属性 'freetype' - AttributeError: module 'cv2.cv2' has no attribute 'freetype' in OpenCV AttributeError: 模块 'cv2.cv2' 在 OpenCV 中没有属性 'faces' - AttributeError: module 'cv2.cv2' has no attribute 'faces' in OpenCV AttributeError: 模块“cv2.cv2”没有属性“createLBHFaceRecognizer” - AttributeError: module 'cv2.cv2' has no attribute 'createLBPHFaceRecognizer'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM