Skip to content

module 'cv2.dnn' has no attribute 'DictValue' #884

Closed

Description

@thepowerfuldeez

Hi! I've come to an issue with the latest opencv (4.8.0.76)
I think you've changed functionality which is now not backward compatible

Full traceback:

    import cv2
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 175, in bootstrap
    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.8/dist-packages/cv2/typing/__init__.py", line 169, in <module>
    LayerId = cv2.dnn.DictValue
AttributeError: module 'cv2.dnn' has no attribute 'DictValue'```

Python=3.7

Activity

  1. asmorkalov commented on Aug 10, 2023

    @asmorkalov
    Collaborator

    DictValue class type is defined in cv2/dnn/__init__.pyi:60 and works with interpreter:

    >>> q = cv2.dnn.DictValue(0)
    >>> print(q)
    < cv2.dnn.DictValue 0x7fefca480fd0>
    

    Tested with Ubuntu, python 3.8 and opencv-python 4.8.0.76 from pip.

  2. VadimLevin commented on Aug 10, 2023

    @VadimLevin

    Can't reproduce

    Python 3.11.4 (main, Jun 20 2023, 16:51:49) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import cv2
    >>> import cv2.typing
    >>> cv2.typing.LayerId is cv2.dnn.DictValue
    True
    >>> import platform
    >>> platform.platform()
    'macOS-12.6.3-x86_64-i386-64bit'
    >>> cv2.__version__
    '4.8.0'
    
  3. caoandong commented on Aug 19, 2023

    @caoandong

    Can confirm that this issue happens in the latest release by simply running import cv2.

    The previous version opencv-python==4.8.0.74 works fine,.

    Maybe it has something to do with the way import work?

  4. VadimLevin commented on Aug 21, 2023

    @VadimLevin

    @caoandong

    Can you confirm that you observe an issue in a clean virtual environment?

    python3 -m venv venv
    source venv/bin/activate
    pip install opencv-python==4.8.0.76
    python
    >>> import platform
    >>> platform.platform()
    'macOS-12.6.3-x86_64-i386-64bit'
    >>> import cv2
    >>> cv2.__version__
    '4.8.0'
  5. caoandong commented on Aug 26, 2023

    @caoandong

    OK yeah installing from the clean virtual env solves the problem.

    I think the issue has something to do with pip uninstall opencv-python not uninstalling the previously installed version.

    After searching and removing all previous installed versions of cv2 and reinstalling opencv-python it works.

  6. thepowerfuldeez commented on Aug 26, 2023

    @thepowerfuldeez
    Author

    Thank you! That should solve the problem

  7. alrafiabdullah commented on Oct 1, 2023

    @alrafiabdullah

    I faced this issue while using YOLOV8. For my case, the docker container had opencv-python & opencv-python-headless installed (both old versions). I uninstalled them using pip and then installed opencv-python==4.8.0.74.

  8. lyheiyu commented on Oct 10, 2023

    @lyheiyu

    For mac
    source activate 'your env'
    You can use a lower opencv version
    pip install opencv-python==4.5.0XXX

  9. K-Ashik commented on Nov 5, 2023

    @K-Ashik

    opencv-python==4.5.64 has solved the problems for me in macbook m1 slicon

  10. added a commit that references this issue on Nov 7, 2023
  11. stillgreyfox commented on Nov 12, 2023

    @stillgreyfox

    I was having this issue as well.

    1. I uninstalled all my opencv versions (found by pip list)
    2. Manually deleted the cv2 in the system python's dist-packages directory
    3. Finally, re-installing opencv-python-headless
    4. Worked fine with the latest version 4.8.1.78

    How to Fix Stale cv2 / opencv-python Confilcts

    # uninstall all versions of opencv
    pip uninstall $(pip list --format=freeze | grep opencv)
    
    # remove the stale cv2 dist-packages directory
    # (pay attention to your location and python version)
    rm -rf /usr/local/lib/python3.10/dist-packages/cv2/
    
    # I'm installing on a Docker image that I don't intend to give a display,
    # so I use the -headless package to avoid installing libgl stuff
    # install latest opencv-python
    pip install opencv-python-headless
    
    # (or opencv-python if you have the gl stuff)
    
    python -c "import cv2; print(cv2.__version__)"
    4.8.1

    Bonus Context

    I'm setting this up on the nvcr.io/nvidia/pytorch:23.10-py3 image, which comes with opencv 4.7.0 installed, and I too was installing requirements for YOLOv8 and anomalib, which bumped me to the latest opencv-python, and then I found out that I wanted opencv-python-headless, because I'm doing a headless docker system, and this package doesn't depend on libgl.so.1.

    If you need to use opencv-python, it should work with this fix, just make sure you have the dependencies that fix the missing libgl.so.1 problem:

    Fix for libgl.so.1 if you need to use opencv-python

    libgl.so.1 opencv-python fix

    apt install ffmpeg libsm6 libxext6 -y
  12. soulteary commented on Jan 7, 2024

    @soulteary

    The above processing process was made into a small tool, thanks @stillgreyfox

    # download the autofix tool
    pip install opencv-fixer==0.2.5
    # execute and wait is done
    python -c "from opencv_fixer import AutoFix; AutoFix()"

    https://github.com/soulteary/opencv-fixer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions