Anaconda Jupyter Notebook Opencv not working - opencv

Ok so to preface this, I am very new to jupyter notebook and anaconda. Anyways I need to download opencv to use in my notebook but every time I download I keep getting a NameError saying that ‘cv2’ is not defined.
I have uninstalled and installed opencv many times and in many different ways and I keep getting the same error. I saw on another post that open cv is not in my python path or something like that…
How do I fix this issue and put open cv in the path? (I use Mac btw) Please help :( Thank you!

Try the following:
Install OpenCV using Anaconda Navigator or via terminal by running:
conda install -c conda-forge opencv
Now you should check if its installed by running this in terminal: conda list
Import OpenCV in Jupyter Notebook: In your Jupyter Notebook, run import cv2 and see if it works.
If the above steps are not working, you should add OpenCV to your Python PATH by writing the following code to your Jupyter NB:
import sys
sys.path.append('/anaconda3/lib/python3.7/site-packages')
This should work.

Related

Cannot import opencv after succesfull install on Raspberry Pi

I try to install opencv on my Raspberry Pi with 4GB RAM (Raspberry OS version Linux 5.15.84-v8+ aarch64). Instructions I found on q-engineering. After a 2hr lasting build process the make announced that opencv was succesfully installed. When I try to import it I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
But opencv appears to be succesfully installed. When I run opencv_version I got:
$ opencv_version
4.7.0-dev
and:
/usr/local/bin/opencv_visualisation
/usr/local/bin/opencv_annotation
/usr/local/bin/opencv_version
/usr/local/bin/opencv_model_diagnostics
/usr/local/bin/opencv_waldboost_detector
/usr/local/bin/opencv_interactive-calibration
/usr/local/share/licenses/opencv4
/usr/local/share/opencv4
/usr/local/lib/aarch64-linux-gnu/cmake/opencv4
/usr/local/lib/aarch64-linux-gnu/pkgconfig/opencv4.pc
/usr/local/include/opencv4
/usr/local/include/opencv4/opencv2
/usr/local/include/opencv4/opencv2/opencv.hpp
/usr/local/include/opencv4/opencv2/gapi/opencv_includes.hpp
/usr/local/include/opencv4/opencv2/opencv_modules.hpp
Looking for cv2 or *cv2* yielded nothing except what was already listed above. The date/time of the files conform to the time I ran the install script.
Next I tried opencv-lite according to instructions from the same source. Same result: succesful installation, traces of newly put opencv files but import cv2 still fails.
To complicate matters I use anaconda environments. I got two: base and rpi. I installed with rpi activated. In both no trace of opencv or cv2 and in both cases import fails. Some information where the system fetches python:
$ which python
/home/arnold/bin/anaconda/envs/rpi/bin/python
(rpi) arnold#bottie:~/bin/opencv/opencv/build
There is python in /bin/python and /usr/bin/python I found out with vscode. I tried both and both failed.
My questions:
did installation of opencv really succeed?
if so, is it possible to 'connect' the installation to a running version of python, preferably a conda environment?
if not, what is the best way to proceed?
I try this links. install OpenCV 4 for RPI 4 It worked for me using RPi 4B/8GB 64-bit.
Do update first
Install OpenCV
Check getBuildInformation()
After that if not getting . Then install sudo apt
pyhton3-opencv-contrib
No needed to do cmake. And you ready to go.

Installing OpenCV using Virtual Environment (Ubuntu 16)

I want to install OpenCV. First thing I did was creating a virtual environment by following command in Ubuntu 16:
" conda create -n opencv3.4_py3.6 "
Then, I followed the official instruction here (Building OpenCV from source):
https://docs.opencv.org/3.4/d2/de6/tutorial_py_setup_in_ubuntu.html
However, when I tried to install dependencies, the following message came up:
Unable to locate package "Package Name"
As it can be seen int the attached image
My purpose is installing OpenCV in a Virtual Environment. Also, I want to import and use OpenCV in Spyder.
Any idea about how it can be done?
(Spyder maintainer here) After doing
conda create -n opencv3.4_py3.6
you need to run these other three commands to get what you want
source activate opencv3.4_py3.6
conda install opencv python=3.6*
conda install spyder
You don't need to compile Opencv because you can install it with conda.

ImportError('Could not import PIL.Image. ' working with keras-ternsorflow

I'm following some lectures from lynda.com about deep learning using Keras-TensorFlow in a PyCharmCE enviroment and they didn't have this problem.
I get this error:
raise ImportError('Could not import PIL.Image. '
ImportError: Could not import PIL.Image. The use of array_to_img requires PIL.
I have checked if others get the same error, but for me installing pillow using pip with the command pip install Pillow doesn't solve anything.
MacBook-Pro-de-Rogelio:~ Rogelio$ pip install Pillow
Requirement already satisfied: Pillow in ./anaconda3/lib/python3.6/site-packages
MacBook-Pro-de-Rogelio:~ Rogelio$
Any solution?
All you need to do is install pillow:
pip install pillow
Then you should be all set. Found this after hours of searching.
I had the exact same error and I fixed it the following way:
1) Run this command in your Jupyter Notebook:
import sys
from PIL import Image
sys.modules['Image'] = Image
2) Run the following two lines in your notebook to be sure that they are correctly pointing to the same directory (if not it's because your PIL old library is messing up with the Pillow library)
from PIL import Image
print(Image.__file__)
import Image
print(Image.__file__)
3) If that's working correctly and both import prints pointing to the same python3 directory then move on.
If not:
3.a) Go to your OS console and to your conda environment (be sure you are working within your desire conda environment) :
conda uninstall PIL
conda uninstall Pillow
conda install Pillow
You should now have successfully installed all the libraries for Pillow and let behind any problems with PIL.
3.b) Now try to execute the code of your jupyer notebook again, now the paths to both the imports should look exactly the same
4) Now, in the OS console/terminal, having your desired conda environment active, run the following commands:
conda install keras
conda install tensorflow
5) Run your jupyter notebook script again, It should be fixed and working now!
If it's still not working, it must be because you have opened a jupyter notebook kernel that's not point to the right environment. Fix that and you will be fine!
If this problem is being seen on an Anaconda env,
use
conda install pillow
and reopen
I ran into a similar issue with keras + tensorflow + miniconda.
I followed this advice from this issue : https://github.com/asataniAIR/Image_DL_Tutorial/issues/4 and did a pip install in conda admin console.
So I enter
pip install --upgrade tensorflow keras numpy pandas sklearn pillow
on anaconda prompt, and add
from sklearn.preprocessing import LabelEncoder
in python code instead
from PIL import Image
Here's what worked for me. Uninstall the conda version of pillow and install the pip version, then restart the kernel of your Jupyter Notebook
conda uninstall --force pillow
pip install pillow
pip install pillow
This did it for me as well.
I am using Jupyter Notebook, and Tensorflow2.0 Keras. To set the context, I got this error when i was trying to use builtin image.load_img() function in Keras. You will have to restart your kernel as well after doing this install.
I encountered the same problem while working on Pycharm. Even after trying various methods on the internet, I could not solve it. When I ran the code on Jupyter notebook, it asked me to install the module, SciPy. I installed it and the code is now working on Pycharm.
I have the same problem good news is it can be easily solvable.
my problem was:
I was using jupyter notebook for my python project which was launched using Anaconda navigator. In the python program I was using the function
image_ = image.load_img(image_path, target_size=(224,224))
then I got this error "raise ImportError('Could not import PIL.Image. ' ImportError: Could not import PIL.Image."
solution worked for me:
in the environment you are using for jupyternotebook firstly uninstall pillow using the command
conda uninstall --force pillow
then install it using the command
pip install pillow
(type these commands in the anaconda prompt)
then close all your notebooks and anaconda navigator, open anaconda navigator with the environment that you have installed before, in that environment launch the jupyter notebook and run the code
it will work
I had the exact same question. And I fixed it by changing my environment variables. Because I had two versions of python in my windows PC.
So I changed the priority, and moved the python 3.x version at the top position.
Then I reinstalled the pillow, and the problem was solved.
if pip and conda installing is not working for you try to:
pip3 install pillow
and then:
from PIL import Image
It should work! Check your python version and which one are you using as default! :)
Using a conda environment run conda install pillow. If you're using Jupyter notebook, don't forget to restart the Kernel.
This just worked for me.
If anyone wondering the above-mentioned process can be implemented using anaconda navigator.
Open the navigator, go to the Environments tab you are currently working and search pillow from the uninstalled tab before the search tab (if it's not installed in the current environment you are working)
Then select pillow and apply. Install then.
After that go to Home tab and launch Spyder.
If your using Anaconda 3, u have to install Pillow in the environment that your working on. just go to the not install section and search for Pillow, then install it to the environment. This way will nail the problem for you.
Install these:
$ pip install pillow -U
$ pip install pathlib -U
It may be a version issue, I had this issue with TensorFlow 3.2.0 and setting pillow to 8.2.0 worked for me somehow along the way of uninstall/reinstall/restart.

OpenCV installation Google Colab

I am trying to install OpenCV in google colab notebook.
The official documentation says to do:
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2
The first statement works fine and installs everything.
But on importing cv2 I am getting an error saying:
ImportError: libSM.so.6: cannot open shared object file: No such file or directory
Any fix for this issue?
Thanks for help!
OpenCV comes preinstalled on Google colab.
Simply import cv2 and use it.
I can't reproduce this issue. The snippet above seems to work fine.
https://colab.research.google.com/notebook#fileId=1OnUy6eFE7XhdfGfAHDCqQxpwueTOj_NO
Perhaps try restarting your backend via the Runtime -> Restart runtime menu.
Use Python 3.x.x version.
it works, I checked it.
Open CV comes preinstalled on Google Colab, so you can run it but if you want to run it in a window then you need to import more Colab opencv modules.ie. from google.colab import files

Anaconda - Manually adding a built package

I have build OpenCV (using cmake) and I can see the libs, include and bin files copied to my conda desired env. However, using conda list doesn't list the OpenCV. Is there a way to overcome this?
conda list will only show pip install or conda install installed packages.
To verify if OpenCV works, type below to check the version.
import cv2
print cv2.__version__
Hope this answer your question.

Resources