DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. How to Deploy OpenCV on Raspberry Pi and Enable Machine Vision

How to Deploy OpenCV on Raspberry Pi and Enable Machine Vision

Want to learn how to enable machine vision on your Raspberry Pi? Check out this tutorial on how to deploy the OpenCV on your Raspberry Pi.

Francesco Azzola user avatar by
Francesco Azzola
CORE ·
Jul. 30, 18 · Tutorial
Like (7)
Save
Tweet
Share
21.18K Views

Join the DZone community and get the full member experience.

Join For Free

OpenCV is an integral part of machine vision. In this tutorial, you will learn to deploy the OpenCV library on a Raspberry Pi, using Raspbian Jessie for testing. You’ll install OpenCV from source code in the Raspberry Pi board.

How to Build OpenCV From the Source on a Raspberry Pi

To start building the OpenCV library from source code on Raspberry Pi, you’ll first need to install development libraries.

How to Install Development Libraries

Type these commands on the Raspberry Pi terminal:

$ sudo apt-get update
$ sudo apt-get install build-essential git cmake pkg-config libgtk2.0-dev
$ sudo apt-get install python2.7-dev python3-dev


You also need to install the required matrix, image, and video libraries:

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install libatlas-base-dev gfortran


The next step is to download the OpenCV source code via Git:

$ mkdir opencv
$ cd opencv
$ git clone https://github.com/Itseez/opencv.git
$ git clone https://github.com/Itseez/opencv_contrib.git


Using a Python virtual environment, deploy the OpenCV on Raspberry Pi with virtualenv. The benefit of this approach is that it isolates the existing Python development environment.

Check out these links below t0 learn more:

How to build a home surveillance system using Raspberry Pi and camera

How to build an IoT system using Raspberry Pi

Install and Configure Virtualenv for OpenCV

If your Raspbian hasn’t installed it yet, you can install it using pip:

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip


Then, you can configure a virtualenv in your bash profile:

$ nano ~/.profile


Now, add the following scripts:

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh


Next, save your bash profile file when finished.  To create a Python virtual environment, type this command:

$ mkvirtualenv cv


This command will create a Python virtual environment called cv. If you’re using Python 3, you can create the virtual environment with the following command:

$ mkvirtualenv cv -p python3


You should see (cv) on your terminal. If you close the terminal or call a new terminal, you’d have to activate your Python virtual environment again:

$ source ~/.profile</strong>
$ workon cv


The following screenshot shows a sample Python virtual environment form called cv:

https://www.packtpub.com/graphics/9781786466518/graphics/B05664_03_02.jpg

Inside the Python virtual terminal, continue installing NumPy as the required library for OpenCV Python. You can install the library using pip:

$ pip install numpy


Now, you’re ready to build and install OpenCV from the source. After cloning the OpenCV library, you can build it by typing the following commands:

$ cd ~/opencv/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D INSTALL_C_EXAMPLES=ON \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules \
        -D BUILD_EXAMPLES=ON ..


Next, install the OpenCV library on your internal system from Raspbian OS:

$ make -j4
$ sudo make install
$ sudo ldconfig


When you are done, you will have to configure the library so that Python can access it through Python binding. The following is a list of command steps for configuring with Python 2.7:

$ ls -l /usr/local/lib/python2.7/site-packages/
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so


If you’re using Python 3.x (say, Python 3.4), perform the following steps on the terminal:

$ ls /usr/local/lib/python3.4/site-packages/
$ cd /usr/local/lib/python3.4/site-packages/
$ sudo mv cv2.cpython-34m.so cv2.so
$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so


The installation process is over.

Verifying the OpenCV Installation on Raspberry Pi

Now, you need to verify whether OpenCV has been installed correctly by checking the OpenCV version:

$ workon cv
$ python >>> import cv2
  >>> cv2.__version__


You should see the OpenCV version on the terminal. A sample program output is shown in the following screenshot:

how to install opencv on raspberry pi







How to use OpenCV to Enable Computer Vision

The next demo displays an image file using OpenCV. For this scenario, you can use the cv2.imshow() function to display a picture file. For testing, log into the Raspberry Pi desktop to execute the program. Type the following script on a text editor:

import numpy as np
import cv2
img = cv2.imread('circle.png')
cv2.imshow('My photo', img)
cv2.waitKey(0)
cv2.destroyAllWindows()


Here, circle.png has been used as a picture source; you can use whichever file you want. Save the script into a file called ch03_hello_opencv.py; now, open the terminal inside your Raspberry Pi desktop and type this command:

$ python ch03_hello_opencv.py


If successful, you should see a dialog that displays a picture:

opencv running on raspberry pi


The picture dialog shows up because you called cv2.waitKey(0) in the code. Press any key to close the dialog.

Lastly, close the dialog by calling the cv2.destroyAllWindows() function after receiving a click event. And, there you have it!

raspberry pi OpenCV Python (language) Virtual environment Library Machine terminal

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Isolating Noisy Neighbors in Distributed Systems: The Power of Shuffle-Sharding
  • Introduction to NoSQL Database
  • Master Spring Boot 3 With GraalVM Native Image
  • Kubernetes-Native Development With Quarkus and Eclipse JKube

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: