Can't import CV2 in jupyter notebook - python

I have install cv2 library but I can't import it to my jupyter notebook.
this is how I installed it:
import sys
!conda install --yes --prefix {sys.prefix} opencv
import cv2
>>>
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
I have tried to install libGL but still got that error. Any idea how can I solve this?

Instead of using conda, try using the pip formula, which is specifically built for the Python ecosystem, place this in a separate cell and run it:
!pip install opencv-python
... or if you want to use non-free modules:
!pip install opencv-contrib-python

Related

How do I install Python packages in Jupyter notebook running from WSL2+ubuntu20.04

I am running jupyter notebook from WSL2+Ubuntu20.04. However, the jupyter notebook image does not include many package.
For instance, I would like to install pandas and append sys path accordingly.
(UPDATE)
Based on the recommendation of Prayson, I did the following from Jupytr notebook terminal:
python3 -m pip install --upgrade --user pip
python3 -m pip install --user pandas
Both steps ran successfully. Then I could call the following code snipper successfully for the FIRST TIME only:
I ran the following code snippet:
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
However, when I shut down and restarted again, I could not import pandas again and I am getting following error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-df58772bf04b> in <module>
7
8 import numpy as np
----> 9 import pandas as pd
10 import matplotlib.pyplot as plt
11
ModuleNotFoundError: No module named 'pandas'
I may be missing adding path. However, I am not sure how to do it correctly.
Any guidance would be appreciated.
You just need to install the package from the terminal, no need to do anything in Jupyter.
To install pandas, type in terminal:
pip install pandas
Now import in jupyter, it will work fine.
Also, no need to use sudo.
Make sure you are installing packages in the correct environment. From the code trace above, it appears that Pandas is installed in environment serving Jupyter server.
import sys
# upgrade pip and install package
!{sys.executable} -m pip install –upgrade pip && !{sys.executable} -m pip install package
The above code, makes sure that a package is install in the environment that you are currently in session.
You can open terminal session in Jupyter that will give you assess to shell. I would recommend installing packages there as you would in normal terminal. See Jupyter Documentation for how to access terminal

Dealing with OCR import

I am trying to import below commands in jupyter:
from ocr import characters
from ocr.normalization import word_normalization, letter_normalization
# Helpers
from ocr.helpers import implt, resize, img_extend
but receive error :ModuleNotFoundError: No module named 'ocr'.
Tried finding reasons but could not find anything as ocr package.What am i missing.
You have to install the package via pip.
Go to the terminal and type:
pip install ocr
Or:
pip3 install ocr
Or try to find the conda command.
Than execute the script
Try pip install ocr or pip3 install ocr in terminal and try to import it again.
Personally, I recommend the using the module Pytesseract instead. It can be installed via pip: pip install pytesseract or pip3 install pytesseract
import pytesseract as ptr
print(ptr.image_to_string("image.jpg"))

Gluonnlp installation not found on Sagemaker jupyter notebook

I am attempting to install Gluonnlp to a sagemaker jupyter notebook. Im using the command !sudo pip3 install gluonnlp to install. Which is successful. However on import I get ModuleNotFoundError: No module named 'gluonnlp'
I got the same issue when attempting to install mxnet with pip in the same notebook. It was resolved when I conda installed mxnet instead. However conda install has not been working for gluonnlp as it cannot find the package. I can't seem to find a way to conda install gluonnlp. Any suggestions would be highly appreciated.
Here are some of the commands I have tried
!sudo pip3 install gluonnlp
!conda install gluonnlp --> Anaconda cant find the package in any channels
!conda install pip --y
!sudo pip3 install gluonnlp
!sudo pip3 install gluonnlp
!conda install -c conda-forge gluonnlp --y
All these commands on my import
import warnings
warnings.filterwarnings('ignore')
import io
import random
import numpy as np
import mxnet as mx
import gluonnlp as nlp
from bert import data, model
result in the error
ModuleNotFoundError: No module named 'gluonnlp'
this is as simple as creating a Jupyter notebook using the 'conda_mxnet_p36' kernel, and adding a cell containing:
!pip install gluonnlp

No module named 'cv2.cv2'

I am a beginner at computers. I use Anaconda python 3.6 in windows 10. I have already installed OpenCV using this command:
pip install opencv-python
But when I try to import cv2 using this:
import cv2
this error shows up:
How can I install openCV for python?
Based on python opencv link: https://pypi.org/project/opencv-python/
Step 1:
Uninstall the opencv first if you have previous/other manually installed (= not installed via pip) version of OpenCV installed (e.g. cv2 module in the root of Python's site-packages)):
pip uninstall opencv-python
Step 2:
Install the package afresh
pip install opencv-python
Hope that works!
try this:
Create Virtual Environment
conda create --name opencv-env python=3.6
Activate the environment
activate opencv-env
Install OpenCV and other important packages
pip install numpy scipy matplotlib scikit-learn jupyter
pip install opencv-contrib-python
pip install dlib
Test your installation
import cv2
cv2.__version__
In my case, using Python 3.8 on Windows 10 and Pycharm (or VS Code as well), I have this same issue.
Finally I noticed that the Antivirus (Nod32) deletes the cv2.cp38-win32.pyd file that should be in the cv2 folder. I simply paused the protection, installed opencv with pip install opencv-python command and it worked just fine.
I hope it helps someone.
This happened to me because I setup a virtual environment with python 32-bit and my modules required the 64-bit version so it seemed there was a version conflict of CV. Changing the python version in my environment fixed the issue.
I was facing a similar issue. In my case, the issue occured because of previous dependencies.
!pip install easyocr
!pip install imutils
if you are running these commands first and then importing
import cv2
from matplotlib import pyplot as plt
import numpy as np
import imutils
import easyocr
then you will get this error. So you first import CV2 and then pip install easyocr or other libraries. This worked in my case.
I used the answer provided here and it worked.
By running pip install opencv_python-3.4.5-cp36-cp36m-win_amd64.whl
I had this exact same problem on Windows 10. Uninstalling via pip and then reinstalling in my virtual environment fixed everything up for me.
Here's a link that helped https://pypi.org/project/opencv-python/
Try reinstalling openCV — it worked for me.
To uninstall:
pip uninstall opencv-python
To reinstall:
pip install opencv-python

no module cv on jupyter

I am working on Jupyter Notebook on my windows. I am trying to import module cv and cv2.
import cv
produces this error.
ImportModuleError: No module cv
Similarly for
import cv2
it produces the error :
ImportModuleError: No module cv
Please help me solve this issue.
To install the opencv-python package without the optional contrib modules into python3:
pip3 install opencv-python --user
To install the opencv-python package with the optional contrib modules into python3:
pip3 install opencv-contrib-python --user
Then to import the module:
import cv2
For both of these installations, their pypi sites state: 'This version does not support video related functionality for MacOS and Linux'.
The Pyimagesearch website by Adrian Rosebrock has a tutorial on how to download and compile opencv for Ubuntu with this functionality.

Categories