In dit project gaan we OpenCV installeren op de Raspberry Pi. OpenCV is een open source computer vision en machine learning library. OpenCV bevat meer dan 2500 algorithmes voor gezichtsherkenning, object detectie en veel meer.
In deze handleiding laten we drie mogenlijke installaties zien:
- De basisinstallatie
- De handmatige, volledige installatie
- De automatische, volledige installatie
Deze handleiding behandelt:
- Niveau – Gemiddeld/Gevorderd 75%
- Tijdsduur – 2+ uur 75%
- Kosten – €129,64 exc. monitor 55%
Stap 1: Benodigdheden
Product: | Aantal: | Prijs: | |
1 | Raspberry Pi 4B (minimaal 4GB RAM) | 1 | €59,89 |
2 | Raspberry Pi 4B behuizing (actieve koeling aangeraden) | 1 | €16,95 |
3 | Raspberry Pi 4B voeding | 1 | €9,95 |
4 | Micro SD-kaart (minimaal 32GB) | 1 | €15,45 |
5 | Muis & Toetsenbord | 1 | €17,95 |
6 | Micro HDMI -> HDMI kabel | 1 | €9,45 |
7 | Monitor | 1 | |
Totaal | €129,64 |
Stap 2: voorbereiding
Dit project gaat er van uit dat je weet hoe je Raspberry Pi OS (32-bit) moet installeren op jou Raspberry Pi, dat je zonder uitleg jou Raspberry Pi kan aansluiten op een monitor, muis en toetsenbord en dat je zelf een internet verbinding kan configureren. Als je zover bent. Open dan de terminal en kies een van de volgende installaties:
– Basis installatie: Deze installatie is gemaakt voor de beginner. Dit is een vrij simpel proces geschikt voor de meeste gebruikers.
– Handmatige, volledige installatie: Deze installatie is een stuk lastiger, maar bevat elk onderdeel van OpenCV. Dit proces duurt meer dan twee uur en in deze instalatie compileer je OpenCV zelf van de source code. Deze installatie raden wij aan voor de gevorderde gebruiker die graag ziet en begrijpt wat hij/zij aan het doen is.
– Automatische, volledige installatie: Deze installatie is een door ons zelf geautomatiseerde versie van de volledige installatie. Het bevat dezelfde inhoud als de handmatige versie. Ook dit proces duurt meer dan twee uur. Deze keuze raden we aan voor de gevorderde gebruiker die een simpele installatie wil draaien en daarna alle opties ter beschikking wilt hebben.
Stap 3a: basis installatie
Hier installeren we de basis versie van OpenCV. Dit proces zal ergens tussen de 15 en 30 minuten duren. Hieronder zie je de stappen met de code die in de Raspberry Pi OS terminal zullen worden uitgevoerd.
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng-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 libfontconfig1-dev libcairo2-dev
sudo apt-get install libgdk-pixbuf2.0-dev libpango1.0-dev
sudo apt-get install libgtk2.0-dev libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103
sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5
sudo apt-get install python3-dev
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo python3 get-pip.py
sudo rm -rf ~/.cache/pip
sudo pip install virtualenv virtualenvwrapper
sudo nano ~/.bashrc
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc
mkvirtualenv cv -p python3
pip install "picamera[array]"
pip install opencv-contrib-python
Stap 3b: volledige, handmatige installatie
Hier installeren we de volledige versie van OpenCV. Dit proces zal boven de twee uur duren. Hieronder zie je de stappen met de code die in de Raspberry Pi OS terminal zullen worden uitgevoerd.
sudo apt -y update
sudo apt -y upgrade
sudo apt-get -y clean
sudo apt-get -y autoremove
cvVersion="masrer"
rm -rf opencv/build
rm -rf opencv_contrib/build
mkdir installation
mkdir installation/OpenCV-"$cvVersion"
cwd=$(pwd)
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
sudo apt-get -y install python3-dev python3-pip
sudo -H pip3 install -U pip numpy
sudo apt-get -y install python3-testresources
cd $cwd
python3 -m venv OpenCV-"$cvVersion"-py3
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "alias workoncv-$cvVersion=\"source $cwd/OpenCV-$cvVersion-py3/bin/activate\"" >> ~/.bashrc
source "$cwd"/OpenCV-"$cvVersion"-py3/bin/activate
deactivate
sudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=1024/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
pip install numpy dlib
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout $cvVersion
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout $cvVersion
cd ..
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-"$cvVersion" \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=$cwd/OpenCV-$cvVersion-py3/lib/python3.5/site-packages \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
make -j$(nproc)
make install
sudo sed -i 's/CONF_SWAPSIZE=1024/CONF_SWAPSIZE=100/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
echo "sudo modprobe bcm2835-v4l2" >> ~/.profile
Stap 3c: volledige, automatische installatie
Hier installeren we de volledige versie van OpenCV. Dit proces zal boven de twee uur duren. Hieronder zie je de stappen met de code die in de Raspberry Pi OS terminal zullen worden uitgevoerd.
curl https://elektronicavoorjou.nl/wp-content/uploads/2020/06/install-openCV.html?_t=1591714866 | bash