OpenCV in Python using Pycharm [duplicate] - python

I have opencv-python installed and the .pyd file is added in the site-packages and the DLLs. The code works with images. When I want to read, show, write an image it works. But I get a warning that the functions' references cannot be found in init.py . Due to this, I can not use the auto-complete feature. Could someone help me out? I am using opencv 3.4.0 and python 3.6.4 in pycharm. I downloaded opencv via pip in the command prompt.

The problem is caused by CV2 and how __init__.py does the imports. Just ignore the warnings the program will work all the same, or you can do an import with an alias like:
import cv2.cv2 as cv2
If you have a warning on it press Alt+Enter to install and fix it. Now you will have the code completion and no other warnings (about that) on the project.

Switching to an older version of opencv solved this problem for me.
Apparently pycharm sometime breaks depending on the version of opencv. For me, the newest version was, 4.6.X unstalling and installing 4.5.X did the trick.

I use python 3.10 and my newest version for opencv is 4.6.0.66 ,by changing opencv version to 4.5.5.62 and with an alias: import cv2.cv2 as cv2 my problem has solved.

Import cv2 as follows:
from cv2 import cv2

I installed version 4.5.5.64 and imported with an alias.
import cv2.cv2 as cvv2

I was using Python 3.10.2288.0 and OpenCV 1.6.0.66.
I resolved the issue by rolling back the OpenCV version to 4.5.5.62.

Related

How to resolve this error of "Cannot find reference 'imread' in '__init__.py" in python after installing opencv?

I am new to python and installed opencv by typing: pip install opencv-contrib-python in the command prompt.
Below I have shown a very simple example of reading in an image and showing it on the screen:
However, I get this error:
When I hover over the imread method in PyCharm, it says Cannot find reference 'imread' in '__init__.py'
It was showing import and runtime errors when I typed import cv2 and import numpy in the command prompt.
I tried going back to the previous version of NumPy by typing pip install --force-reinstall numpy=1.19.3. So, now when I type import cv2 and import numpy in the command prompt, it shows no error but it says it still can't find a reference to it in PyCharm.
I am using Python 3.9.1 with pip 20.2.3.
If you are using python interpreter with anaconda change it to default python one. if you have not installed python separately then install it and assign the path to it.
somewhat this will be the path
c://user/APpData/Local/Programs/Python/python39/python.exe
The NumPy multicore import error is a result of installing NumPy on Windows that is incompatible with the version of OpenCV that you have. Judging from your screenshots as well as the error, you are running Windows. Try installing Christoph Gohlke's NumPy libraries instead that were built with multicore support enabled.
First do:
pip install pipwin
Then:
pipwin install numpy
This should hopefully settle the OpenCV dependency problem you have.
To solve it, you should upgrade numpy installation. Try:
pip install -U numpy
For information here
Instead of doing:
import cv2
try doing:
from cv2 import cv2
This worked for me after a lot of troubleshooting also using pycharm. I now have access to all of the regular cv2 methods.

What is the different between opencv-python library and cv2 library?

I am trying to use openCV. Normally, When I run command panel on desktop, I can see version of my openCV:
But when I couldn't import to my project. Normally, I was adding opencv-python library from interpreter but I builded cv2 library which version is 4.4.0 to use gpu. So I haven't got a opencv-python library at site-packages but I have cv2 file in site-packages. Unfortunately I couldn't import it to my project. How can I use this opencv version or file which name is cv2.cp37-win_amd64.pyd?
I can use all other libraries in site-packages. But when I couldn't add cv2 library. Here the a basic image read from file :
The interesting part of the problem is, all cv2 function cannot be found but program is working :D I am really confused.
cv2 is the module import name for opencv-python, "Unofficial pre-built CPU-only OpenCV packages for Python". The traditional OpenCV has many complicated steps involving building the module from scratch, which is unnecessary. I would recommend remaining with the opencv-python library.
The IDE probably doesn't recognize the cv2 commands because you are using a 64 bit version of opencv-python on a 32 bit version of Python or vice-versa. I believe this can be resolved by uninstalling your current OpenCV installation using pip uninstall opencv-python and then reinstalling it with the correct version following the linked tutorial.
Sources / Additional Links:
Install Tutorial: https://www.youtube.com/watch?v=Z78zbnLlPUA

