/ ai

troubleshooting OpenCV (Raspberry Pi edition)

My previos article, Installing OpenCV on Raspberry Pi 3 B, generated a lot of feedback. Most people found it helpful, but some were having trouble getting OpenCV to run for the first time. I will try to list a few basic troubleshooting steps here - that will hopefully help you diagnose and fix your problem.

  1. Can you call OpenCV from Python, in case OpenCV was installed globally? My Python 2 example:
pi@raspberrypi:~ $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named cv2
>>> 

I can't, because installed OpenCV in a virtual environment, not system-wide.

  1. Can you call OpenCV from Python 3, in case OpenCV was installed system-wide?
pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named 'cv2'
>>> 

I can't, because installed OpenCV in a virtual environment, not system-wide. Just like the Python 2 example above, if you don't get an error trying to "import cv2" - that means OpenCV was installed system-wide, and you don't need a virtual environment to access it.

  1. What about the virtual environment I configured? Mine was called "cv", and when I open this virtual environment correctly, its name ("cv") is added in front of the command prompt, in parenthesis:
pi@raspberrypi:~ $ workon cv
(cv) pi@raspberrypi:~ $ 
(cv) pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.1'
>>> 

As you can see in this last example above, I am able to access OpenCV from my virtual environment.

One important thing to remember when using virtualenv - this tool creates isolated Python environments. So, if you're trying to use a library that was installed inside a specific environments, make sure that you are using that virtual environment!

virtualenv

I will keep adding to this page as I get more feedback. Comments and suggestions are welcome!