1

I have installed the Open Computer Vision library on a Raspberry Pi 3 Model B with camera module, using these steps:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-setuptools
sudo apt-get install python-pip
sudo apt-get install ipython python-opencv python-scipy python-numpy python-pygame

I would like to use this in a project using Python 3.4.2 as supplied with the device, but the "import cv2" statement (first line) is getting an error message "ImportError: No module named 'cv2'". This does not happen if the same file is run under Python 2.7.9, also on the Pi.

2

In Debian, Python 3 compatible packages are explicitly named python3-x. Packages such as python-opencv and python-scipy are the Python 2 versions of those packages; you will need to install the Python 3 versions. Packages generally don't provide Python 2 and 3 modules — they either offer Python 2 or 3 modules individually.

You will need to run:

sudo apt-get install python3-numpy python3-scipy

To install numpy and scipy for Python 3. There is no python3-opencv or python3-pygame for Stretch; you will need to follow alternative methods such as compiling OpenCV from source (note that this is significantly more troublesome than just using the package, so this is a rare occasion in which I'd recommend sticking with Python 2).

  • Couldn't he just download the python source for the modules and use them rather than having to compile the entire OpenCV library from source? – NomadMaker Jan 30 '18 at 15:17
  • @NomadMaker I'm not aware of that being possible (that said, I'm not intimately familiar with the package either), but the documentation says the Python wheel has a statically built version of OpenCV, so I suspect building is necessary. I'd be interested to hear if someone has found a way to avoid building the entirety of OpenCV, though. – Aurora0001 Jan 30 '18 at 15:29
  • I'll be watching this question to find out. If I have the time I may just try to do this. I can't imagine that recompiling OpenCV is all that difficult even on a Raspberry Pi. When I started with Linux, the computers were less powerful than the RasPi and our benchmark for speed was how long it took to recompile the entire system. At least he can get the OpenCV source for the Raspberry Pi and just modify it so to add the python libraries he needs. – NomadMaker Jan 30 '18 at 15:38
1

As has been pointed out opencv2 and Python 3 are not compatible. You can however get that Python3 Experience™ at almost no extra effort by importing the future.

To do this put this line at the very top of each file:

from __future__ import (absolute_import, division,
                    print_function, unicode_literals)

You will still need to use the python 2 version of all of your other packages but this should work.

I would recomend checking out this article for more infomation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.