Why the Python crashes on importing cv2? - python

I am just beginning to learn OpenCV in python for a face recognition project, I installed opencv-python in an python environment using pip and tried to run this simple program
import cv2
img = cv2.imread('.\\jerry.jpg',1)
cv2.imshow('jerry',img)
cv2.waitKey(0)
but as I executed this, the python stoped without any error. Like this:
(venv) C:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice>python -u "c:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice\1_read.py"
(venv) C:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice>
When I run this program
a = 12
b = 2131
print(a+b)
import cv2
print(cv2.__version__())
It outputs
(venv) C:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice>python -u "c:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice\simple.py"
2143
(venv) C:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice>
This confirmed python stopped working on statement import cv2
This same is also happening when i tried to import numpy
I tried searching for this, but all in vain. I reinstalled opencv-python, deleted and again created virtual environment, tried uninstalling and reinstalling numpy, but it didn't helped.
I am using windows 7 32-bit, python 3.8.10, these are the installed libraries in the environment
numpy==1.24.1
opencv-python==4.7.0.68
Please help me out, I am stuck here. If any other details are required, please let me know.
Thanks
Edit:
I have figured something out, It is being caused because of numpy, when cv2 imports numpy, it crashed there. This issue is being discussed at github
I tried to reinstall opencv-python, numpy. Destroyed and recreated virtual environment.

Related

Error: Import cv cannot be resolved: How to install OpenCV correctly?

To keep a long story short. I copied this code from https://www.geeksforgeeks.org/detect-an-object-with-opencv-python/ (not really import but I still mentioned it)
import cv2
from matplotlib import pyplot as plt
# Opening image
img = cv2.imread("image.jpg")
# OpenCV opens images as BRG
# but we want it as RGB and
# we also need a grayscale
# version
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Creates the environment
# of the picture and shows it
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()
Anyways this line below has this error that says "Import "cv2" could not be resolved"
import cv2
When I run this code below to download opencv. I get the message that the "Requirement already satisfied: "
pip install opencv-python
Despit me closing vscode the reopening I still get that message that "Import "cv2" could not be resolved" Ive updated pip, I've printed the version of cv2 in cmd which I get 4.5.5, I've deleted python and python intelligence. I just cant to figure out why opencv isn't working. Any suggestions? (I'm a newbie lol)
According to your description, I think there should be multiple versions of Python in your system.
You could use "ctrl+shift+P" to choose your python interpreter.
You can also use pip's command to install opencv into the current Python package floder.
pip install -t FloderPath opencv-python
Two reasons come to mind:
1.) Either the path to the opencv library is not set in the editor so it cannot find it. That happens sometimes if you changed settings in the runtime environment.
2.) Or you created a virtual environment prior to installing opencv. In that case, install opencv for your virtual environment as well.
It is difficult to tell what is going on with so little information. The above causes are just the most likely. Check your runtime environment and your library paths.
u need to do several things to clear it up, open cmd and follow this,
step 0: of course u must check python is added in ur system variable path,two path is essential, for me two dir is like this (add for ur installation, if have added this just skip this step)
C:\Python\Scripts\
C:\Python\
step 1: clear current package from python site package
pip uninstall opencv-python
step 2: clear cache from pip , for fresh install
pip cache dir
u will get a list of dir printed, now browse to that dir using explorer and delete everything in there.
step 3: check pip package install dir for ur python, it should be like "..\python\lib\site-packages" , to check this type into cmd
pip list -v
this will give u dir reference of all site packages, and u should check whether it is
"..\python\lib\site-packages" or not.
step 4: reinstall the opencv , u can install community contrib version of opencv, this is extended package of opencv-python with extra modules
pip install opencv-python
or
pip install opencv-contrib-python
step 5: type python in cmd, if python idle responded in cmd, then ur system found python, then type import cv2 and make sure it is imported. If it is imported successfully then u need to make sure ur vs code python plugin in up to date and configured well, for me i have added python path to system variable and didn't have to configure the plugin, it works well.
let me know if u have the problem unsolved.

Python import cv2 runs forever

My OS is ubuntu 16.04, Python 2
I installed opencv followed this link, the only difference is that I am not using an virtual python environment.
After installation is done, there is a cv2.so file in both /usr/lib/python2.7/dist-packages and /usr/local/lib/python2.7/dist-packages.
However, when I try to import cv2 in Python, it will keep importing forever like this
Any ideas?
Update: After waiting for a loooooong time, cv2 was imported successfully

How to import cv2 in python project "Visual Studio 2015"?

I would like to start with OpenCV . I followed some tutorials for installing OpenCV in Windows.
After downloading and extracting OpenCV
First, I create environment variable and path
Then I moved to python in Visual Studio. I installed python and I started with a very simple tutorial, but I faced some problems with python.
First problem was with numpy module was not found. solved
cv2 error I couldn't solved it, I followed instruction said that I need copy cv2.pyd file in ..\Python34\Lib\site-packages
afterword I tried to >>> import cv2
but I got this error
Thanks for all of you.

Importing OpenCV with python 2.7 in Virtualenv and PyCharm

I am struggling with installing opencv for python 2.7.11 on OSX for almost three days now.
After some failures, I accomplished a first success by following the instructions here. So my basic setup is python 2.7.11 running from ~/.virtualenvs/cv_env/bin/python and I have a cv2.so located in ~/.virtualenvs/cv/lib/python2.7/site-packages/.
So good so far. Using source ~/.virtualenvs/cv_env/bin/activate, I can activate the virtualenv and than use import cv2. For some reasons, this does not work always. From time to time, I have to deactivate first and than reactivate (any guesses?).
Next, I wanted to use opencv in PyCharm. Under "preferences - Project interpreter", I selected the virtualenv interpreter and this is also working. I can import other moduals like numpy and pandas (previously installed into the vortualenv using pip). But for some reasons, I am not able to import opencv (import cv2). It always gives me
ImportError: No module named cv2
So my question is, why I am able to import opencv in terminal (at least sometimes) but not in PyCharm. Any ideas are welcomed.
Best,
Fabian
Your cv2.so located in a different directory. And you activating from another directory. I mean cv_env and cv.

PIL Image data can not convert to float

I am working through a few Python image tutorials. It seems that the greatest challenge every time I try something new in Python is just getting it to work! I have imported the PIL code and am trying to run the short snippet below in Spyder on Python 2.7.8
from PIL import Image
from pylab import *
im = array(Image.open('test.jpg'))
imshow(im)
show()
When I do so, I get "Image data can not convert to float" error.
I tried to do the same in an iPython notebook and received the same error. When I run pip freeze it shows that I have PIP 1.1.7 installed. Of note, I installed PIP using the executable at the host site, not with PIP. I am also using the Spyder instance that comes with Anaconda.
Any advice?
After trying other solutions posted I discovered that PIL may be out of date with my newer Python build (see link in comments). Once Pillow was installed the issue was resolved.

Categories