Installing OpenCV On Raspberry Pi 3 B - by Mike Alatortsev - Towards Data Scienc
Installing OpenCV On Raspberry Pi 3 B - by Mike Alatortsev - Towards Data Scienc
Installing OpenCV On Raspberry Pi 3 B - by Mike Alatortsev - Towards Data Scienc
)/")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:
4 USB ports, 40 GPIO pins, Full HDMI port, Combined 3.5mm audio jack
and composite video
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.
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.
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
It’s unlikely that you will need these two packages on your computer vision
box, so:
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
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
Source: https://pip.pypa.io/en/stable/installing/
source ~/.profile
mkvirtualenv cv -p python2
pi@raspberrypi:~ $ workon cv
(cv) pi@raspberrypi:~ $
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:
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:
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:
To avoid rebooting in order for these changes to take effect, simply restart the
swap service:
https://github.com/opencv/opencv_contrib
make -j4
$ 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:
cd /usr/local/lib/python3.5/site-packages/
sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so
!"#$$%&'()'*#+%',-.$/"$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%
!"#$%&&'"()*+,"-.)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%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