LoginSignup
2
1

More than 1 year has passed since last update.

Raspberry Pi 4のセットアップ(Raspberry Pi OS編)

Last updated at Posted at 2022-05-28

Raspberry Pi 4のセットアップ(Raspberry Pi OS編)

はじめに

NVIDIA Jetson Nanoでこの2年ほど色々とやってきたのですが、最近、Raspberry Pi 4(以下、ラズパイと称す)でも検討しはじめました。しばらくの間は、Jetsonシリーズとそろえて、Ubuntuで検討してきたのですが、Jetsonのときと同じことをやるのにも、なかなかあっさりとは行かないので。。

気分一新で、Raspberry Pi OSでの環境で検討しようと思い立ちました。私の場合は、イメージセンサ系の検討を進めたいわけですが、libcameraという新しい枠組みも用意されているようで、そのあたりに期待したい、というところです。

IMG20220528193230.jpg

インストール

あらためて、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

で、カメラでライブビューできた。

カーネルのビルド

後で、イメージセンサのドライバを追加することを想定して、早速、Linuxカーネルのビルド環境も確認しておく。

Raspberry Pi Documentation→The Linux kernel内の以下のページに従って作業する。

Building the Kernel

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

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をインストール

を参考に、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でキャプチャする

を参考に。レガシーカメラスタックを有効にすると、こちらの世界ではあっさりと動作したが、libcamera-helloなどは動作しなくなるみたい。

(メモ)今後、自分で新規に作成したドライバをラズパイ環境にも組み込んでみる予定だが、多分、かんたんにやれるのはレガシー系になるのだろうと予想。libcameraにおける世界も確認しながら検討していく。

$ 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編】

おわりに

ひとまず、最近、ラズパイで改めて、Raspberry Pi OSにて今日までの時点でセットアップしたところまで、こちらで紹介してみました、というところです。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1