Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views19 pages

Installing OpenCV On Raspberry Pi 3 B - by Mike Alatortsev - Towards Data Scienc

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

!"#$%&&'"()*+,"-.

)/")0%#+1,223)4')5)6
!"#$%&'()*+),$- . /*''*0
123'",4$5%"6 7*0(+5,%8()(%9:"$6:$
;%<"6%+$(5 . &=+%>?@%>ABC

D",)$6 94(+$
I am using the B version, purchased on Amazon, with the following specs:

1.2GHz 64-bit quad-core ARMv8 CPU, 1 GB RAM

802.11n Wireless LAN, 10/100Mbps LAN Speed

Bluetooth 4.1, Bluetooth Low Energy

4 USB ports, 40 GPIO pins, Full HDMI port, Combined 3.5mm audio jack
and composite video

Camera interface (CSI),Display interface (DSI), Micro SD card slot (now


push-pull rather than push-push), VideoCore IV 3D graphics core

There is a newer version (“B+”) there now and it should work the same way,
but some steps (such as compiling OpenCV, which took almost 2 hours on my
device) should be faster.

Also, as expected, compiling OpenCV made the fanless CPU overheat 30 min
into the process — so I had to place a powerful fan next to it, until I receive a
case with proper cooling.

Step 1: make sure you have the latest version of OS

Current OS version is Raspbian Stretch (April 2018). You can either do a clean
E=$6%"6%(== 9"F6%2= 9"F6%"6
install from SD (follow the instructions listed here), or upgrade your existing
version.

To upgrade, open (as sudo) the files /etc/apt/sources.list and


/etc/apt/sources.list.d/raspi.list in your favorite editor, and change all occurences
of your current distro name (e.g. “jessie”) to “stretch”. Then, open a terminal
and run the update:
sudo apt-get update
sudo apt-get -y dist-upgrade

Step 2: configure SSH and utilities

Make sure SSH is enabled. Change the default password, too!

Some of my favorite utilities on Linux are screen (to keep processes running if
your terminal session is dropped) and htop (performance monitoring) — these
may already be pre-installed:
;C

sudo apt-get install screen


sudo apt-get install htop

Step 3: free up 1GB+ by ditching Wolfram and Libreoffice

It’s unlikely that you will need these two packages on your computer vision
box, so:

sudo apt-get purge wolfram-engine


sudo apt-get purge libreoffice*
sudo apt-get clean sudo apt-get autoremove

Step 4: install dependencies

sudo apt-get install build-essential cmake pkg-config


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 libgtk2.0-dev libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran

Step 5: install Python 2.7 & 3

We need this in order to enable Python bindings in Open CV:

In OpenCV, all algorithms are implemented in C++. But these algorithms can
be used from different languages like Python, Java etc. This is made possible
by the bindings generators. These generators create a bridge between C++ and
Python which enables users to call C++ functions from Python. To get a
complete picture of what is happening in background, a good knowledge of
Python/C API is required. A simple example on extending C++ functions to
Python can be found in official Python documentation[1]. So extending all
functions in OpenCV to Python by writing their wrapper functions manually is
a time-consuming task. So OpenCV does it in a more intelligent way. OpenCV
generates these wrapper functions automatically from the C++ headers using
some Python scripts which are located in modules/python/src2

sudo apt-get install python2.7-dev


sudo apt-get install python3-dev

Step 6: get the latest OpenCV source code

I am using version 3.4.1 of OpenCV. You can check the Releases section of the
official site (or Github) to see what the current build is. If your desired version
is different, update the commands and paths below accordingly.
:
Download and unzip OpenCV 3.4.1 and its experimental modules (those are
stored in the opencv_contrib repository):

wget -O opencv.zip
https://github.com/opencv/opencv/archive/3.4.1.zip wget -O
opencv_contrib.zip
https://github.com/opencv/opencv_contrib/archive/3.4.1.zip
unzip opencv.zip unzip opencv_contrib.zip

Step 7: install pip and virtualenv

These are the lowest-level tools for managing Python packages.

Get pip first:

wget -O get-pip.py https://bootstrap.pypa.io/get-pip.py


sudo python get-pip.py
sudo python3 get-pip.py

Source: https://pip.pypa.io/en/stable/installing/

Then, install virtual environments:

sudo pip install virtualenv virtualenvwrapper

Modify your ~/.profile file to include the following lines:

export WORKONHOME=$HOME/.virtualenvs export


VIRTUALENVWRAPPERPYTHON=/usr/bin/python3
:
source /usr/local/bin/virtualenvwrapper.sh

This is what my file looks like:

# ~/.profile: executed by the command interpreter for login


shells. # This file is not read by bash(1), if ~/.bash_profile
or ~/.bash_login # exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the
umask
# for ssh logins, install and configure the libpam-umask
package. #umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
# virtualenv and virtualenvwrapper settings
export WORKON_HOME=$HOME/.virtualenvs export
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 source
/usr/local/bin/virtualenvwrapper.sh

… activate the changes:

source ~/.profile

Step 8: create a virtual environment


