2

I am trying to use OpenCV on a python web application I created on an Amazon EC2 Micro instance running apache.

I've got everything configured and working, except OpenCV isn't installing. This is the output I got from the Apache Error Log.

[Thu Aug 04 18:31:54 2016] [error] [client 72.219.147.5]     import cv2
[Thu Aug 04 18:31:54 2016] [error] [client 72.219.147.5] ImportError: No module named cv2

Here is what I've tried:

I've installed pip and tried running pip install pyopencv

That doesn't work and gives me errors.

I've also tried manually installing it by following this: How to install OpenCV on Amazon Linux?

and this: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_setup/py_setup_in_fedora/py_setup_in_fedora.html?highlight=download#installing-opencv-python-from-pre-built-binaries

and this: http://techieroop.com/install-opencv-in-centos/

Even after installation, the cv2.so file is nowhere to be be found. I tried to search for it using sudo find / -name "cv2.so" but nothing came up.

I do, however, have the following .so files installed:

/usr/local/lib/libopencv_photo.so
/usr/local/lib/libopencv_stitching.so
/usr/local/lib/libopencv_flann.so
/usr/local/lib/libopencv_imgcodecs.so
/usr/local/lib/libopencv_videostab.so
/usr/local/lib/libopencv_ml.so
/usr/local/lib/libopencv_objdetect.so
/usr/local/lib/libopencv_imgproc.so
/usr/local/lib/libopencv_superres.so
/usr/local/lib/libopencv_core.so
/usr/local/lib/libopencv_video.so
/usr/local/lib/libopencv_highgui.so
/usr/local/lib/libopencv_features2d.so
/usr/local/lib/libopencv_shape.so
/usr/local/lib/libopencv_videoio.so
/usr/local/lib/libopencv_calib3d.so

Also, when running the cmake command, this is the output I'm getting:

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.10)
--     Libraries:                   NO
--     numpy:                       NO (Python wrappers can not be generated)
--     packages path:               lib/python2.7/dist-packages

Any help is appreciated.

Community
  • 1
  • 1
Shalin Shah
  • 8,145
  • 6
  • 31
  • 44
  • Did you copy cv2.so to site-packages under your python distribution (e.g. /usr/local/lib/python2.7/site-packages)? – bblincoe Aug 04 '16 at 19:22
  • @bblincoe where is the cv2.so file? In my /usr/local/lib/python2.6/ I don't have any cv2.so file but I have files for pip. In my /usr/local/lib/python2.7/ I only have a README – Shalin Shah Aug 04 '16 at 19:32
  • Take a look at this thread: http://stackoverflow.com/questions/15790501/why-cv2-so-missing-after-opencv-installed – bblincoe Aug 04 '16 at 19:37
  • Another good resource is http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/ (specifically geared towards Raspberry Pi, but still a Linux env) – bblincoe Aug 04 '16 at 19:40
  • EC2 uses yum, not apt-get, and some of the same dependencies aren't available in yum – Shalin Shah Aug 04 '16 at 19:44
  • Which OS did you choose when you launched your EC2 instance? CentOS? Any particular reason why you can't switch to a Debian platform? – bblincoe Aug 04 '16 at 19:49
  • 1
    What about this? http://techieroop.com/install-opencv-in-centos/ – bblincoe Aug 04 '16 at 20:04
  • http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_setup/py_setup_in_fedora/py_setup_in_fedora.html?highlight=download#installing-opencv-python-from-pre-built-binaries should help – be_good_do_good Aug 04 '16 at 20:36
  • @be_good_do_good I tried that but for some reason the cv2.so file can't be found. I tried to search for it using `sudo find / -name "cv2.so"` – Shalin Shah Aug 09 '16 at 05:12
  • @bblincoe I tried that but for some reason the cv2.so file can't be found. I tried to search for it using `sudo find / -name "cv2.so"` – Shalin Shah Aug 09 '16 at 05:13

3 Answers3

17

tested and working on amzn-ami-hvm-2016.03.1.x86_64-gp2

sudo yum install git cmake gcc-c++ numpy python-devel 
sudo pip install --upgrade pip
sudo ln -rs /usr/local/bin/pip /usr/bin/
wget https://pypi.python.org/packages/18/eb/707897ab7c8ad15d0f3c53e971ed8dfb64897ece8d19c64c388f44895572/numpy-1.11.1-cp27-cp27mu-manylinux1_x86_64.whl
sudo pip install numpy-1.11.1-cp27-cp27mu-manylinux1_x86_64.whl 
git clone https://github.com/Itseez/opencv.git
cd opencv
git checkout 3.1.0
mkdir build
cd build
cmake .. -DBUILD_opencv_python2=ON
make -j4
sudo make install
echo 'export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages/:/usr/local/lib/python2.7/dist-packages/'>>~/.bashrc;. ~/.bashrc
python -c 'import cv2; print "cv2 imported"'

most importantly after cmake step. you should see this in the output.

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.10)
--     Libraries:                   /usr/lib64/libpython2.7.so (ver 2.7.10)
--     numpy:                       /usr/local/lib64/python2.7/site-packages/numpy/core/include (ver 1.11.1)
--     packages path:               lib/python2.7/dist-packages

now if it is not showing up, you need to completely remove build folder and rerun cmake again after correctly installing numpy, just rerunning cmake inside your already existing build folder will not work.

Zaw Lin
  • 5,629
  • 1
  • 23
  • 41
  • I ran the `cmake` command, and realized that `numpy` wasn't correctly installed. So I did `sudo yum uninstall numpy`. Then I tried installing `numpy-1.11.1-cp27-cp27mu-manylinux1_x86_64.whl`, but I got `numpy-1.11.1-cp27-cp27mu-manylinux1_x86_64.whl is not a supported wheel on this platform` – Shalin Shah Aug 11 '16 at 05:12
  • I've also updated my question with the output I get when running `cmake` – Shalin Shah Aug 11 '16 at 05:21
  • hmm..try the whl listed here https://pypi.python.org/pypi/numpy. there's 4 for linux. – Zaw Lin Aug 11 '16 at 06:45
  • None of the four work. I keep getting this error: `numpy-1.11.1-cp27-cp27mu-manylinux1_i686.whl is not a supported wheel on this platform.` – Shalin Shah Aug 11 '16 at 07:41
  • that's strange. what's the ami id of your instance? – Zaw Lin Aug 11 '16 at 07:51
  • This is what it says when I ssh into my instance: `https://aws.amazon.com/amazon-linux-ami/2016.03-release-notes/` – Shalin Shah Aug 11 '16 at 08:02
  • did you upgrade pip? if it's not upgraded, you will get the platform error as well. – Zaw Lin Aug 11 '16 at 12:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120729/discussion-between-shalin-shah-and-zaw-lin). – Shalin Shah Aug 11 '16 at 18:40
  • I opened a new instance because it just wasn't working on the old one. It worked on the new instance. – Shalin Shah Aug 12 '16 at 06:19
  • I was using a t2.nano and during `make -j4` I got `make: *** [all] Error 2` at about 40%. When I tried it on a t2.small which has more RAM everything worked just fine. – Patch92 Sep 11 '18 at 23:01
3

First create a virtual environment for python with the updated version of python because python2.7 is installed on it by default. Take help from the following link:-

https://aws.amazon.com/premiumsupport/knowledge-center/python-boto3-virtualenv/

Then cd into the venv folder and execute the command :-

$ pip install opencv-python

Now run any script containing opencv by activating the virtual environment.

$ source /home/ec2-user/venv/python34/bin/activate

Done!!!

1

I tried the command below and it worked:

cmake -D PYTHON2_LIBRARIES=/home/ubuntu/anaconda2/lib/libpython2.7.so.1.0 \
-D PYTHON2_INCLUDE_DIR=/home/ubuntu/anaconda2/include/python2.7/ \
-D PYTHON2_EXECUTABLE=/home/ubuntu/anaconda2/bin/python \
-D HAVE_opencv_python2=ON ..
Marcs
  • 3,768
  • 5
  • 33
  • 42
tungat
  • 11
  • 1