Thursday, July 11, 2019

Time to revive this blog. Compiling open cv 4.1 on a raspberry pi

Open CV 4.1 is out. This almost idiot proof tutorial should help you compile OpenCV 4 on a raspberry pi.


Step 1. Lets download the source for OpenCV 

https://opencv.org/releases/
I download version 4.1. If there's a newer version you can use that too


Step 2. Bring the Raspberry Pi up to par.

I mostly followed this blog https://towardsdatascience.com/setting-up-raspberry-pi-for-computer-vision-installing-opencv-e0f973d38819?gi=57ee64265a76

Skip the part about downloading OpenCV and just get the source file from step 1.

Save the following code to a shell script and do chmod +x your_script.sh 

In addition to the dependencies below to view images
sudo apt-get install libgtk2.0-dev pkg-config  



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
sudo apt-get update && sudo apt-get upgrade
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

sudo apt-get -y remove x264 libx264-dev
 
## Install dependencies
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
sudo apt-get -y install git gfortran
sudo apt-get -y install libjpeg8-dev libjasper-dev libpng12-dev
 
sudo apt-get -y install libtiff5-dev
 
sudo apt-get -y install libtiff-dev
 
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get -y install libxine2-dev libv4l-dev
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
cd $cwd
 
sudo apt-get -y install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get -y install libgtk2.0-dev libtbb-dev qt5-default
sudo apt-get -y install libatlas-base-dev
sudo apt-get -y install libmp3lame-dev libtheora-dev
sudo apt-get -y install libvorbis-dev libxvidcore-dev libx264-dev
sudo apt-get -y install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get -y install libavresample-dev
sudo apt-get -y install x264 v4l-utils
 
# Optional dependencies
sudo apt-get -y install libprotobuf-dev protobuf-compiler
sudo apt-get -y install libgoogle-glog-dev libgflags-dev
sudo apt-get -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen


Step 3. Lets build OpenCV using CMAKE

Before we do that make sure you do a pip3 install numpy
and sudo apt-get -y install cmake

Now you should be good to go. Create a build folder inside of open-cv
Inside this folder, make a sh file called build_me.sh 


Put the following code in the file and make it executable (chmod +x build_me.sh). I added something to include GTK, not sure if this will work but it's worth a try.


1
cmake ../ -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D WITH_GTK=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.1.0/modules \ -D BUILD_EXAMPLES=ON ..

Execute this file ./build_me.sh. It should make the installation files required for open-cv

Step 4. Lets make open-cv 

Before running make. Be sure to expand your swap space https://wpitchoune.net/tricks/raspberry_pi3_increase_swap_size.html

Do not run parallel make if you do not have a heat sink. It will over heat and freeze and you'll probably have to start the make over again.

Navigate to the cd build folder and run make

After make has completed, run sudo make install 

This builds all the install files you need

cd into python_loader and do a sudo python3 setup.py install 




Step 5. Lets check if it works

Bring up python in new terminal and do import cv2 if this works then you have open cv successfully installed.




No comments:

Post a Comment