Домой United States USA — software How to Deploy OpenCV on Raspberry Pi and Enable Machine Vision

How to Deploy OpenCV on Raspberry Pi and Enable Machine Vision

647
0
ПОДЕЛИТЬСЯ

This tutorial demonstrates how to deploy the OpenCV feature on a Raspberry Pi and install development libraries to enable machine vision on your device.
Let’s be friends:
Comment ( 0)
Join the DZone community and get the full member experience.
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.
To start building the OpenCV library from source code on Raspberry Pi, you’ll first need to install development libraries.
Type these commands on the Raspberry Pi terminal:
You also need to install the required matrix, image, and video libraries:
The next step is to download the OpenCV source code via 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
If your Raspbian hasn’t installed it yet, you can install it using pip:
Then, you can configure a virtualenv in your bash profile:
Now, add the following scripts:
Next, save your bash profile file when finished. To create a Python virtual environment, type this command:
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:
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:
The following screenshot shows a sample Python virtual environment form called cv:
Inside the Python virtual terminal, continue installing NumPy as the required library for OpenCV Python. You can install the library using pip:
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:
Next, install the OpenCV library on your internal system from Raspbian OS:
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:
If you’re using Python 3.x (say, Python 3.4), perform the following steps on the terminal:
The installation process is over.
Now, you need to verify whether OpenCV has been installed correctly by checking the OpenCV version:
You should see the OpenCV version on the terminal. A sample program output is shown in the following screenshot:
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:
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:
If successful, you should see a dialog that displays a picture:
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!
Free DZone Refcard
Comment ( 0)
Published at DZone with permission
of
Francesco Azzola
, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.

Continue reading...