opencv import causes python to crash - python

I've installed opencv using following command:
pip3 install opencv-python
And I'm using anaconda environment to run my code on Jupyter notebook. But When I try to import opencv like following:
import cv2
It gives a dialog with Python has stopped working. I've also tried to install opencv via conda like following:
conda install -c menpo opencv
But this gives error:
following spec. found conflict
-opencv
-python 3.5*
So what am I missing here?

Okay I figured out the solution. Just ran the following command and now it's working.
conda install -c menpo opencv3

Related

PyCharm does not find cv2 no matter what I try

I tried to install OpenCV in so many ways. I tried this tutorial:
https://docs.opencv.org/master/d0/db2/tutorial_macos_install.html
I tried using conda:
conda install -c conda-forge opencv
etc. etc.
But when I type in import cv2 it says:
PyCharm Image OpenCV
Does somebody have an idea?
To install opencv you can either build it with CMake (like the link you tagged) or install a pre-built version of it (easier).
You can do the pre-built via PyCharm going to your project settings and adding the 'opencv-python' package or running pip install opencv-python
If you need both main and contrib modules you can install 'opencv-contrib-python' instead
Here's the package's page: https://pypi.org/project/opencv-python/

problem while installing python package in spyder

I'm trying to install python library called "librosa" in spyder anaconda.
I tried conda install -c conda-forge librosa from here to install package.
This was result in anaconda shell.
But when I try to import librosa in spyder, it gives error.
How to fix this?
Thank you!

importing opencv version 3.4.2.17 in python 3.7

I have already installed opencv in anaconda prompt by entering the command:
pip install opencv-python
it installed properly, but I'm unable to import the module in jupyter notebook.I entered the following command in jupyter:
import opencv
but it's showing ModuleNotFoundError
Also, to resolve anaconda environment issues, I entered the following command in anaconda prompt:
conda install -c conda-forge opencv
this further downloaded and extracted packages:
libopencv-3.4.1,opencv-3.4.1,py-opencv3.4.1
but still I'm unable to import opencv or libopencv module in my Jupyter notebook.
What should I do to use opencv3.4.2.17 in my python version3.7?
The correct syntax for importing opencv is as follows:
import cv2
If opencv was correctly installed, it should work.
For installation of OpenCV, use:
pip install opencv
To import OpenCV, use:
import cv2

How do I install opencv using pip?

I need to install cv2 for a script that has been written for me. I tried pip install cv2 and pip install open_cv and got the same problem - a warning message from dist.py and complains about zlib being not found. No cv2 installed. I also tried pyopenvc and pip install opencv-python.
So, I went to the opencv site and downloaded the relevant exe. Ran it - generated a heap of subdirectories and a make file and stuff.
What do I do now?
Install opencv-python (which is an unofficial pre-built OpenCV package for Python) by issuing the following command:
pip install opencv-python
run the following command by creating a virtual enviroment using python 3 and run
pip3 install opencv-python
to check it has installed correctly run
python3 -c "import cv2"
In pip package management, there are 4 different OpenCV packages all using the same namespace, cv2. Although they are not officially supported by OpenCV.org, they are commonly used in developers' community. You could install any of them using the following command:
pip install PACKAGE_NAME
where PACKAGE_NAME can be
opencv-python (only contains main modules)
opencv-contrib-python (contains both main and contrib modules)
opencv-python-headless (same as opencv-python but without GUI functionality)
opencv-contrib-python-headless (same as opencv-contrib-python but without GUI functionality)
You should only install one of them depending on your needs. If you accidentally installed multiple of them in the same environment, you can remove them using pip uninstall before installing the correct one again.
For more details, you can refer to the project description of OpenCV on Wheels.
As of 2021, all of these 4 packages are official OpenCV projects. Source: OpenCV Website.
To Install the Current Latest version of OpenCV then use the below commands:
Use this Command:
pip install --upgrade opencv-python
If you're facing problem in above command then try this :
pip install --upgrade opencv-contrib-python
To check the version of installed OpenCV:
import cv2
print(cv2.__version__)
Simply use this for the so far latest version 4.1.0.
pip install opencv-contrib-python==4.1.0.25
For the default version use this:
pip install opencv-contrib-python
If you have a new Raspberry Pi and want to install OpenCV then this tutorial would be a good choice.
For Ubuntu/Linux users:
sudo apt install python3-opencv
As of 10/22/2019, I think the best answer is simply
conda install opencv
It appears opencv is now in the main Anaconda channel.
To see which packages (including opencv) are in the main Anaconda channel go to Anaconda Package Lists and follow the link corresponding to your python version and os version.
Everybody struggles initially while installing OpenCV. OpenCV requires a lot of dependencies in the backend. The best way to start with OpenCV is, install it in a virtual environment. I suggest that you use the Python Anaconda distribution and create a virtual environment using it. Then inside the virtual environment, you can install OpenCV using this command:
conda install -c conda-forge opencv
Please follow the command:
pip install opencv-python
then if you want to use:
import cv2
If it's not worked due to any update, please follow the documentation
Make a virtual enviroment using python3
virtualenv env_name --python="python3"
and run the following command
pip3 install opencv-python
to check it has installed correctly run
python3 -c "import cv2"
To install open_cv you can go to this website or do this,
pip install opencv-contrib-python --upgrade
pip install opencv-python
You can test it by:
C:\> python
>>> import cv2
>>> print(cv2.__version__)
'4.5.1' # your version may be a newer one
You can install opencv the normal way:
pip install opencv-python
If you are getting errors, you can do this:
pip install opencv-python-headless
Open anaconda command prompt and type in below command.
conda install -c conda-forge opencv
Once the 'Solving environment' is done. It will ask to download dependencies. Type 'y'.
It will install all the dependencies and then you are ready to code.
I recommend this for Python 3: Please install it this way with pip
pip3 install opencv-python
This will download and install the latest version of OpenCV.
You could try using below command-
pip install opencv-contrib-python
It will basically download the compatible version. If this command fails, you could upgrade you pip using below command-
python -m pip install –upgrade pip
If you need a pictorial guide, head over to Simple Steps to Install OpenCV in Windows
You can also try installing OpenCV from prebuilt binaries from the official OpenCV site.
->pip install opencv-python you can use this.
But if this code does not working then you can check python version on cmd and anaconda because they are different. So you type command in anaconda prompt and cmd, it will work. You can check this -> pip list
Open terminal
Run the following command
pip install --trusted-host=pypi.org --trusted-host=files.pythonhosted.org opencv-python.
Hope it will work.
Installing cv2 or opencv-python using pip is sometimes a problem. I was having the same problem of installing cv2 with pip. The installation wasn't a problem the problem was to import cv2 after installation. I was getting an Import Error so to fix this i import main from pip to install opencv-python. Try to run the following code in your python file then opencv-python will be installed
from pip._internal import main as install
try:
import cv2
except ImportError as e:
install(["install", "opencv-python"])
finally:
pass
I hope this will help someone
As a reference it might help someone... On Debian system I hard to do the following:
apt-get install -y libsm6 libxext6 libxrender-dev
pip3 install opencv-python
python3 -c "import cv2"
On Ubuntu you can install it for the system Python with
sudo apt install python3-opencv
if you are using Pycharm navigate settings > Project:name > Project interpreter just search the module by name(in this case OpenCV-python) and install it. worked for me
In case you use aarch64 platform with ARM64 cpu - and/or docker
On a development board on ARM64, no python-opencv version were found at all
version: NONE.
I've had to build from source. This allowed to include CUDA support.
In my case it was already available on the board but it wasn't found on the development environment.
If compiling from source is out of reach, there are Dockers
Of course compiling will take some time (few hours on ARM core), but it is worthy process to know as most open source tools can be built this way in case of issues.
I've had this problem in Google Colab, It only worked with this specific package version.
!pip install "opencv-python-headless<4.3"
There are two options-
pip install cv2
or
pip install opencv-python
Hope it helps.

Can't pip install GDAL for python 3.6 on mac os

I can pip install GDAL for the python2.7 on my mac just fine using pip. However when I try to install the package for my python3.6 using
pip3 install gdal I get a long error message that ends with this:
warning: unknown warning option '-Werror=unused-command-line-argument-hard-error-in-future'; did you mean '-Werror=unused-command-line-argument'? [-Wunknown-warning-option]
extensions/gdal_wrap.cpp:3168:10: fatal error: 'cpl_port.h' file not found
#include "cpl_port.h"
^~~~~~~~~~~~
1 warning and 1 error generated.
error: command 'clang' failed with exit status 1
I tried re-installing GDAL using homebrew as suggested here, Unable to install GDAL in python 3 using pip (clang failed with exit status 1), but no luck. Looking for any suggestions.
I have tried a lot to install GDAL package using PIP and ended up in either missing some modules or encountered errors. So, best solution for installing GDAL is to setup condo environment and install gdal which will work :
conda create --name yourenvnamehere python=python3.7
conda activate yourenvnamehere
conda install -c anaconda numpy
conda install -c conda-forge gdal
if you have followed the above code:
import gdal
should work. Happy coding with spatial data:)
Do you have anaconda python installed?¿ Inside anaconda navigator you create an environment for python 2.7 or python 3.6, in this case you want 3.6 so create it. And then inside this env you can search for the lib and install it.
if it doesn't work try getting in your terminal (anaconda promp) you get inside your environment doing:
activate Anaconda3_64
(Anaconda3_64 is the name of the environment you defined inside anaconda navigator) and try pip install there.
pip install gdal
it worked for me but i have Windows so hope its the same in mac.

Categories