2.カメラを使う(RaspberryPiOS Bullseye)

2023/9/26


カメラ

カメラは公式カメラRaspberry Pi Camera V2を使う。
付属のフレキシブルケーブルでRaspberry Pi3Aに接続する。
OSはRaspberryPiOS(32bit 2023-05-03-raspios-bullseye-armhf.img)を使用する。
https://www.switch-science.com/products/8940によると
  • SONY IMX219PQ 8メガピクセル固定フォーカス
  • 静止画像解像度: 3280 × 2464
  • フレームレート:
    • 1080 p@30 fps
    • 720 p@60 fps
    • VGA(640 x 480 p)@90 fps
  • サイズ: 23.86 × 25 × 9 mm
  • フラットリボンケーブルによるRaspberry Piコンピュータボード上の15ピンMIPIカメラシリアルインターフェイス(CSI)コネクタS5への接続


カメラの動作確認をする。何かが映ればよい。
libcamera-hello
カメラで撮影している画像が5秒間表示される。

libcamera-hello -t 0
でずっと表示できる。フォーカスの調整などには便利な機能だ。

カメラをつかう

カメラの動作確認をする。何かが映ればよい。


OPT = -lopencv_coreだけでは コンパイル時に"undefined reference to ...."がたくさん出る。
$ make
g++ -Wall capture.cpp -I /usr/include/opencv4  -lopencv_core
/usr/bin/ld: /tmp/ccBuOzXu.o: in function `main':
capture.cpp:(.text+0x30): undefined reference to `cv::VideoCapture::VideoCapture(int, int)'
/usr/bin/ld: capture.cpp:(.text+0x3c): undefined reference to `cv::VideoCapture::isOpened() const'
/usr/bin/ld: capture.cpp:(.text+0xc8): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
/usr/bin/ld: capture.cpp:(.text+0x144): undefined reference to `cv::imshow(std::__cxx11::basic_string, std::allocator > const&, cv::_InputArray const&)'
/usr/bin/ld: capture.cpp:(.text+0x170): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: capture.cpp:(.text+0x1a0): undefined reference to `cv::VideoCapture::~VideoCapture()'
/usr/bin/ld: capture.cpp:(.text+0x1fc): undefined reference to `cv::VideoCapture::~VideoCapture()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:9: a.out] エラー 1
$ 
OepnCVのサイト(https://docs.opencv.org/4.5.1/index.html) を見るとライブラリ・APIについての説明があるので、cv::VideoCapture他を探す。


説明の中に#include <opencv2/videoio.hpp>と記載されている。
ライブラリとしてvideoioを必要としているので、Makefile中のOPT=...に -lopencv_videoioを追記する。
同様にcv::imshow()のために、OPT=...に -lopencv_highguiを追記する。
なお、ライブラリは/usr/include/opencv4/opencv2/以下に格納されている。
ラズベリパイ本体でコマンドラインからa.outを実行するとカメラが撮影した画像が新しいダイアログで表示される。
リモート接続(ssh -X username@host)で接続するとPC側に画像を表示できる。


RaspberryPi OS 64bitでは失敗する?

OSにRaspberryPiOS(64bit 2023-05-03-raspios-bullseye-arm64.img)で試す。
libcamera-helloは画像が表示されるのでカメラは動いている。
opencvを使うと実行時にエラーを表示する。
 $ ./a.out 
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1824) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (914) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (501) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Start grabbing
Press any key to terminate
ERROR! Blank frame grabbed 
管理者権限でlsmodを実行する。カメラそのものはimx219として認識している。
# lsmod |grep imx219
imx219                 20480  1
v4l2_fwnode            24576  2 imx219,bcm2835_unicam
v4l2_async             24576  3 v4l2_fwnode,imx219,bcm2835_unicam
videodev              274432  9 v4l2_async,bcm2835_codec,imx219,videobuf2_v4l2,bcm2835_unicam,bcm2835_v4l2,videobuf2_common,v4l2_mem2mem,bcm2835_isp
mc                     61440  9 v4l2_async,videodev,bcm2835_codec,imx219,videobuf2_v4l2,bcm2835_unicam,videobuf2_common,v4l2_mem2mem,bcm2835_isp
管理者権限でvcgencmdを実行する。カメラは見つからないらしい。
# vcgencmd get_camera
supported=0 detected=0, libcamera interfaces=0
---カメラが見つからない
📷カメラモジュールを使ってOpenCVでキャプチャする - ⭐( https://zenn.dev/kotaproj/books/raspberrypi-tips/viewer/400_kiso_camera)によるとcamaraをLegacy Camera supportを有効にすればよいらしい。
アプリケーションメニューからRaspberry Piの設定からを開いても、カメラに関する項目は表示されない。


端末からraspi-confで設定画面を開き、管理者権限でraspi-confを実行する。
raspi-conf
インターフェースオプションからカメラの設定を選ぶ。


Legact Camera Enable/disableを選ぶ。


<はい>を選ぶ。


この機能は非推奨で将来サポートしないらしい。


再起動後、カメラを探すと
# lsmod |grep imx219  --- imx219は見つからない bcm2835はある。
# vcgencmd get_camera
supported=1 detected=1, libcamera interfaces=0
---supported=1 detected=1でみつけたようだ
libcamera-helloは実行に失敗する。 これはダメな設定かもしれない。
$ libcamera-hello 
libEGL warning: DRI2: failed to authenticate
Made X/EGL preview window
[0:05:33.338142193] [1574]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
ERROR: *** no cameras available ***

openvcでカメラ画像の表示(上記プログラム)を実行してみる。 WARNは先程と同じだが、今度は画像が表示できる。
$ ./a.out 
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1824) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (914) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (501) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Start grabbing
Press any key to terminate


Legacy Cameraとして使用しているので、OSをアップデートする等で使用できなくなるかもしれない。
[参考]
ラズベリーパイでモニタリング!!( https://www.rs-online.com/designspark/beginners-guide-to-computer-vision-with-raspberry-pi-4-jp)

OpenCVによるUSBカメラ画像の取得( https://qiita.com/vs4sh/items/4a9ce178f1b2fd26ea30)

OepnCV modules( https://docs.opencv.org/4.5.1/index.html)

📷カメラモジュールを使ってOpenCVでキャプチャする - ⭐( https://zenn.dev/kotaproj/books/raspberrypi-tips/viewer/400_kiso_camera)