4

I have been trying to use the cv2 library in python (in IDLE on raspberry pi 3) but I can't manage to import it successfully.

when I type import cv2, I get this:

Traceback (most recent call last):
File "/home/pi/Desktop/python/test.py", line 2, in <module>
from .cv2 import *
ImportError: libjasper .so.1: cannot open shared object file: No such file or directory

I have used the terminal to update, upgrade, and install opencv along with pretty much every other thing I could find relating to opencv or lib. I currently can import cv2 in the terminal but not in IDLE. What do I need to do to be able to import the cv2 library? Thanks in advance.

wilson wilson
  • 113
  • 1
  • 6
  • Possibility 1: you have more than one Python installed on the system. Possibility 2: the '.' in '.cv2' makes the import relative to a starting point that is different in the two scenarios. It can depends on the startup methods and mode (interactive versus batch), which affect the current working directory. Adding `import os; print(os.getcwd())` before `import .cv2` might reveal something. – Terry Jan Reedy Nov 04 '18 at 16:48

3 Answers3

5

As discussed here, below installations seemed to resolve the issue. It worked for me.

pip3 install opencv-python
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4
sudo apt-get install python3-pyqt5
sudo apt-get install libqt4-test

Or

the newer OpenCV version handles other installations themselves.

pip install opencv-python==4.1.2.30
sam
  • 2,263
  • 22
  • 34
5

Recommended way of installing opencv on linux as given by opencv docs "https://docs.opencv.org/4.2.0/d2/de6/tutorial_py_setup_in_ubuntu.html" for python3 is:

sudo apt install python3-opencv

for python2:

sudo apt install python-opencv

On Raspberry pi you may occasionally need to run:

sudo apt update --fix-missing

It will auto-install all the dependencies.

Neeraj Jain
  • 598
  • 10
  • 10
0

Follow the commands for opencv and face recognition : (it worked for me)

-> sudo apt update

-> sudo apt upgrade

-> sudo apt-get install python3-pip

pi@raspberrypi:~ $ python3 -V
Python 3.5.3
pi@raspberrypi:~ $ python2 -V
Python 2.7.13

-> pip3 install numpy

-> pip3 install pillow

-> pip3 install dlib

-> sudo apt-get install cmake

-> pip3 install face_recognition

-> sudo apt-get install python-opencv

-> sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev

-> sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

-> sudo apt-get install libatlas-base-dev

-> sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev -y

-> sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y

-> sudo apt-get install libgtk2.0-dev libgtk-3-dev -y

-> sudo pip3 install opencv-python

-> sudo apt-get install libavformat-dev libswscale-dev openexr libopenexr-dev

-> sudo apt-get install libqt4-dev

-> sudo apt-get install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev



pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import dlib
>>> import face_recognition
>>> 

Depends on the system. for PI will take : 2 to 3 hr. (total time)

thanks :)

Ujwala Patil
  • 180
  • 14
M. D. P
  • 604
  • 2
  • 6
  • 18
  • What is the use of install `face-recognition` when the op has only asked to install opencv. You are installing too many packages that why it took you 2 to 3hrs – S Andrew May 04 '20 at 08:32