AttributeError: module 'cv2.cv2' has no attribute 'bgsegm

前端 未结 5 988
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 20:54
import numpy as np
import cv2
cap = cv2.VideoCapture(\'vtest.avi\')
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
while(1):
    ret, frame = cap.read()
    fgmask =          


        
相关标签:
5条回答
  • 2021-02-19 21:01

    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.

    0 讨论(0)
  • 2021-02-19 21:05

    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()
    
    0 讨论(0)
  • 2021-02-19 21:18

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

    0 讨论(0)
  • 2021-02-19 21:19

    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
    
    0 讨论(0)
  • 2021-02-19 21:26

    If you are using python3, write:

    fgbg =cv2.createBackgroundSubtractorMOG2() #For python3
    

    And not:

    fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
    
    0 讨论(0)
提交回复
热议问题