# Raspberry Pi 4のセットアップ(Raspberry Pi OS編) ## はじめに しばらく、Jetsonシリーズとそろえて、Ubuntuで検討してきたが、気分一新で、Raspberry Pi OSの環境で検討しようと思い立った。 イメージセンサ系の検討をしたいわけだが、libcameraという新しい枠組みもあるようで、それに期待したい、というところ。 ## インストール あらためて、64GB microSDをいつものように、SD Card Formatterにてフォーマットした上で、Imagerで、今回は、Raspberry Pi OSを選択してインストールしてみる。 やはり、64-bit版を使いたい、そして、画像も扱いたいので、あえて、Desktop版を、ということで、 * Raspberry Pi OS (64-bit) - A port of Debian Bullseye with the Raspberry Pi Desktop を選択して、SDカードに書き込んで、書き込み終えたら、ラズパイ4に挿入して起動。また、事前に、Raspberry Pi Camera Rev 1.3 (OV5647)をCAMERA スロット(J3)に装着している。 起動すると、あっさりと、 ``` $ libcamera-hello -t 0 ``` で、カメラでライブビューできた。 ## カーネルのビルド Raspberry Pi Documentation→The Linux kernel内の以下のページに従って作業する。 [Building the Kernel](https://www.raspberrypi.com/documentation/computers/linux_kernel.html#building) ``` sudo apt install git bc bison flex libssl-dev make git clone --depth=1 https://github.com/raspberrypi/linux cd linux export KERNEL=kernel8 make bcm2711_defconfig vi .config // CONFIG_LOCALVERSION="-v8-custom" ``` ``` make -j4 Image.gz modules dtbs sudo make modules_install sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/ sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/overlays/ sudo cp arch/arm64/boot/dts/overlays/README /boot/overlays/ sudo cp arch/arm64/boot/Image.gz /boot/$KERNEL.img ``` ## libcamera関連 [libcamera and libcamera-apps](https://www.raspberrypi.com/documentation/accessories/camera.html#libcamera-and-libcamera-apps) ``` git clone git://linuxtv.org/libcamera.git ``` にて取得。 ## シリアルコンソール ヘッダの * Pin6をGNDに * Pin8をTxDとして * Pin10をRxDとして UART-USBコンバータに接続して、Windows PCのTeraTermにて確認。無事、シリアルコンソールが確認できた。 ただし、起動時の詳細なログはなぜかまだ取れていない。WindowsのTeraTermは起動時は取りにくいので、Ubuntu PCのminicomでも試す。 ## OpenCVのインストール [【Python】Raspberry Pi OSにOpenCVをインストール](https://invisiblepotato.com/raspberrypi09/) を参考に、OpenCVをインストール。 ``` $ pip3 install opencv-python Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting opencv-python Downloading opencv_python-4.5.5.64-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (39.2 MB) |████████████████████████████████| 39.2 MB 71 kB/s Requirement already satisfied: numpy>=1.19.3 in /usr/lib/python3/dist-packages (from opencv-python) (1.19.5) Installing collected packages: opencv-python Successfully installed opencv-python-4.5.5.64 $ pip3 install numpy --upgrade Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (1.19.5) Collecting numpy Downloading numpy-1.22.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (13.4 MB) |████████████████████████████████| 13.4 MB 3.8 MB/s Installing collected packages: numpy WARNING: The scripts f2py, f2py3 and f2py3.9 are installed in '/home/sirius/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed numpy-1.22.4 $ sudo apt install libatlas3-base パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています... 完了 状態情報を読み取っています... 完了 以下のパッケージが自動でインストールされましたが、もう必要とされていません: libfuse2 これを削除するには 'sudo apt autoremove' を利用してください。 以下のパッケージが新たにインストールされます: libatlas3-base アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 1 個。 2,278 kB のアーカイブを取得する必要があります。 この操作後に追加で 9,160 kB のディスク容量が消費されます。 取得:1 http://deb.debian.org/debian bullseye/main arm64 libatlas3-base arm64 3.10.3-10 [2,278 kB] 2,278 kB を 0秒 で取得しました (4,986 kB/s) 以前に未選択のパッケージ libatlas3-base:arm64 を選択しています。 (データベースを読み込んでいます ... 現在 103232 個のファイルとディレクトリがインストールされています。) .../libatlas3-base_3.10.3-10_arm64.deb を展開する準備をしています ... libatlas3-base:arm64 (3.10.3-10) を展開しています... libatlas3-base:arm64 (3.10.3-10) を設定しています ... update-alternatives: /usr/lib/aarch64-linux-gnu/libblas.so.3 (libblas.so.3-aarch64-linux-gnu) を提供するために自動モードで /usr/lib/aarch64-linux-gnu/atlas/libblas.so.3 を使います update-alternatives: /usr/lib/aarch64-linux-gnu/liblapack.so.3 (liblapack.so.3-aarch64-linux-gnu) を提供するために自動モードで /usr/lib/aarch64-linux-gnu/atlas/liblapack.so.3 を使います libc-bin (2.31-13+rpt2+rpi1+deb11u2) のトリガを処理しています ... ``` 上記のやり方でインストールされたOpenCVのバージョンを確認してみた。 ``` $ python3 -c 'import cv2; print(cv2.getBuildInformation())' General configuration for OpenCV 4.5.5 ===================================== Version control: 4.5.5-dirty Platform: Timestamp: 2022-03-04T10:02:17Z Host: Linux 4.9.140-tegra aarch64 CMake: 3.22.2 CMake generator: Unix Makefiles CMake build tool: /bin/gmake Configuration: Release CPU/HW features: Baseline: NEON FP16 C/C++: Built as dynamic libs?: NO C++ standard: 11 C++ Compiler: /opt/rh/devtoolset-10/root/usr/bin/c++ (ver 10.2.1) C++ flags (Release): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-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 -O3 -DNDEBUG -DNDEBUG C++ flags (Debug): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-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 -g -O0 -DDEBUG -D_DEBUG C Compiler: /opt/rh/devtoolset-10/root/usr/bin/cc C flags (Release): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-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 -O3 -DNDEBUG -DNDEBUG C flags (Debug): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-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 -g -O0 -DDEBUG -D_DEBUG Linker flags (Release): -L/root/ffmpeg_build/lib -Wl,--gc-sections -Wl,--as-needed Linker flags (Debug): -L/root/ffmpeg_build/lib -Wl,--gc-sections -Wl,--as-needed ccache: YES Precompiled headers: NO Extra dependencies: /lib64/libopenblas.so Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Test Qt5::Concurrent /lib64/libz.so dl m pthread rt 3rdparty dependencies: libprotobuf ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf quirc tegra_hal OpenCV modules: To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio Disabled: world Disabled by dependency: - Unavailable: java python2 ts Applications: - Documentation: NO Non-free algorithms: NO GUI: QT5 QT: YES (ver 5.15.0 ) QT OpenGL support: NO GTK+: NO VTK support: NO Media I/O: ZLib: /lib64/libz.so (ver 1.2.7) JPEG: libjpeg-turbo (ver 2.1.2-62) WEBP: build (ver encoder: 0x020f) PNG: build (ver 1.6.37) TIFF: build (ver 42 - 4.2.0) JPEG 2000: build (ver 2.4.0) OpenEXR: build (ver 2.3.0) HDR: YES SUNRASTER: YES PXM: YES PFM: YES Video I/O: DC1394: NO FFMPEG: YES avcodec: YES (58.134.100) avformat: YES (58.76.100) avutil: YES (56.70.100) swscale: YES (5.9.100) avresample: NO GStreamer: NO v4l/v4l2: YES (linux/videodev2.h) Parallel framework: pthreads Trace: YES (with Intel ITT) Other third-party libraries: Lapack: YES (/lib64/libopenblas.so) Eigen: NO Custom HAL: YES (carotene (ver 0.0.1)) Protobuf: build (3.19.1) OpenCL: YES (no extra features) Include path: /io/opencv/3rdparty/include/opencl/1.2 Link libraries: Dynamic load Python 3: Interpreter: /opt/python/cp36-cp36m/bin/python3.6 (ver 3.6.15) Libraries: libpython3.6m.a (ver 3.6.15) numpy: /opt/python/cp36-cp36m/lib/python3.6/site-packages/numpy/core/include (ver 1.19.3) install path: python/cv2/python-3 Python (for build): /bin/python2.7 Java: ant: NO JNI: NO Java wrappers: NO Java tests: NO Install to: /io/_skbuild/linux-aarch64-3.6/cmake-install ----------------------------------------------------------------- ``` この後は、 [カメラモジュールを使ってOpenCVでキャプチャする](https://zenn.dev/kotaproj/books/raspberrypi-tips/viewer/400_kiso_camera) を参考に。レガシーカメラスタックを有効にすると、こちらの世界は動作するが、libcamera-helloなどは動作しなくなる。 ``` $ libcamera-hello -t 0 --rotation 180 ERROR: the system appears to be configured for the legacy camera stack ``` C++向けのOpenCV環境も作りたいが、パッケージ化されていない?ひとまず、ソースからビルドする案内をしているページを載せておく。 [OpenCV + OpenCV Contribをビルド 【Raspberry Pi OS編】](https://swallow-incubate.com/archives/blog/20200709/) opencv-pythonでインストールされたバージョン(4.5.5)に合わせて、基本的に、上記サイトを参考にしてダウンロードして展開。 ``` $ sudo apt update $ sudo apt upgrade $ sudo apt install cmake libgtk2.0-dev libjpeg-dev libpng-dev $ wget https://github.com/opencv/opencv/archive/4.5.5.tar.gz -O opencv.tar.gz $ wget https://github.com/opencv/opencv_contrib/archive/4.5.5.tar.gz -O opencv_contrib.tar.gz $ gzip -d -c opencv.tar.gz |tar xvf - $ gzip -d -c opencv_contrib.tar.gz |tar xvf - ``` カーネルをビルドしたときに熱で落ちたりしたことがあるので、一応、 ``` #!/bin/sh while true do echo temparature: `cat /sys/class/thermal/thermal_zone0/temp` sleep 1 done ``` などというスクリプトで温度をモニタリングしながら、以下でビルド。 ``` $ cd opencv-4.5.5 $ mkdir build $ cd build $ cmake -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.5/modules .. $ make -j4 $ sudo make install $ sudo ldconfig ```