Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thepowerfuldeez opened this issue Aug 9, 2023 · 11 comments
Closed

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

thepowerfuldeez opened this issue Aug 9, 2023 · 11 comments
Assignees

Comments

@thepowerfuldeez
Copy link

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
@asmorkalov
Copy link
Collaborator

asmorkalov commented Aug 10, 2023

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.

@VadimLevin
Copy link

VadimLevin commented Aug 10, 2023

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'

@caoandong
Copy link

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?

@VadimLevin
Copy link

@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'

@caoandong
Copy link

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.

@thepowerfuldeez
Copy link
Author

Thank you! That should solve the problem

@alrafiabdullah
Copy link

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.

@lyheiyu
Copy link

lyheiyu commented Oct 10, 2023

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

@K-Ashik
Copy link

K-Ashik commented Nov 5, 2023

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

doombeaker added a commit to siliconflow/onediff that referenced this issue Nov 7, 2023
opencv-python==4.8.0.76 版本有问题,会报错

```text
AttributeError: module 'cv2.dnn' has no attribute 'DictValue'
```


opencv/opencv-python#884 (comment)
@stillgreyfox
Copy link

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

camilo-nunez added a commit to camilo-nunez/ca-bifpn that referenced this issue Dec 14, 2023
@soulteary
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants