For my upcoming Computational Methods within the Civic Sphere class at Stanford, I wished my college students to have entry to OpenCV in order that they may discover computer-vision algorithms, resembling face detection with Haar classifiers.

On the Stanford FarmShare machines (which run on Ubuntu 13.10), I had bother getting their set up of OpenCV working, however was ready to make use of the
Anaconda distribution to put in each Python 2.7.8 and OpenCV 2.4.9.1 by way of the Binstar bundle repo.

Briefly, listed here are the directions:

  1. Get the Anaconda obtain hyperlink
  2. curl (*the-anaconda-URL-script*) -o /tmp/anaconda-install.sh && bash /tmp/anaconda-install.sh
  3. conda set up binstar
  4. conda set up -c opencv

Note: For Mac customers for whom `brew set up opencv` isn’t working: Anaconda labored nicely sufficient for me, although I needed to set up from a special pacakge repo:

conda set up -c  opencv

The Anaconda system, which I hadn’t used earlier than however discover actually handy, mechanically upgrades/downgrades the mandatory dependencies (resembling numpy).

Using Anaconda works fantastic on contemporary Ubuntu installs (I examined on AWS and Digital Ocean), however I wished to see if I might compile it from supply simply in case I could not use Anaconda. This ended up being a really painful time of wading via weblog articles and Github points. Admittedly, I’m in no way an knowledgeable at *nix administration, but it surely’s apparent there’s lots of incomplete and ranging solutions on the market.

The assist.ubuntu.docs on OpenCV are probably the most intensive, however proper on the high, they state:

Ubuntu’s newest incarnation, Utopic Unicorn, comes with a brand new model of libav, and opencv sources will fail to construct with this new library model. Likewise, some packages required by the script now not exist (libxine-dev, ffmpeg) in the usual repositories. The procedures and script described beneath will due to this fact not work not less than since Ubuntu 14.10!

The elimination of ffmpeg from the official Ubuntu bundle repo is, from what I can inform, the primary supply of errors when making an attempt to compile OpenCV for Ubuntu 14.04/14.10. Many of the directions cope with getting ffmpeg from a personal-package-archive after which making an attempt to construct OpenCV. That method did not work for me, however admittedly, I did not check out all of the doable variables (resembling model of ffmpeg).

In the tip, what labored was to easily simply set the flag to construct with out ffmpeg:

  cmake [etc] -D WITH_FFMPEG=OFF

I’ve created a gist to construct out all of the software program I would like for my class machines, however listed here are the related elements for OpenCV:

sudo apt-get replace && sudo apt-get -y improve
sudo apt-get -y dist-upgrade && sudo apt-get -y autoremove

# construct developer instruments. Some of those are most likely non-pertinent
sudo apt-get set up -y git-core curl zlib1g-dev build-essential 
     libssl-dev libreadline-dev libyaml-dev libsqlite3-dev 
     libxml2-dev libxslt1-dev libcurl4-openssl-dev 
     python-software-properties

# numpy is a dependency for OpenCV, so most of those different
# packages are most likely non-compulsory
sudo apt-get set up -y python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
## Other scientific libraries (clearly not wanted for OpenCV)
pip set up -U scikit-learn
pip set up -U nltk

### opencv from supply
# first, putting in some utilities
sudo apt-get set up -y qt-sdk unzip
OPENCV_VER=2.4.10
curl " -o opencv-${OPENCV_VER}.zip
unzip "opencv-${OPENCV_VER}.zip" && cd "opencv-${OPENCV_VER}"
mkdir construct && cd construct
# construct with out ffmpeg
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON 
      -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON 
      -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON 
      -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON 
      -D WITH_FFMPEG=OFF ..

A recurring situation I had come throughout – I did not check it myself, however simply noticed it within the number of hypothesis concerning the issue of constructing OpenCV – is that constructing with a Python aside from the system’s Python would trigger issues. So, for what it is value, the above course of works with 14.04’s Python 2.7.6, and 14.10’s 2.7.8. I’m not a lot of a Python person myself so I do not know a lot about greatest practices concerning surroundings…pyenv works fairly effortlessly (that’s, it really works identical to rbenv), however I did not strive it in relation to constructing OpenCV.

Also, this is not the naked minimal…I’m unsure what dev instruments or which cmake flags are are completely wanted, or if qt-sdk is required in the event you do not construct with Qt assist. But it really works, so hopefully anybody Googling this situation will be capable of make some progress.

Note: Other issues I attempted that didn’t work on clear installs of Ubuntu 14.04/14.10:

The Python code wanted to do easy face-detection seems to be one thing like this (based mostly off of examples from OpenCV-Python and Practical Python and OpenCV:

(You can discover pre=constructed XML classifiers on the OpenCV repo)


import cv2
face_cascade_path="/YOUR/PATH/TO/haarcascade_frontalface_default.xml"
face_cascade = cv2.CascadeClassifier(face_cascade_path)

scale_factor = 1.1
min_neighbors = 3
min_size = (30, 30)
flags = cv2.cv.CV_HAAR_SCALE_IMAGE

# load the picture
image_path = "YOUR/PATH/TO/picture.jpg"
picture = cv2.imread(image_path)

# this does the work
rects = face_cascade.detectMultiScale(picture, scaleFactor = scale_factor,
  minNeighbors = min_neighbors, minSize = min_size, flags = flags)

for( x, y, w, h ) in rects:
  cv2.rectangle(picture, (x, y), (x + w, y + h), (255, 255, 0), 2)

cv2.imwrite("YOUR/PATH/TO/output.jpg", picture)

Source link