Color

日々の備忘録

Raspberry Pi 5 に OpenCV 4.9.0 インストール

2024年04月03日 07時42分19秒 | Raspberry ...
◆Raspberry Pi に OpenCV をインストールする手順

 

1.準備
RP : Raspberry Pi 5 8GB
SD : SDSQXNE-032G-GN6MA [SANDISK]
AD : AD-PD6001A 5V/3A [Akizuki]
Case : Rspberry Pi 5 Case Red/White [SC1159]
Fan : Active Cooler [SC1148]
Camera : Raspberry Pi HQ Camera [SC0818]
Cable : RPI 5 Camera Cable 500mm [SC1130]
Lens : CGL 16mm Tele-photo Lens [SC0123]
PC : Windows10 Pro 64bit Version 22H2

 

2.OSバージョン

$ uname -a

    Linux raspberrypi 6.6.20+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.6.20-1+rpt1 (2024-03-07) aarch64 GNU/Linux
    

$ lsb_release -a

    No LSB modules are available.
    Distributor ID: Debian
    Description: Debian GNU/Linux 12 (bookworm)
    Release: 12
    Codename: bookworm
    
3.インストール
# 境界を拡大する ( CONF_MAXSWAP=4096 )
$ sudo nano /sbin/dphys-swapfile
# 必要なメモリ サイズを指定します ( CONF_SWAPSIZE=4096 )
$ sudo nano /etc/dphys-swapfile
# その後再起動します
$ sudo reboot
# メモリ確認
$ free -m
                   total        used        free      shared  buff/cache   available
    Mem:            8052         593        6846          36         745        7459
    Swap:           4095           0        4095
    
#openCV sorce install
$ wget https://github.com/Qengineering/Install-OpenCV-Raspberry-Pi-64-bits/raw/main/OpenCV-4-9-0.sh
$ sudo chmod 755 ./OpenCV-4-9-0.sh
$ ./OpenCV-4-9-0.sh
# only install if you want Qt5 to beautify your GUI
# Bookworm

$ sudo apt-get install qtbase5-dev
$ cd ~
$ git clone --depth=1 https://github.com/opencv/opencv.git
$ git clone --depth=1 https://github.com/opencv/opencv_contrib.git
$ cd ~/opencv
$ mkdir build
$ cd build
$ 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 WITH_PROTOBUF=ON \
-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 ..
$ make -j4
$ sudo make install
$ sudo ldconfig
# cleaning (frees 300 KB)
$ make clean
$ sudo apt-get update
$ sudo apt-get upgrade
$ python
    >>> import cv2
    >>> print( cv2.getBuildInformation() )
    General configuration for OpenCV 4.9.0-dev =====================================
      Version control:               52f3f5a
    
      Extra modules:
        Location (extra):            /home/pi/opencv_contrib/modules
        Version control (extra):     9373b72
    
      Platform:
        Timestamp:                   2024-03-25T04:33:16Z
        Host:                        Linux 6.6.20+rpt-rpi-2712 aarch64
        CMake:                       3.25.1
        CMake generator:             Unix Makefiles
        CMake build tool:            /usr/bin/gmake
        Configuration:               RELEASE
    
      CPU/HW features:
        Baseline:                    NEON FP16
          required:                  NEON
        Dispatched code generation:  NEON_DOTPROD NEON_FP16 NEON_BF16
          requested:                 NEON_FP16 NEON_BF16 NEON_DOTPROD
          NEON_DOTPROD (1 files):    + NEON_DOTPROD
          NEON_FP16 (2 files):       + NEON_FP16
          NEON_BF16 (0 files):       + NEON_BF16
    
      C/C++:
        Built as dynamic libs?:      YES
        C++ standard:                11
        C++ Compiler:                /usr/bin/c++  (ver 12.2.0)
        C++ flags (Release):         -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -O3 -DNDEBUG  -DNDEBUG
        C++ flags (Debug):           -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -g  -O0 -DDEBUG -D_DEBUG
        C Compiler:                  /usr/bin/cc
        C flags (Release):           -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fopenmp -O3 -DNDEBUG  -DNDEBUG
        C flags (Debug):             -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections    -fvisibility=hidden -fopenmp -g  -O0 -DDEBUG -D_DEBUG
        Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
        Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined
        ccache:                      NO
        Precompiled headers:         NO
        Extra dependencies:          dl m pthread rt
        3rdparty dependencies:
    
      OpenCV modules:
        To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape signal stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
        Disabled:                    world
        Disabled by dependency:      -
        Unavailable:                 alphamat cannops cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv java julia matlab ovis python2 sfm viz
        Applications:                perf_tests apps
        Documentation:               NO
        Non-free algorithms:         YES
    
      GUI:                           GTK3
        GTK+:                        YES (ver 3.24.38)
          GThread :                  YES (ver 2.74.6)
          GtkGlExt:                  NO
    
      Media I/O:
        ZLib:                        /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.13)
        JPEG:                        /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 62)
        WEBP:                        /usr/lib/aarch64-linux-gnu/libwebp.so (ver encoder: 0x020f)
        PNG:                         /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.39)
        TIFF:                        build (ver 42 - )
        JPEG 2000:                   build (ver 2.5.0)
        OpenEXR:                     build (ver 2.3.0)
        HDR:                         YES
        SUNRASTER:                   YES
        PXM:                         YES
        PFM:                         YES
    
      Video I/O:
        DC1394:                      NO
        FFMPEG:                      YES
          avcodec:                   YES (59.37.100)
          avformat:                  YES (59.27.100)
          avutil:                    YES (57.28.100)
          swscale:                   YES (6.7.100)
          avresample:                NO
        GStreamer:                   YES (1.22.0)
        v4l/v4l2:                    YES (linux/videodev2.h)
    
      Parallel framework:            TBB (ver 2021.11 interface 12110)
    
      Trace:                         YES (with Intel ITT)
    
      Other third-party libraries:
        Lapack:                      NO
        Custom HAL:                  YES (carotene (ver 0.0.1, Auto detected))
        Protobuf:                    build (3.19.1)
        Flatbuffers:                 builtin/3rdparty (23.5.9)
    
      Python 3:
        Interpreter:                 /usr/bin/python3 (ver 3.11.2)
        Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.11.so (ver 3.11.2)
        Limited API:                 NO
        numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.24.2)
        install path:                /usr/lib/python3/dist-packages/cv2/python-3.11
    
      Python (for build):            /usr/bin/python3
    
      Java:
        ant:                         NO
        Java:                        NO
        JNI:                         NO
        Java wrappers:               NO
        Java tests:                  NO
    
      Install to:                    /usr/local
    -----------------------------------------------------------------
    
4.Pythonサンプルプログラム
    import cv2
    from picamera2 import Picamera2
    
    camera = Picamera2()
    camera.configure(camera.create_preview_configuration(main={
        "format": 'XRGB8888',
        "size": (640, 480)
    }))
    camera.start()
    #camera.set_controls({'AfMode': controls.AfModeEnum.Continuous})
    
    image = camera.capture_array()
    
    channels = 1 if len(image.shape) == 2 else image.shape[2]
    if channels == 1:
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
    if channels == 4:
        image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
    
    cv2.imwrite('test.jpg', image)
    
5.サンプル実行

─以上─