This article mainly includes the following content:
-
Install Raspbian System
-
Connect and Set Up Network
-
Install Chinese Support
-
Control Raspberry Pi from Computer
-
Log in to Raspberry Pi command line interface via SSH
-
Install VNC to access Raspberry Pi graphical desktop
-
Connect Raspberry Pi via serial port
-
Install OpenCV and Related Development Packages
-
One-click Backup of Raspberry Pi SD Card
1 Install Raspbian System
The first thing to do after getting the Raspberry Pi is to install the system. The required configurations for installing the Raspberry Pi system are:
-
1 USB power adapter with output of 5V and >700mA (usually choose 1A or 2A)
-
1 micro USB cable (Android cable)
-
1 SD card with system image and NOOBS copied (>=8G)
-
1 monitor that can be used with Raspberry Pi and related video adapter cable (if needed)
-
1 USB wireless network card or a network cable + network
For machine vision applications, choose a card larger than 8G; mine is 32G, and after installing all content in this article, about 6G of space is used. If you are worried about reinstalling all the software after struggling to install everything, you need about 7G of space for backup.
1.1Download System and Installation Tools
Download Raspberry Pi operating system Raspbian from the following address:
-
https://www.raspberrypi.org/downloads/
Download the Raspbian system and installation tool NOOBS (New Out Of Box System).
1.2 Install System
Copy the extracted image file xxx-raspbian-jessie.img into the root directory of the SD card.
Copy all files from the extracted NOOBS_v1_9_1 folder into the root directory of the SD card, making sure to enter the NOOBS_v1_9_1 folder to copy all files inside, rather than copying the folder directly.
After copying, the root directory of the SD card should look like this:
Insert the SD card into the Raspberry Pi. Find a monitor with an HDMI interface; if you don’t have one, buy an HDMI to VGA adapter to use a common VGA monitor as the Raspberry Pi screen. Connect the USB mouse and keyboard, and everything is ready. After connecting the power supply, the Raspberry Pi will automatically enter the installation program. Wait a moment, and the system will be installed, logging into the graphical interface by default.
2 Install and Set Up Network
2.1 Connect to Network
I use a USB wireless wifi card; after plugging it in, it automatically configures and finds the wifi hotspot to input the password to connect to the internet. We can use the following command in the Raspberry Pi LX terminal command line to check the network connection status:
-
ifconfig
The part of eth0 is the wired network connection status, and the part of wlan0 is the wireless network connection status. inet addr: xxx.xxx.xxx.xxx is the IP address of the Raspberry Pi.
2.2 Set Static IP
By default, the Raspberry Pi’s IP is dynamically configured by DHCP. For convenience in using VNC login later, the usual practice is to change the IP to a static address. Modify the /etc/network/interfaces configuration file:
-
sudo nano /etc/network/interfaces
-
sudo is a Linux command that means to run as a superuser.
-
nano is a text editor that comes with the Raspberry Pi, and many configuration files can be opened and modified with it.
Modify the corresponding section of wlan0 in the opened configuration file as follows:
-
allow-hotplug wlan0
-
iface wlan0 inet static
-
wpa-conf/etc/wpa_supplicant/wpa_spplicant.conf
-
address 192.168.0.112
-
netmask 255.255.255.0
-
gateway 192.168.0.1
-
IP is set to static: static
-
This fixes the Raspberry Pi’s wireless network IP address to: 192.168.0.112
-
Subnet mask is set to: 255.255.255.0
-
Gateway is the address of the home router: 192.168.0.1
The modified configuration file looks similar to the image below:
Press Ctrl+O to save, Ctrl+X to exit the editor. Then restart the Raspberry Pi to make the configuration effective:
-
sudo reboot
There are many methods for network configuration; I won’t list them all here, but you can refer to other related articles.
3 Install Chinese Support
3.1 Install Chinese Fonts
Enter the Raspberry Pi LX terminal and type the following command:
-
sudo apt-get install ttf-wqy-zenhei
This installs the open-source Chinese font WenQuanYi, and now Chinese can be displayed normally.
3.2 Install Chinese Input Method
Install the Chinese input method SCIM (Smart Common Input Method) for Linux by entering the command line:
-
sudo apt-get install scim-pinyin
After installing the pinyin input method, you can directly enter the command line to inputscim to activate it; it will start automatically the next time. The shortcut key isCtrl+Space. Or you can directly click the input method icon in the upper right corner of the screen to select.
3.3 Configure Chinese Operating System
Run the command:
-
sudo raspi-config
to perform system settings. Select Internationalisation Options–>Change Locale–>zh_CN.UTF-8–>OK, then restart the system to set it to the Chinese operating system.
4 Control Raspberry Pi from Computer
4.1 SSH
Secure Shell (SSH) is a feature of Linux that allows you to quickly open a terminal session of Raspberry Pi from your host computer. To use SSH, you first need to enable it. The easiest way is to use Raspi Config; the current Raspbian system has SSH function enabled by default.
With a network connection, SSH is a common way to remotely connect to a computer; you can execute any command that can be run on the Raspberry Pi in the Secure Shell. Meanwhile, the communication is encrypted.
The downside of this method is that it is command-line-based rather than graphical interface-based. If you want to remotely access the graphical desktop environment, please refer to the next section about using VNC.
Windows users need to install Putty to establish an SSH session.
Putty download address:
-
http://pan.baidu.com/share/link?shareid=2217335081&uk=605377859
After extracting, run putty.exe
In the basic settings of putty, select SSH for “Connection Type” and fill in the Raspberry Pi’s IP address for the Host Name or IP Address, then click “Open”. A command line prompt will pop up asking for the login username, enter: pi, and after a moment, it will prompt for the password, enter raspberry to achieve command-line remote control of the Raspberry Pi.
4.2 VNC
Using SSH only allows command-line control of the Raspberry Pi, while VNC allows remote login to the Raspberry Pi graphical interface system. VNC includes a server (VNC server) and a client (VNC viewer).
(1) Install Server: tightvncserver
First, install the VNC server on the Raspberry Pi by installing tightvncserver through the LX terminal command line:
-
sudo apt-get install tightvncserver
(2) Start the VNC Server on Raspberry Pi: tightvncserver
After installing the tightvncserver server, start the server via command line:
-
sudo tightvncserver
After starting the server, you need to enter the password twice for the first time and confirm whether to use view-only mode for login, which can be chosen as no.
After starting, you can see that the server has started: New ‘X’ desktop is raspberrypi: 1, 1 is the port number when logging in to the computer. You can also start multiple servers at the same time and distinguish them by different numbers.
(3) Get Raspberry Pi’s IP Address
Get the Raspberry Pi’s IP address by entering the following command in the LX terminal:
-
ifconfig
The result will show 192.168.0.112, which is the static IP we set earlier. If you haven’t set a static IP, you need to obtain the Raspberry Pi’s IP for client login through this step.
(4) Install Client on Computer: VNC Viewer
Install a VNC client on the computer; there are many software options available, and RealVNC is recommended for stable connections. Download address:
-
http://www.realvnc.com
After installing VNC Viewer, open it, and the following dialog will pop up:
In the VNC Server box, enter 192.168.0.112:1 (1 is the port number of the VNC server), click connect, and then enter the login password set when starting the VNC server for the first time to complete the login.
(5) Set Raspberry Pi to Start VNC Server on Boot
The following is a simple and easy way to set the VNC server to start on boot. Create a startup file:
-
cd /home/pi
-
cd .config
-
mkdir autostart
-
cd autostart
-
nano tightvnc.desktop
In the opened file, enter the following code:
-
[Desktop Entry]
-
Type=Application
-
Name=tightVNC
-
Exec=vncserver :1
-
StartupNotify=false
Restart the Raspberry Pi
-
sudo reboot
Now every time you power on, you can use the Raspberry Pi’s static IP and VNC server port 1 to log in remotely to the graphical interface system without needing to connect an external monitor.
4.3 Use Serial Port to Connect to Raspberry Pi
If there is no network environment and you want to control the Raspberry Pi with a computer, you can do so through a serial cable.
Use a USB to TTL cable to connect to the Raspberry Pi. The connection is shown in the diagram below: the 5th to 3rd pin from the bottom right GPIO interface connects to the green, white, and black USB wires respectively, with the red wire being the power supply line. Theoretically, it can connect to the first pin from the bottom to power the Raspberry Pi, but I tested it and found that the power is insufficient to start the Raspberry Pi.
Open putty, select “Serial” to connect, and the port is the port number assigned to the USB to TTL cable by the computer. You can check the serial port number by right-clicking on the computer and selecting Device Manager–>Ports. My machine is COM5, set the baud rate to 115200, and click the “Open” button in the putty dialog to connect.
In the popped command line connection window, enter pi, then enter the password: raspberry, and you can connect to the Raspberry Pi via the serial port.
5 Install OpenCV and Related Development Packages
Installing OpenCV took a lot of effort. There are many guides online, and after trying to install successfully, the Python2 in the Raspberry Pi could not import the cv2 library. I also tried configuring environment variables and other methods but still couldn’t call it. Later, I found that installing the packages one by one according to the book’s method could work with Python. Here I provide three installation methods: the official installation guide, online tutorial links, and my own successful installation method.
5.1 OpenCV Official Linux System Installation Guide
Official guide address:
-
http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html
(1) Required Packages
-
GCC 4.4.x or later
-
CMake 2.6 or higher
-
Git
-
GTK+2.x or higher, including headers (libgtk2.0-dev)
-
pkg-config
-
Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
-
ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
-
[optional] libtbb2 libtbb-dev
-
[optional] libdc1394 2.x
-
[optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
(2) Installation Method
Install Compiler:
-
sudo apt-get install build-essential
Install Support Packages:
-
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
Install Optional Packages:
-
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
(3) Get OpenCV Source Code
OpenCV download address:
-
http://opencv.org/downloads.html
-
https://github.com/Itseez/opencv/archive/2.4.13.zip
-
https://sourceforge.net/projects/opencvlibrary/
Method 1:
-
Download the OpenCV installation package (for Linux) from the above addresses. Here, I downloaded opencv-2.4.13.zip and copied it to the /home/pi/ folder on the Raspberry Pi via USB, then extracted it to generate an opencv-2.4.13 folder.
Method 2:
-
You can download the installation package directly on the Raspberry Pi via command line and then extract it.
-
wget -O opencv-2.4.13.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.13/opencv-2.4.13.zip/download
-
unzip opencv-2.4.13.zip
In short, obtain the latest stable OpenCV installation file and extract it. I downloaded opencv-2.4.13.zip because of Python2.
(4) Use CMake to Compile OpenCV Source Files
Create a temporary folder to store Makefiles, project files, target files, and output binary files. Here,
-
cd ~/opencv
-
mkdir release
-
cd release
Enter the command line in the cd opencv-2.4.13/release folder and input:
-
cmake [<some optional parameters>] <path to the OpenCV source directory>
For example:
-
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
Enter the command line in the opencv-2.4.13/release folder and input:
-
make
-
sudo make install
This completes the compilation and installation; this step takes about 6 hours.
Update the search dynamic link library:
-
sudo ldconfig
(5) Test Installation
-
Check the installation version:
-
pkg-config –modversion opencv
It shows the version of the installed OpenCV is 2.4.13.
-
Run the test to confirm whether the suite can run:
Enter the command line in the /opencv-2.4.13/release/bin directory:
-
cd ~/opencv-2.4.13/release/bin
-
./opencv_test_core
It shows the result of the test installation:
Reference articles:
-
http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html
-
http://www.raspberries.tw/post/49782936305/raspberrypi-build-opencv-with-xwindow#_=_
-
http://blog.csdn.net/xukai871105/article/details/40988101
-
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=80601
5.2 Install OpenCV via Source Package
Through the method in the previous section, the OpenCV installed cannot be called by Python. I defined the environment variable according to the methods found online, but it still didn’t work. So I found a method introduced in a foreign book, which is relatively simple, and after installing it, it can be called normally by Python. Here I provide it for reference:
(1) Update Advanced Package Tool (apt)
apt is a tool used to install and uninstall software in the Debian system, and we use it to update the software on the Pi:
-
sudo apt-get update
(2) Update Installed Software
-
sudo apt-get upgrade
(3) Update Firmware
-
sudo rpi-update
(4) Install Necessary Packages and Dependencies for OpenCV
-
sudo apt-get install <package-name>
<package-name> is shown in the table below:
libopencv-dev |
libpng3 |
libdc1394-22-dev |
build-essential |
libpnglite-dev |
libdc1394-22 |
libavformat-dev |
zlib1g-dbg |
libdc1394-utils |
x264 |
zlib1g |
libv4l-0 |
v4l-utils |
zlib1g-dev |
libv4l-dev |
ffmpeg |
pngtools |
libpython2.7 |
libcv2.4 |
libtiff4-dev |
python-dev |
libcvaux2.3 |
libtiff4 |
python2.7-dev |
libhighgui2.4 |
libtiffxx0c2 |
libgtk2.0-dev |
python-opencv |
libtiff-tools |
libpngwriter0-dev |
opencv-doc |
libjpeg8 |
libpngwriter0c2 |
libcv-dev |
libjpeg8-dev |
libswscale-dev |
libcvaux-dev |
libjpeg8-dbg |
libjpeg-dev |
libhighgui-dev |
libavcodec-dev |
libwebp-dev |
python-numpy |
libavcodec53 |
libpng-dev |
python-scipy |
libavformat53 |
libtiff5-dev |
python-matplotlib |
libgstreamer0.10-0-dbg |
libjasper-dev |
python-pandas |
libgstreamer0.10-0 |
libopenexr-dev |
python-nose |
libgstreamer0.10-dev |
libgdal-dev |
libeigen3-dev |
libxine1-ffmpeg |
python-tk |
libgtkglext1-dev |
libxine-dev |
python3-dev |
libpng12-0 |
libxine1-bin |
python3-tk |
libpng12-dev |
libunicap2 |
python3-numpy |
libpng++-dev |
libunicap2-dev |
Yes, that’s a lot -_-
(5) Install OpenCV
After installing all the packages above, install OpenCV:
-
sudo apt-get install python-opencv
This is the simplest way to install OpenCV for Python, but the downside is that it may not be the latest version of OpenCV. When writing this article, the version installed by this method was 2.4.9.
Now you can finally use OpenCV in Python on the Raspberry Pi. Open Python to test the installation version:
-
import cv2
-
cv2.__version__
-
‘2.4.9.1’
6 Backup SD Card
If you have completed the Pi’s machine vision programming environment setup after half a day’s effort and are worried about problems that require reinstallation, you can choose to use the following method to back up the SD card. It only backs up the space used on the SD card, not the entire SD card.
This method refers to the following articles:
-
http://www.fendou.info/raspberry-pi-sd-card-backup-and-recovery/comment-page-1/
-
http://www.littledai.com/20140527/000753/3196
6.1 Install Required Software
-
dosfstools: FAT32 partition formatting tool
-
dump: backup & restore tool
-
parted & kpartx: virtual disk tool
-
sudo apt-get install dosfstools
-
sudo apt-get install dump
-
sudo apt-get install parted
-
sudo apt-get install kpartx
6.2 Generate New img File
First, use thedf -h command to check the actual used space on the Raspberry Pi and then decide the size of the generated file. Just look at the space occupied by root; here it has used 6.7G.
The following command generates an image file with a block size of 1MB and a total size of 7500MB:
-
sudo dd if=/dev/zero of=raspberrypi.img bs=1MB count=7500
6.3 Split Virtual Disk
Useparted to split the generated virtual disk into two partitions:
-
The first partition usesFAT32 format, starting fromsector 8192 tosector 12879, about62.9MB
-
The second partition usesEXT4, starting fromsector 122880 to the end.
-
sudo parted raspberrypi.img –script — mklabel msdos
-
sudo parted raspberrypi.img –script — mkpart primary fat32 8192s 122879s
-
sudo parted raspberrypi.img –script — mkpart primary ext4 122880s -1
6.4 Mount Virtual Disk and Format
loopdevice: Executelosetup to connect the img file to the loop device
device: After executingkpartx, the identifier of the virtual disk.
partBoot: Formed by combiningdevice andp1, the FAT32 virtual disk partition.
partRoot: Formed by combiningdevice andp2, the EXT4 virtual disk partition.
-
loopdevice=`sudo losetup -f –show raspberrypi.img`
-
device=`sudo kpartx -va $loopdevice | sed -E ‘s/.*(loop[0-9])p.*/
/g’ | head -1` -
device=”/dev/mapper/${device}”
-
partBoot=”${device}p1″
-
partRoot=”${device}p2″
Format the virtual disk partitions:
-
sudo mkfs.vfat $partBoot
-
sudo mkfs.ext4 $partRoot
6.5 Start Backup
After establishing the FAT32 virtual disk partition, directly use thecp command to back up files:
-
sudo mount -t vfat $partBoot /media
-
sudo cp -rfp /boot/* /media/
-
sudo umount /media
Usedump & restore to back up the EXT4 virtual disk partition:
-
sudo mount -t ext4 $partRoot /media/
-
cd /media
-
sudo dump -0uaf – / | sudo restore -rf –
-
cd
-
sudo umount /media
6.5 Unmount Virtual Disk
-
sudo kpartx -d $loopdevice
-
sudo losetup -d $loopdevice
After the above six steps, all data files in the Raspberry Pi will be backed up toraspberrypi.img. You can restore the image to the SD card using Win32DiskImager in Windows, or restore it to the SD card using dd in Linux, and it does not require the restored SD card size to be the same as the original, making it easy to replace the SD card. A backup file named raspberrypi.img will be generated in the /home/pi directory:
6.6 One-click Backup Setup
Create a script file, save it in any location (such as home/pi/backup), and give it any filename (such as backup.sh):
-
cd backup
-
sudo nano backup.sh
Copy the following content into the opened file:
-
#!/bin/sh
-
sudo dd if=/dev/zero of=raspberrypi.img bs=1MB count=7500
-
sudo parted raspberrypi.img –script — mklabel msdos
-
sudo parted raspberrypi.img –script — mkpart primary fat32 8192s 122879s
-
sudo parted raspberrypi.img –script — mkpart primary ext4 122880s -1
-
-
loopdevice=`sudo losetup -f –show raspberrypi.img`
-
device=`sudo kpartx -va $loopdevice | sed -E ‘s/.*(loop[0-9])p.*/
/g’ | head -1` -
device=”/dev/mapper/${device}”
-
partBoot=”${device}p1″
-
partRoot=”${device}p2″
-
sudo mkfs.vfat $partBoot
-
sudo mkfs.ext4 $partRoot
-
sudo mount -t vfat $partBoot /media
-
sudo cp -rfp /boot/* /media/
-
sudo umount /media
-
sudo mount -t ext4 $partRoot /media/
-
cd /media
-
sudo dump -0uaf – / | sudo restore -rf –
-
cd
-
sudo umount /media
-
sudo kpartx -d $loopdevice
-
sudo losetup -d $loopdevice
Press Ctrl+O to save the file and Ctrl+X to exit. When you need to back up, you can execute:
-
sudo sh backup.sh
After execution, you can see the generated backup file:
Now you can freely tinker!
The following image shows an example of using VNC to log into the Raspberry Pi and using Geany IDE to write Python machine vision programs:
Unfortunately, when using RealVNC to remotely debug machine vision programs, RealVNC does not support OpenGL, so the imshow() function in the cv2 module cannot be called to display images. In the example above, I changed it to use functions in the matplotlib library to display.
Reprinted from, click to read the original text: http://blog.csdn.net/iracer/article/details/51620051
Disclaimer: This article is a network reprint, and the copyright belongs to the original author. If there are copyright issues, please contact for deletion!
Limited time download of resources::Follow us on
Leave a Comment
Your email address will not be published. Required fields are marked *