1

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.

theberzi
  • 2,142
  • 3
  • 20
  • 34
  • 1
    shouldn't it be `print(cv2.__version__)` ? – Jeru Luke Jul 08 '18 at 15:29
  • Oof. Rookie mistake. I retried with the correct call and it does work in the console, and (within pycharm) typing `cv2.` does list various methods such as `cv2.imread`, but in my module it still doesn't work and the init file in cv2 still has an unresolved reference. What may cause this discrepancy? – theberzi Jul 08 '18 at 16:02

4 Answers4

3

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

Xaaris
  • 51
  • 5
0

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 -> +

taront
  • 146
  • 4
  • Yes I installed through PyCharm. Did doing that solve it for you and allow and PyCharm to index the modules? It might be due to the new version, since when I posted the answer I was still on 2017. I should note that I have since found an alternative to opencv for what I needed. – theberzi Aug 02 '18 at 17:59
  • I'm sorry I couldn't be mach of a help. I checked with `PyCharm Community Edition 2018.2` and `PyCharm Community Edition 2017.3.3` . In my case, cv2's autocomprication worked only when installed through PyCharm. – taront Aug 03 '18 at 00:47
  • Never install both `opencv-python` and `opencv-contrib-python` in the same environment. Only install one of them. `opencv-python` contains only core modules. `opencv-contrib-python` contains both core modules and extra modules. If you install both, there will be unexpected problems. – skvark Aug 08 '18 at 06:21
0

I got this same issue. You need to try a couple of things.

  1. You need to simply import cv2 instead of cv2.cv2. Simply write "import cv2"
  2. If you have installed any other library such as cv before, then uninstall it first.
  3. The library that we need to install is opencv-python
  4. You need to install it via IDE not with the terminal. Steps are as follows: File -> Settings -> (Click on your project) -> Project Interpreter -> + -> (Type opencv-python) -> Download and install it -> It should work now.
Hassan Shahzad
  • 455
  • 6
  • 14
0

From Python Interpreter download the version of opencv-python 4.5.4.60 and opencv-contrib-python 4.5.5.64.