LoginSignup
0
0

ラズパイ5+Pythonで2つのカメラによる同時撮影をする[RaspberryPi5][Python][OpenCV][picamera2]

Posted at

この記事でわかること

ラズパイ5へカメラモジュールを2つ差し,Python+OpenCVで同時に画像撮影を行う方法.
ステレオカメラやデュアルカメラとして使いたい場合に使用できます.
(検索で出てくるタイトルを変えたかったので,再投稿しました.)

環境

Python

Python 3.11.2
opencv-python 4.9.0.80
picamera2 0.3.17

Raspberry Pi

Raspberry Pi 5 Model B Rev 1.0
Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm

1秒ずつ撮影をするコード

RaspberryPi上で以下のコードを実行すると,Ctrl+Cが入力されるまで1秒ごとに2つのカメラで同時に撮影を行います.

DualCamera.py
import cv2
from picamera2 import Picamera2
import time

def capture_image(camera0, camera1, image_index):
    # カメラ0から画像を取得
    image0 = camera0.capture_array()
    imgpath0 = f"test{image_index}_0.jpg"
    cv2.imwrite(imgpath0, image0)

    # カメラ1から画像を取得
    image1 = camera1.capture_array()
    imgpath1 = f"test{image_index}_1.jpg"
    cv2.imwrite(imgpath1, image1)

    print(imgpath0, "and", imgpath1, "saved!")

camera0 = Picamera2(0)
camera1 = Picamera2(1)

# それぞれのカメラに対して設定を行い、起動
config0 = camera0.create_preview_configuration(main={"size": (1640, 1232)})
camera0.configure(config0)
camera0.start()

config1 = camera1.create_preview_configuration(main={"size": (1640, 1232)})
camera1.configure(config1)
camera1.start()

# 1秒ごとに画像を撮影
try:
    image_index = 0
    while True:
        capture_image(camera0, camera1, image_index)
        time.sleep(1)
        image_index += 1
except KeyboardInterrupt:
    print("Stopping cameras...")
finally:
    camera0.stop()
    camera1.stop()
    print("Cameras stopped.")

コードの説明

カメラのインスタンスを生成します.Picamera2()の引数にカメラの番号を入れましょう.番号はカメラモジュールを差したラズパイの端子を見れば書いてあります.

camera0 = Picamera2(0)
camera1 = Picamera2(1)

次にカメラの設定と起動を行います.今回は撮影する画像を1640×1232で指定しましたが,ここは調整をしてください.これはcamera0の設定ですが,camera1も同様です.
config0 = camera0.create_preview_configuration(main={"size": (1640, 1232)})
camera0.configure(config0)
camera0.start()

カメラの準備ができたら撮影をします.今回はcapture_array()を使いましたが,他の撮影方法もあるようなので,気になる方は以下のリンクを参照してください.

Picamera2ライブラリについて

image0 = camera0.capture_array()

カメラインスタンスの生成に失敗した人向け

カメラインスタンスを用意するときに,以下のようにするとエラーが出ます.ChatGPTに「ラズパイ5でデュアルカメラを使いたい!Pythonでコードを書いて!」と頼むと以下のように出力された人もいるのではないでしょうか(私がそうでした).

修正前
camera0 = Picamera2()
canera1 = Picamera2()
エラー文
ERROR Camera camera.cpp:675 Camera in Acquired state trying acquire() requiring state Available
Camera __init__ sequence did not complete.
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 254, in __init__
    self._open_camera()
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 465, in _open_camera
    self.camera.acquire()
RuntimeError: Failed to acquire camera: Device or resource busy

アクセスしようとしているカメラが使用済みであるという旨のエラーが出るので,カメラインスタンスを作る時は以下のようにしてアクセスするカメラを分けましょう.

修正後
camera0 = Picamera2(0)
camera1 = Picamera2(1)

参考記事:
How To Use Dual Cameras on the Raspberry Pi 5

0
0
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
0
0