:
mkvirtualenv cv -p python3

… or, if you want to use Python 2.7 instead of Python 3:

mkvirtualenv cv -p python2

These are the basic commands for working with virtualenvwarapper:

mkvirtualenv virtualenv_name # create virtualenv workon


virtualenv_name # activate/switch to a virtualenv deactivate
virtualenv_name # deactivate virtualenv

In our case, we can activate a virtualenv called “cv”:

pi@raspberrypi:~ $ workon cv
(cv) pi@raspberrypi:~ $

Step 9: install Numpy, Scipy

Now that you are inside your virtual environment (as evidenced by the “(cv)”
prefix in your terminal window), let’s install some additional packages for data
analysis — numpy and scipy:

sudo pip install numpy scipy


:
Step 10: finally, install OpenCV

Note: this will take a long, long, long time. Took almost 2 hours on my device. Also,
your Raspberry Pi will overheat without proper cooling.

Again, I am using version 3.4.1 of OpenCV. If you aren’t — update your paths
accordingly:

cd ~/opencv-3.4.1/ 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-3.4.1/modules \ -D
BUILD_EXAMPLES=ON ..

Make sure cmake finishes without errors.

Now, get yourself a glass of beer, and prepare for the final step — compiling.
To speed things up, temporarily increase the swap file size in your /etc/dphys-
swapfile by changing CONF_SWAPSIZE from 100 to 1024:

# set size to absolute value, leaving empty (default) then uses


computed value
# you most likely don't want this, unless you have an special
disk situation
#CONF_SWAPSIZE=100
CONF_SWAPSIZE=1024

To avoid rebooting in order for these changes to take effect, simply restart the
swap service:

sudo /etc/init.d/dphys-swapfile restart


:
There’s a detailed guide on how to compile OpenCV with packages here:

https://github.com/opencv/opencv_contrib

What I did (using all 4 CPU cores):

make -j4

Once OpenCV compiles successfully, continue the installation:

sudo make install


sudo ldconfig
sudo apt-get update

… then reboot the system — and you should be good to go!

One more thing:

Test the installation:

$ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04) [GCC 6.3.0
20170124] on linux Type "help", "copyright", "credits" or
"license" for more information.
>>> import cv2
>>> cv2.__version__ '3.4.1'
>>>

If you are getting an error (ImportError: No module named ‘cv2’), the library
:
may be named incorrectly:

(cv) pi@raspberrypi:$ ls -l /usr/local/lib/python3.5/site-


packages/
total 4500 -rw-r--r-- 1 root staff 4604912 Apr 27 14:41
cv2.cpython-35m-arm-linux-gnueabihf.so

Fix it by renaming the library file to “cv2.so”:

cd /usr/local/lib/python3.5/site-packages/
sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so

Originally published at www.alatortsev.com on April 27, 2018.

G(,=3$++H%1" E=$6:- I*<=2)$+%J","*6 !(:4"6$%D$(+6"6F


:
/*''*0

!"#$$%&'()'*#+%',-.$/"$0%1
>;A%/*''*0$+, . K+")$+%L*+%7*0(+5,%8()(%9:"$6:$

32"'5"6F%L2)2+$%)$:4%M"7+$65NO%M&5-$:)"*6%M:(+F*L*6$P+2,Q%8"F")('%<$5"(@%&R@%<(:4"6$%'$(+6"6FQ
9$$%000Q('()*+),$-Q:*<

*/"%'2"/3'*#+%',-.$/"$0%1'.&4'5/6."40'7.$.'89#%&9%

Installing OpenCV 3.4.3 on Raspberry Pi 3 Model B+

!"#$%&'()*+),$- "6 7*0(+5,%8()(%9:"$6:$

!"#$%&&'"()*+,"-.)57875)/")0%#+1,223)4')5)9/:,&)6;
R%=+$-"*2,'H%0+*)$%(%,)$=S3HS,)$=%F2"5$%,4*0"6F%4*0%)*%<(#$%E=$6IJ%TQUQB%+26%*6%(
G(,=3$++H%1"%T%VQ%74()%(+)":'$%F$6$+()$5%(%'*)%*LW

U%<"6%+$(5 . 9$=%;@%>ABC
:
U%<"6%+$(5 . 9$=%;@%>ABC

The Math behind Adam Optimizer

I+",)"(6%D$* "6 7*0(+5,%8()(%9:"$6:$

<=,)9%$=)1,='":)>:%?)*+$'?'@,2
K4H%",%&5(<%)4$%<*,)%=*=2'(+%*=)"<"X$+%"6%8$$=%D$(+6"6FY%D$)Z,%265$+,)(65%")%3H%5"-"6F
"6)*%"),%<()4@%(65%+$:+$()"6F%)4$%('F*+")4<Q

B[%<"6%+$(5 . \(6%TA@%>A>U

BQ?] BB

Python’s Most Powerful Decorator


:
9"(-(,4%^(,"6" "6 7*0(+5,%8()(%9:"$6:$

43$=/"A#)9/#$)4/B,2CD&)E,F/2%$/2
&65%;%0(H,%)*%2,$%")%"6%5()(%,:"$6:$%(65%<(:4"6$%'$(+6"6F

. BB%<"6%+$(5 . /$3%>@%>A>U

>] B;

Object detection without Machine Learning

!"#$%&'()*+),$-

*1G,F$):,$,F$'/")B'$=/D$)9%F='",)H,%2"'"(
:
*1G,F$):,$,F$'/")B'$=/D$)9%F='",)H,%2"'"(
K4$6%*3_$:),%(+$%*L%,"<"'(+%,"X$%(65%,4(=$@%'"#$%)4$,$%+("63*0%5*62),@%)4$H%:(6%3$
5$)$:)$5%2,"6F%,"<='$%)$<='()$%<():4"6F%"6%E=$6IJ`

>%<"6%+$(5 . /$3%;@%>ABC

BBB B

9$$%(''%L+*<%!"#$%&'()*+),$-

9$$%(''%L+*<%7*0(+5,%8()(%9:"$6:$

:%9/33%&4%4'2"/3'*%4#;3

Top Raspberry PI 5 accessories you must have


:
!+7$:4a2H

</+)0%#+1,223)4!)I)%FF,##/2',#)3/D)?D#$)=%J,
b6'*:#%)4$%L2''%=*)$6)"('%*L%H*2+%G(,=3$++H%1"%;%0")4%)4$%'()$,)%(65%<*,)%"66*-()"-$
(::$,,*+"$,Q%/+*<%$64(6:$5%(25"*%,*'2)"*6,%)*%+*32,)W

T%<"6%+$(5 . E:)%>?@%>A>T

;B

Handwritten Text Recognition with Encoder-Decoder net (using TensorFlow)

^(,"694(L"$"

K%":B2'$$,")<,L$)0,F/("'$'/")B'$=)M"F/:,2NE,F/:,2)",$)OD#'"(
<,"#/2P&/BQ
74$,$%5(H,@%&R%(65%8$$=%'$(+6"6F%",%:(=(3'$%*L%5*"6F%(%'*)%*L%)4"6F,Q%L*+%$c(<='$
:4())"6F@%+$(5"6F%"<(F$,@%+$:*F6"X"6F%-*":$,@%$):Q

BB%<"6%+$(5 . 8$:%U@%>A>T

TC B
:
<#0$0

="%4#9$#1%'*/4%-#&>'6?'=)$@/&
>A%,)*+"$, . dAC%,(-$,

=".9$#9.-'A;#4%0'$/'*.9@#&%'<%."&#&>
BA%,)*+"$, . BA[A%,(-$,

B.$;".-'<.&>;.>%'="/9%00#&>
B>AB%,)*+"$, . [?B%,(-$,

5@%'B%6'C@.$(/$0D'C@.$A=5E'F."4E'.&4'F%)/&4
B>%,)*+"$, . TA[%,(-$,

Arduino vs. Raspberry Pi: Unraveling the Tech Showdown

/2)2+$%/(6()":

>2:D'"/)J#7)0%#+1,223)4'R)S"2%J,&'"()$=,)<,F=)T=/B:/B"
K4$6%")%:*<$,%)*%)4$%0*+'5%*L%8R^%$'$:)+*6":,%(65%)"6#$+"6F@%)0*%6(<$,%,)(65%*2)
:
=+*<"6$6)'H`%&+52"6*%(65%G(,=3$++H%1"Q%74$,$%,<(''W
T%<"6%+$(5 . E:)%BA@%>A>T

B>B

Cool Raspberry Pi Projects

\*,42(%8$,4(+6(",

-//&)0%#+1,223)4')42/G,F$#
RL%H*2%_2,)%F*)%(%+(,=3$++H%="%(65%(+$%0*65$+"6F%04()%)4$%L26%(65%:**'%)4"6F,%H*2%:(6%5*
0")4%")%)4$6%)4",%(+)":'$%",%L*+%H*2Q%74$,$%(+$W

. ;%<"6%+$(5 . \(6%>[@%>A>U

TB

Contour Detection in OpenCV: A Comprehensive Guide


:
J"="6

-/"$/D2)E,$,F$'/")'")*+,"-.R)>)-/?+2,=,"#'J,)UD':,
I*6)*2+"6F%",%(%L265(<$6)('%)$:46"e2$%"6%:*<=2)$+%-","*6%(65%"<(F$%=+*:$,,"6F@
='(H"6F%(%:+2:"('%+*'$%"6%"5$6)"LH"6F%(65%$c)+(:)"6F%*3_$:)W

U%<"6%+$(5 . \(6%TB@%>A>U

Hand Detection in Python Using OpenCV and MediaPipe

D(#,4")4(%J"<2)4
:
D(#,4")4(%J"<2)4

K%":)E,$,F$'/")'")43$=/")S#'"()*+,"-.)%":)9,:'%4'+,
R6)+*52:)"*6

T%<"6%+$(5 . E:)%>>@%>A>T

BA?

9$$%<*+$%+$:*<<$65()"*6,
:

You might also like