コンテンツへスキップ

picameraライブラリとストリーミングデモ

PythonでRasPi3の公式カメラモジュール V2.1からストリーミング配信」で、Pythonのpicameraライブラリと、picameraを使ったストリーミングのデモが紹介されていたので試してみます。

pistreamingのガイドに沿って関連パッケージと共にインストールします。

mypi@raspberrypi:~ $ sudo apt-get install libav-tools git python3-picamera python3-ws4py
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
git はすでに最新バージョン (1:2.11.0-3+deb9u2) です。
python3-picamera はすでに最新バージョン (1.13) です。
以下の追加パッケージがインストールされます:
ffmpeg
提案パッケージ:
ffmpeg-doc python3-cherrypy3 | python3-tornado
以下のパッケージが新たにインストールされます:
ffmpeg libav-tools python3-ws4py
アップグレード: 0 個、新規インストール: 3 個、削除: 0 個、保留: 1 個。
1,701 kB のアーカイブを取得する必要があります。
この操作後に追加で 3,444 kB のディスク容量が消費されます。
続行しますか? [Y/n] Y
取得:1 http://archive.raspberrypi.org/debian stretch/main armhf ffmpeg armhf 7:3.2.10-1~deb9u1+rpt1 [1,516 kB]
取得:2 http://mirrordirector.raspbian.org/raspbian stretch/main armhf python3-ws4py all 0.3.4-4 [129 kB]
取得:3 http://archive.raspberrypi.org/debian stretch/main armhf libav-tools all 7:3.2.10-1~deb9u1+rpt1 [55.7 kB]
1,701 kB を 19秒 で取得しました (85.3 kB/s)
以前に未選択のパッケージ ffmpeg を選択しています。
(データベースを読み込んでいます ... 現在 124191 個のファイルとディレクトリがインストールされています。)
.../ffmpeg_7%3a3.2.10-1~deb9u1+rpt1_armhf.deb を展開する準備をしています ...
ffmpeg (7:3.2.10-1~deb9u1+rpt1) を展開しています...
以前に未選択のパッケージ libav-tools を選択しています。
.../libav-tools_7%3a3.2.10-1~deb9u1+rpt1_all.deb を展開する準備をしています ...
libav-tools (7:3.2.10-1~deb9u1+rpt1) を展開しています...
以前に未選択のパッケージ python3-ws4py を選択しています。
.../python3-ws4py_0.3.4-4_all.deb を展開する準備をしています ...
python3-ws4py (0.3.4-4) を展開しています...
ffmpeg (7:3.2.10-1~deb9u1+rpt1) を設定しています ...
man-db (2.7.6.1-2) のトリガを処理しています ...
python3-ws4py (0.3.4-4) を設定しています ...
libav-tools (7:3.2.10-1~deb9u1+rpt1) を設定しています ...

pistreamingをgitからcloneします。

mypi@raspberrypi:~ $ git clone https://github.com/waveform80/pistreaming.git
Cloning into 'pistreaming'...
remote: Counting objects: 93, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 93 (delta 9), reused 13 (delta 4), pack-reused 75
Unpacking objects: 100% (93/93), done.

実行します。

mypi@raspberrypi:~/pistreaming $ python3 server.py
Initializing camera
Initializing websockets server on port 8084
Initializing HTTP server on port 8082
Initializing broadcast thread
Spawning background conversion process
Starting recording
Starting websockets thread
Starting HTTP server thread
Starting broadcast thread
192.168.1.6 - - [04/Mar/2018 15:20:13] "GET /index.html HTTP/1.1" 200 -
192.168.1.6 - - [04/Mar/2018 15:20:13] "GET /jsmpg.js HTTP/1.1" 200 -
192.168.1.8 - - [04/Mar/2018 15:21:10] "GET /jsmpg.js HTTP/1.1" 200 -
192.168.1.8 - - [04/Mar/2018 15:21:11] "GET / HTTP/1.1" 301 -
192.168.1.8 - - [04/Mar/2018 15:21:11] "GET /index.html HTTP/1.1" 200 -
^CStopping recording
Waiting for background conversion process to exit
Waiting for broadcast thread to finish
Shutting down HTTP server
Shutting down websockets server
Waiting for HTTP server thread to finish
Waiting for websockets thread to finish

Webブラウザから「http://ラズパイのIPアドレス:8082」にアクセスするとストリーミング映像が見えます。
WindowsのFirefoxとiPhoneのSafariの両方からアクセスしてみたところ、両方で同時に映像を見ることが出来ました。ブラウザからアクセスしたタイミングでラズパイのコマンドラインには上記のように接続元IPアドレスが表示されていきます。Ctrl+Cで終了すると後処理が走って終了します。

ストリーミングで見える映像は体感的には遅延はあまり無く、せいぜいコンマ数秒程度に思えます。

コメントを残す

%d人のブロガーが「いいね」をつけました。