Skip to content

Latest commit

 

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Raspberry Pi カメラストリーミングサーバー

Raspberry Pi Zero 2 W + Arducam IMX219カメラを使用したMJPEGストリーミングサーバー

  • プロトコル: HTTP
  • フォーマット: MJPEG (Motion JPEG)
  • 解像度: 640x480

1. システム構成

Raspberry Pi Zero 2 W

  • OS: Raspberry Pi OS Lite (Trixie)
  • カメラ: Arducam IMX219 (8MP, Sony IMX219センサー)

Mac

  • 作業用マシン

2. 環境構築手順

2-1. Raspberry Pi Zero 2 W 側の手順

2-1-1. カメラの接続

Arducamカメラをカメラポート(CSI)に接続

カメラ接続

2-1-2. カメラ設定

/boot/firmware/config.txtを編集

sudo vi /boot/firmware/config.txt

差分

# Additional overlays and parameters are documented
# /boot/firmware/overlays/README

+ # imx219 setting
+ camera_auto_detect=0
+ dtoverlay=imx219

# Automatically load overlays for detected DSI displays
display_auto_detect=1

再起動

sudo reboot

参考 「SOFTWARE SETTING」

2-1-3. カメラ認識確認

rpicam-hello --list-cameras

Available camerasに、imx219 が表示されることを確認

Available cameras
-----------------
0 : imx219 [3280x2464 10-bit RGGB] (/base/soc/i2c0mux/i2c@1/imx219@10)
    Modes: 'SRGGB10_CSI2P' : 640x480 [206.65 fps - (1000, 752)/1280x960 crop]
                             1640x1232 [41.85 fps - (0, 0)/3280x2464 crop]
                             1920x1080 [47.57 fps - (680, 692)/1920x1080 crop]
                             3280x2464 [21.19 fps - (0, 0)/3280x2464 crop]
           'SRGGB8' : 640x480 [206.65 fps - (1000, 752)/1280x960 crop]
                      1640x1232 [83.70 fps - (0, 0)/3280x2464 crop]
                      1920x1080 [47.57 fps - (680, 692)/1920x1080 crop]
                      3280x2464 [21.19 fps - (0, 0)/3280x2464 crop]

2-1-4. Picamera2インストール

sudo apt update
sudo apt full-upgrade
sudo apt install -y python3-picamera2 --no-install-recommends

参考 「2.2. Installation and updating」

2-1-5. ~/Cameraフォルダを作成

mkdir ~/Camera

※ソースファイル格納用

2-2. Mac 側の手順

2-2-1. リポジトリをクローン

git clone https://github.com/stonedev-app/raspi_camera.git
cd raspi_camera

2-2-2. sync.conf の設定

3-1-1. sync.confをコピー3-1-2. sync.confの設定を実施

2-2-3. ソースコードをアップロード

./sync.sh upload

2-3. 動作確認

2-3-1. ストリーミングサーバー起動

Raspberry Piで以下のコマンドを実行

cd ~/Camera
python src/streaming_server.py

2-3-2. ブラウザでカメラ画像確認

ブラウザでhttp://<RASPI_HOST>:8000/stream.mjpgにアクセス

3. ファイル同期

sync.sh でMacと Raspberry Pi 間のファイル同期を行う

3-1. 初回設定手順

3-1-1. sync.confをコピー

cp sync.conf.example sync.conf

3-1-2. sync.confの設定

設定が必要な項目
項目名 説明
RASPI_HOST Raspberry Pi のホスト名または IP アドレス
RASPI_USER Raspberry Pi のユーザー名
REMOTE_DIR Raspberry Pi 上の同期先ディレクトリ

3-2. 使い方

# Mac → Raspberry Pi へアップロード
./sync.sh upload

# Raspberry Pi → Mac へダウンロード
./sync.sh download

4. カメラ画像の利用方法

4-1. ブラウザ

http://<RASPI_HOST>:8000/stream.mjpg

4-2. HTML

<img src="http://<RASPI_HOST>:8000/stream.mjpg" />

4-3. Python

import cv2
cap = cv2.VideoCapture('http://<RASPI_HOST>:8000/stream.mjpg')

while True:
    ret, frame = cap.read()
    if ret:
        # フレーム処理
        cv2.imshow('Camera', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

5. 参考資料

About

Raspberry Pi Camera Streaming Server

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages