As described in my first post, OpenCV is an important requirement for my application, In this post I will describe how I installed OpenCV on the Pi 3.
Previous posts:
[Pi IoT] Plant Health Camera #4 - Putting the parts together
[Pi IoT] Plant Health Camera #3 - First steps
[Pi IoT] Plant Health Camera #2 - Unboxing
[Pi IoT] Plant Health Camera #1 - Application
Install OpenCV
Next thing we need is OpenCV. I installed it from source, as described in this Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3 - PyImageSearch tutorial. This is a very well written tutorial, it doesn't make much sense to repeat all steps here, so below I only describe the steps in which I deviate from this tutorial.
First make sure that I indeed run the latest Raspbian version:
pi@pi3iot:~/planthealthcam $ cat /etc/os-release PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)" NAME="Raspbian GNU/Linux" VERSION_ID="8" VERSION="8 (jessie)" ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
Then I followed the steps of the above mentioned Install guide until 'Step #3: Download the OpenCV source code'.
I decided to do a git clone instead of downloading the zip file. This way it is much easier to update to the latest version.
pi@pi3iot:~/opencv $ git clone https://github.com/opencv/opencv.git Cloning into 'opencv'... remote: Counting objects: 191347, done. remote: Compressing objects: 100% (49/49), done. remote: Total 191347 (delta 22), reused 0 (delta 0), pack-reused 191298 Receiving objects: 100% (191347/191347), 414.50 MiB | 904.00 KiB/s, done. Resolving deltas: 100% (132424/132424), done. Checking connectivity... done. Checking out files: 100% (4791/4791), done. pi@pi3iot:~ $ git clone https://github.com/opencv/opencv_contrib.git Cloning into 'opencv_contrib'... remote: Counting objects: 15903, done. remote: Compressing objects: 100% (6/6), done. remote: Total 15903 (delta 0), reused 0 (delta 0), pack-reused 15897 Receiving objects: 100% (15903/15903), 114.08 MiB | 910.00 KiB/s, done. Resolving deltas: 100% (9087/9087), done. Checking connectivity... done. pi@pi3iot:~ $
Tutorial Step #4: Python 2.7 or Python 3? instructs to install pip. In my case pip seems to be already installed:
pi@pi3iot:~ $ pip --version pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
I also installed the virtualenvwrapper, as recommended by the tutorial.
I decided to use python3 for my project:
pi@pi3iot:~/planthealthcam $ mkvirtualenv cv -p python3 Running virtualenv with interpreter /usr/bin/python3 Using base prefix '/usr' New python executable in /home/pi/.virtualenvs/cv/bin/python3 Also creating executable in /home/pi/.virtualenvs/cv/bin/python Installing setuptools, pip, wheel...done. virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/predeactivate virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/postdeactivate virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/preactivate virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/postactivate virtualenvwrapper.user_scripts creating /home/pi/.virtualenvs/cv/bin/get_env_details (cv) pi@pi3iot:~/planthealthcam $
Note the (cv) preceding my prompt, indicating that I am in the cv virtual environment.
Since I cloned the distribution using git, I used the following make build:
cd ~/opencv/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Unfortunately the make -j4 build process stoppen after 44% (27 min) with an error compiling ffmpeg:
Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o
[ 44%] Built target opencv_dnn
In file included from /home/pi/opencv/modules/videoio/src/cap_ffmpeg.cpp:47:0:
/home/pi/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp: In member function ‘double CvCapture_FFMPEG::get_fps() const’:
/home/pi/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:1138:49: error: ‘AVStream’ has no member named ‘r_frame_rate’
double fps = r2d(ic->streams[video_stream]->r_frame_rate);
^
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:169: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o' failed
make[2]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o] Error 1
CMakeFiles/Makefile2:6100: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:147: recipe for target 'all' failed
make: *** [all] Error
Quickest solution was to exclude ffmpeg from the build:
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON \
-D WITH_FFMPEG=OFF ..
Now the make -j4 runs just fine, and after 95 minutes and 37 the make process finished at 100%.
Last step is to install it with:
$ sudo make install $ sudo ldconfig
Test OpenCV
After some small additional steps described in the tutorial I have a working OpenCV environment.
Lets give it a try:
pi@pi3iot:~/planthealthcam $ workon cv (cv) pi@pi3iot:~/planthealthcam $ python Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.1.0-dev' >>>
Now let's try a small demo program, in this python script a camera image is grabbed, and a canny edge detector is applied, showing the edges in the image. Both the input image and the edges are displayed.
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)
# allow the camera to warmup
time.sleep(0.1)
# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
edges = cv2.Canny(image,100,200)
# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.imshow("Edges", edges)
cv2.waitKey(0)
This gives me the following result:
Success!
An important software requirement for my plant health camera is now ready to use.
stay tuned for the next steps.


Top Comments