Unresolved reference “cv2” inside cv2 (cv2.cv2)

浪尽此生 提交于 2019-12-24 10:48:12

问题


I've looked around and people seem to have similar problems but none described my case exactly, and solutions that worked for them didn't seem to work for me (or there was no answer to the question at all).

Using pycharm, after having installed opencv-python and opencv-contrib-python I noticed that import cv2 works, but when I tried accessing cv2.imread() pycharm complained about not being able to find it.

So I went to the cv2 init file, which looks like this:

import importlib

from .cv2 import *
from .data import *

# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module('cv2.cv2').__dict__)

Pycharm detects an unresolved reference on the from .cv2 import * line and I imagine the same problem happens on the last line - I tried doing the following in a python console:

import cv2
print(__version__)

But I got a NameError, which seems to confirm my suspicion.

As I wrote, I have tried installing opencv-contrib-python but that didn't seem to do anything and frankly I'm already out of ideas.

Notes: - I'm on Windows 10 x64. - I'm using Python 3.6 x64. - I have a virtual environment set up on my Pycharm project.


回答1:


I'm no expert but the following line worked for me:

import cv2.cv2 as cv2

Everything seems to work afterwards. Autocompletion is also back




回答2:


I found out that the problem is due to how cv2's __init__.py imports all the stuff, namely the globals().update() line.

In substance, everything does work, but since Pycharm doesn't index that kind of call, it doesn't detect the namespace as being properly imported. The solution would thus be to ignore the "error" and do without autocompletion.




回答3:


Have you installed opencv via terminal?

For example, like this.

$ pip install opencv-python
$ pip install opencv-contrib-python

I also experienced the same problem.

If you use pycharm, you should install opencv via pycharm.

File -> Settings... -> Project interpreter -> +



来源:https://stackoverflow.com/questions/51233491/unresolved-reference-cv2-inside-cv2-cv2-cv2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!