2022年最新真正可以安裝 OpenCV 4 在 Raspberry Pi 4上(一鍵安裝)
講在前面,碰觸 OpenCV 已經快20年了,但每次碰每次都需要編譯,各個平台編譯和支援庫都不一樣,即便我已經做了 N 次,我每次要在不同平台導入,都要花上一整天才能編譯成功,老天爺太折磨人了,還好永遠都有大神幫忙協助,這一次靠著 Q-engineering 協助,終於可以成功在 Raspberry Pi 4 執行 OpenCV 4 了。
Raspberry Pi 4 上確認平台是 32 bit or 64bit
uname -a
先執行 uname -a 確認輸出是否有關鍵字 armv7l , 有的話是 32-bit 版本,如果你有看到關鍵字是 aarch64 則是 64 位元的 OS,要注意 32 bit 和 64 bit 的安裝方法是不同的,以下先示範 32 bit 的安裝方法
OpenCV 版本介紹
現在要安裝的話,推薦都是要安裝 OpenCV 4 以上的版本,在去年 2021 年底推出了 4.5.5 ,完整的支援 DNN module.
OpenCV 安裝前更新 Raspberry Pi
sudo rpi-eeprom-update sudo rpi-eeprom-update -a sudo reboot
加大 Raspberry Pi 的 swapfile
開啟以及編輯 dphys-swapfile
sudo nano /etc/dphys-swapfile
找到 CONF_MAXSWAP ,將值調大超過最少 1024 以上,我自己是調到 4096 後,存檔後離開
CONF_MAXSWAP=4096
重新啟動服務
sudo systemctl restart dphys-swapfile
重頭戲,在 RPI 中一鍵安裝 OpenCV 4
# 檢查記憶體要有6.5GB以上再開始 free -m # 取得 OpenCV-4-5-5.sh 執行檔 wget https://github.com/Qengineering/Install-OpenCV-Raspberry-Pi-32-bits/raw/main/OpenCV-4-5-5.sh sudo chmod 755 ./OpenCV-4-5-5.sh ./OpenCV-4-5-5.sh
接下來只要你的前置作業無誤,等上約1.5小時就可以看到安裝 OpenCV 成功的訊息
不用一鍵安裝的可以按照順序輸入指令
#!/bin/bash set -e echo "Installing OpenCV 4.5.5 on your Raspberry Pi 32-bit OS" echo "It will take minimal 2.0 hour !" cd ~ # install the dependencies sudo apt-get install -y build-essential cmake git unzip pkg-config sudo apt-get install -y libjpeg-dev libtiff-dev libpng-dev sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev sudo apt-get install -y libgtk2.0-dev libcanberra-gtk* libgtk-3-dev sudo apt-get install -y libgstreamer1.0-dev gstreamer1.0-gtk3 sudo apt-get install -y libgstreamer-plugins-base1.0-dev gstreamer1.0-gl sudo apt-get install -y libxvidcore-dev libx264-dev sudo apt-get install -y python3-dev python3-numpy python3-pip sudo apt-get install -y libtbb2 libtbb-dev libdc1394-22-dev sudo apt-get install -y libv4l-dev v4l-utils sudo apt-get install -y libopenblas-dev libatlas-base-dev libblas-dev sudo apt-get install -y liblapack-dev gfortran libhdf5-dev sudo apt-get install -y libprotobuf-dev libgoogle-glog-dev libgflags-dev sudo apt-get install -y protobuf-compiler # download the latest version cd ~ sudo rm -rf opencv* wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.5.zip wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.5.zip # unpack unzip opencv.zip unzip opencv_contrib.zip # some administration to make live easier later on mv opencv-4.5.5 opencv mv opencv_contrib-4.5.5 opencv_contrib # clean up the zip files rm opencv.zip rm opencv_contrib.zip # set install dir cd ~/opencv mkdir build cd build # run cmake cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D ENABLE_NEON=ON \ -D WITH_OPENMP=ON \ -D WITH_OPENCL=OFF \ -D BUILD_TIFF=ON \ -D WITH_FFMPEG=ON \ -D WITH_TBB=ON \ -D BUILD_TBB=ON \ -D WITH_GSTREAMER=ON \ -D BUILD_TESTS=OFF \ -D WITH_EIGEN=OFF \ -D WITH_V4L=ON \ -D WITH_LIBV4L=ON \ -D WITH_VTK=OFF \ -D WITH_QT=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D BUILD_EXAMPLES=OFF .. # run make make -j4 sudo make install sudo ldconfig # cleaning (frees 300 MB) make clean sudo apt-get update echo "Congratulations!" echo "You've successfully installed OpenCV 4.5.5 on your Raspberry Pi 32-bit OS"
檢查 Raspberry Pi 是否有安裝 OpenCV 成功
Python3 import cv2
不藏私,來做人臉辨識吧!!
參考資料
https://qengineering.eu/install-opencv-4.5-on-raspberry-pi-4.html
https://qengineering.eu/install-opencv-4.5-on-raspberry-64-os.html
近期留言