Opencv imshow crashes python launcher on macOS 11.0.1 (Big Sur)

I'm trying to run some old code from gaussian filter when I find out that python launcher gets stuck trying to do the imshow function.
I tried:
Used Matplotlib to display a graph to see if the python launcher was the problem but no, graph showed up fine.
Remove process in between just to have the image read and display in fear that something in my code was breaking the launcher but no success.
Reinstalled opencv-python but no success.
Also saw one question like this in the google search but OP deleted it.
Has anyone encounter this issue or has any fix for this?
Example code:
import cv2 as cv
filename = 'chessboard.png'
img = cv.imread(filename)
cv.imshow('dst',img)
cv.waitKey(0)
OS: MacOS Big Sur (11.0.1)
I resolved the issue with below steps:
Install the anaconda.
Install the libraries needed.
Run the script, there is an error appeared as below:
You might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
then I installed two libraies:
pip install opencv-python opencv-python-headless
Retry run the script, the image can be shown on the left top of the monitor.
I was also facing the same problem. I solved it by just installing opencv-python-headless. use:
pip install opencv-python-headless
I also encounter this problem when I upgraded to Big Sur.
Uninstall anaconda(every package), and reinstall python.
pip install opencv-python opencv-python-headless
This does help me with imshow() but I can't run cv.face. This attribute is not found.
This solve my problem
pip install --force-reinstall opencv-contrib-python==4.1.2.30
This downgrade gets my code working again.
I naturally have my environment installed in anaconda.
I had to use the package
opencv-python-headless.
It is now running again.
Also the
cv.imread(...)
method
I also ran into this problem after installing macOS Big Sur. It was not only cv2.imshow() that did not respond but also cv2.namedWindow()
cv2.imread() was working however.
Solution was to install opencv-python-headless as others have said before
I had the same problem as you. I solved it by installing anaconda and using anaconda's virtual environment(PyCharm)
import cv2 as cv
import matplotlib.pyplot as plt
filename = 'chessboard.png'
img = cv.imread(filename)
cv.imshow('dst',img)
cv.waitKey(0)
I also found that pc hangs up at cv.imread(...).
Nothing was happened.
So I added line 2 import matplotlib.pyplot ...,
the image was displayed.
It seems to be a package reference issue. My solution solves the issue:
re-create conda env
re-install package, incl. opencv-python-headless
This is the snap of the error that I face (Click this link to watch)
This is common problem in Mac, not only in the Big Sur but also in the Catalina and others too.
and I solved this using a single command.
pip install opencv-python-head
And here watch the problem is solved now (Click this link to watch)

cannot import Image from PIL in Python 3.8

I am using macOS and installed pillow in the terminal with code pip3 install pillow and I got the version 8.0.1.
But I cannot import it in Pycharm. I typed from PIL import Image and an error message show ModuleNotFoundError: No module named 'PIL' . then I changed PIL to pillow from pillow import Image, it works but cannot find Image class.
How can I fix it?
Is very likely that your Pycharm has created a virtual environment for the project and is not using the same library as the python on the system.
If that is the case, go to Settings > Project > Python Interpreter, press on the plus sign (+), write Pillow on the search bar, select the right package, and add it to your interpreter running on PyCharm.
I recommend the use of the pipenv tool to manage your project dependencies for each project. It helps you to maintain the project dependencies separated from the system or other projects.
Pillow not pillow.
run pip uninstall pillow then run pip install Pillow.
Not sure if the solutions given have worked for everyone but try but writing the imports out separately. I tried everything and by accident this worked. So try as below, I hope this works for anyone who needs the help
from PIL import Image
from PIL import ImageTk

I have installed PIL with Homebrew, but when I try to import it, it says no module exists

I have installed PIL with homebrew. It then says that there was a symlink error, and that is can be easily fixed by doing it again, but with a sudo. I did so. Now, when I go into python, and import Image, it doesn't work! I've tried import PIL, import image, import pil, and none of them work. When I try to install it again, it says Error: pil-1.1.7 already installed. Please help!
I think you would see this issue if you were using the python binary that was not installed by homebrew along with a package that you did install via homebrew. Could you verify that the python binary you are using is not the one that was included in OS X by default?

Categories

Resources