Raspberry Pi 備忘録 / Mbedもあるよ!

Raspberry Pi であれこれやった事の記録

カメラモジュール導入

カメラモジュールを購入したので、テストする。

Raspberry PiモデルA、B、B+、コンピュート用の高精細ビデオカメラ。
Omnivision 5647センサ、焦点固定モジュールに内蔵
5メガピクセルセンサ
内蔵IRフィルタ
静止画像の解像度: 2592 x 1944
最大動画解像度: 1080 p
最大フレームレート: 30fps
サイズ: 20 x 25 x 10 mm
フラットリボンケーブルによるRaspberry Piコンピュータボード上の15ピンMIPIカメラシリアルインターフェイス(CSI)コネクタS5への接続
接続用フレキシケーブル長さは約155mmです

差し込む

起動する

raspi-config

raspi-config から、5 Enable Camera で、 reboot させられる。

用意されているプログラムを試す

raspistill

静止画撮影

raspistill

raspistill Camera App v1.3.8

Runs camera for specific time, and take JPG capture at end if requested

usage: raspistill [options]

Image parameter commands

-?, --help      : This help information
-w, --width     : Set image width <size>
-h, --height    : Set image height <size>
-q, --quality   : Set jpeg quality <0 to 100>
-r, --raw       : Add raw bayer data to jpeg metadata
-o, --output    : Output filename <filename> (to write to stdout, use '-o -'). If not specified, no file is saved
-l, --latest    : Link latest complete image to filename <filename>
-v, --verbose   : Output verbose information during run
-t, --timeout   : Time (in ms) before takes picture and shuts down (if not specified, set to 5s)
-th, --thumb    : Set thumbnail parameters (x:y:quality) or none
-d, --demo      : Run a demo mode (cycle through range of camera options, no capture)
-e, --encoding  : Encoding to use for output file (jpg, bmp, gif, png)
-x, --exif      : EXIF tag to apply to captures (format as 'key=value') or none
-tl, --timelapse        : Timelapse mode. Takes a picture every <t>ms
-fp, --fullpreview      : Run the preview using the still capture resolution (may reduce preview fps)
-k, --keypress  : Wait between captures for a ENTER, X then ENTER to exit
-s, --signal    : Wait between captures for a SIGUSR1 from another process
-g, --gl        : Draw preview to texture instead of using video render component
-gc, --glcapture        : Capture the GL frame-buffer instead of the camera image
-set, --settings        : Retrieve camera settings and write to stdout
-cs, --camselect        : Select camera <number>. Default 0
-bm, --burst    : Enable 'burst capture mode'
-md, --mode     : Force sensor mode. 0=auto. See docs for other modes available
-dt, --datetime : Replace frame number in file name with DateTime (YearMonthDayHourMinSec)
-ts, --timestamp        : Replace frame number in file name with unix timestamp (seconds since 1900)

Preview parameter commands

-p, --preview   : Preview window settings <'x,y,w,h'>
-f, --fullscreen        : Fullscreen preview mode
-op, --opacity  : Preview window opacity (0-255)
-n, --nopreview : Do not display a preview window

Image parameter commands

-sh, --sharpness        : Set image sharpness (-100 to 100)
-co, --contrast : Set image contrast (-100 to 100)
-br, --brightness       : Set image brightness (0 to 100)
-sa, --saturation       : Set image saturation (-100 to 100)
-ISO, --ISO     : Set capture ISO
-vs, --vstab    : Turn on video stabilisation
-ev, --ev       : Set EV compensation
-ex, --exposure : Set exposure mode (see Notes)
-awb, --awb     : Set AWB mode (see Notes)
-ifx, --imxfx   : Set image effect (see Notes)
-cfx, --colfx   : Set colour effect (U:V)
-mm, --metering : Set metering mode (see Notes)
-rot, --rotation        : Set image rotation (0-359)
-hf, --hflip    : Set horizontal flip
-vf, --vflip    : Set vertical flip
-roi, --roi     : Set region of interest (x,y,w,d as normalised coordinates [0.0-1.0])
-ss, --shutter  : Set shutter speed in microseconds
-awbg, --awbgains       : Set AWB gains - AWB mode must be off
-drc, --drc     : Set DRC Level
-st, --stats    : Force recomputation of statistics on stills capture pass
-a, --annotate  : Enable/Set annotate flags or text
-3d, --stereo   : Select stereoscopic mode
-dec, --decimate        : Half width/height of stereo image
-3dswap, --3dswap       : Swap camera order for stereoscopic
-ae, --annotateex       : Set extra annotation parameters (text size, text colour(hex YUV), bg colour(hex YUV))


Notes

Exposure mode options :
off,auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks

AWB mode options :
off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon

Image Effect mode options :
none,negative,solarise,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,watercolour,film,blur,saturation,colourswap,washedout,posterise,colourpoint,colourbalance,cartoon

Metering Mode options :
average,spot,backlit,matrix

Dynamic Range Compression (DRC) options :
off,low,med,high

Preview parameter commands

-gs, --glscene  : GL scene square,teapot,mirror,yuv,sobel
-gw, --glwin    : GL window settings <'x,y,w,h'>

とのことなので、

$ raspistill -o mod.jpeg

してみたところ、、、X window 立ち上げてないのに、モニターに映像が写った! とにかく、デフォルトでは、プレビューする様だ。

$ raspistill -w 480 -h 360 -n -o mod.jpg
$ fim test.jpg

f:id:pongsuke:20151228125859j:plain

raspivid

動画撮影

raspivid Camera App v1.3.12

Display camera output to display, and optionally saves an H264 capture at requested bitrate


usage: raspivid [options]

Image parameter commands

-?, --help      : This help information
-w, --width     : Set image width <size>. Default 1920
-h, --height    : Set image height <size>. Default 1080
-b, --bitrate   : Set bitrate. Use bits per second (e.g. 10MBits/s would be -b 10000000)
-o, --output    : Output filename <filename> (to write to stdout, use '-o -')
-v, --verbose   : Output verbose information during run
-t, --timeout   : Time (in ms) to capture for. If not specified, set to 5s. Zero to disable
-d, --demo      : Run a demo mode (cycle through range of camera options, no capture)
-fps, --framerate       : Specify the frames per second to record
-e, --penc      : Display preview image *after* encoding (shows compression artifacts)
-g, --intra     : Specify the intra refresh period (key frame rate/GoP size). Zero to produce an initial I-frame and then just P-frames.
-pf, --profile  : Specify H264 profile to use for encoding
-td, --timed    : Cycle between capture and pause. -cycle on,off where on is record time and off is pause time in ms
-s, --signal    : Cycle between capture and pause on Signal
-k, --keypress  : Cycle between capture and pause on ENTER
-i, --initial   : Initial state. Use 'record' or 'pause'. Default 'record'
-qp, --qp       : Quantisation parameter. Use approximately 10-40. Default 0 (off)
-ih, --inline   : Insert inline headers (SPS, PPS) to stream
-sg, --segment  : Segment output file in to multiple files at specified interval <ms>
-wr, --wrap     : In segment mode, wrap any numbered filename back to 1 when reach number
-sn, --start    : In segment mode, start with specified segment number
-sp, --split    : In wait mode, create new output file for each start event
-c, --circular  : Run encoded data through circular buffer until triggered then save
-x, --vectors   : Output filename <filename> for inline motion vectors
-cs, --camselect        : Select camera <number>. Default 0
-set, --settings        : Retrieve camera settings and write to stdout
-md, --mode     : Force sensor mode. 0=auto. See docs for other modes available
-if, --irefresh : Set intra refresh type
-fl, --flush    : Flush buffers in order to decrease latency


H264 Profile options :
baseline,main,high


H264 Intra refresh options :
cyclic,adaptive,both,cyclicrows

Preview parameter commands

-p, --preview   : Preview window settings <'x,y,w,h'>
-f, --fullscreen        : Fullscreen preview mode
-op, --opacity  : Preview window opacity (0-255)
-n, --nopreview : Do not display a preview window

Image parameter commands

-sh, --sharpness        : Set image sharpness (-100 to 100)
-co, --contrast : Set image contrast (-100 to 100)
-br, --brightness       : Set image brightness (0 to 100)
-sa, --saturation       : Set image saturation (-100 to 100)
-ISO, --ISO     : Set capture ISO
-vs, --vstab    : Turn on video stabilisation
-ev, --ev       : Set EV compensation
-ex, --exposure : Set exposure mode (see Notes)
-awb, --awb     : Set AWB mode (see Notes)
-ifx, --imxfx   : Set image effect (see Notes)
-cfx, --colfx   : Set colour effect (U:V)
-mm, --metering : Set metering mode (see Notes)
-rot, --rotation        : Set image rotation (0-359)
-hf, --hflip    : Set horizontal flip
-vf, --vflip    : Set vertical flip
-roi, --roi     : Set region of interest (x,y,w,d as normalised coordinates [0.0-1.0])
-ss, --shutter  : Set shutter speed in microseconds
-awbg, --awbgains       : Set AWB gains - AWB mode must be off
-drc, --drc     : Set DRC Level
-st, --stats    : Force recomputation of statistics on stills capture pass
-a, --annotate  : Enable/Set annotate flags or text
-3d, --stereo   : Select stereoscopic mode
-dec, --decimate        : Half width/height of stereo image
-3dswap, --3dswap       : Swap camera order for stereoscopic
-ae, --annotateex       : Set extra annotation parameters (text size, text colour(hex YUV), bg colour(hex YUV))


Notes

Exposure mode options :
off,auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks

AWB mode options :
off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon

Image Effect mode options :
none,negative,solarise,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,watercolour,film,blur,saturation,colourswap,washedout,posterise,colourpoint,colourbalance,cartoon

Metering Mode options :
average,spot,backlit,matrix

Dynamic Range Compression (DRC) options :
off,low,med,high

とりあえず動かすために raspivid -d おなじく、モニターに映像が出る!

$ raspivid -o video.h264 -t 4000 -w 640 -h 480
$ omxplayer video.h264

おや? 4秒の動画のはず(撮影には4秒かかる)のに、再生が一瞬で終わるぞ・・・。 フレームレートや何やらが問題らしい。 だから、情報を足してやればいいとか言うのだが、、、

# apt-get install gpac

して、

$ raspivid -o video.h264 -fps 30 -t 4000 -w 640 -h 480
$ MP4Box -fps 30 -add video.h264 video.mp4
$ omxplayer video.mp4

おぉ、、、確かに、きちんと再生された。

しかし不思議だ

$ omxplayer video.mp4 
Video codec omx-h264 width 640 height 480 profile 100 fps 30.000000
Subtitle count: 0, state: off, index: 1, delay: 0
V:PortSettingsChanged: 640x480@30.00 interlace:0 deinterlace:0 anaglyph:0 par:1.00 layer:0 alpha:255
have a nice day ;)

は、正常に再生されるのに、

$ omxplayer --fps=30 video.h264 
Video codec omx-h264 width 640 height 480 profile 100 fps 30.000000
Subtitle count: 0, state: off, index: 1, delay: 0
V:PortSettingsChanged: 640x480@30.00 interlace:0 deinterlace:0 anaglyph:0 par:1.00 layer:0 alpha:255
have a nice day ;)

は、高速で終わってしまう。

mjpg-streamer(モジュール対応)

jacksonliam の、カメラモジュール対応 mjpg-streamer を入れます。 gitから入手して、コンパイルし直す。

# git clone https://github.com/jacksonliam/mjpg-streamer.git mjpg-streamer
# cd mjpg-streamer/mjpg-streamer-experimental/
# make

そして、

./mjpg_streamer -o "./output_http.so -w ./www -p 8080" -i "./input_raspicam.so -fps 30 -x 320 -y 240" 

Python からカメラモジュールを触る

ドキュメント Python picamera - Raspberry Pi Documentation

#!/usr/bin/python
# coding=utf-8

import os
import time
import picamera

camera = picamera.PiCamera()
camera.resolution = (640, 480)

# 静止画撮影
camera.start_preview()
camera.capture('test.jpg')
time.sleep(3)
camera.stop_preview()

# 動画画撮影
camera.start_preview()
camera.start_recording('video.h264')
#time.sleep(3)
camera.wait_recording(3)
camera.stop_recording()
camera.stop_preview()

# フレームレート情報を足す
os.system('/usr/bin/MP4Box -add video.h264 video.mp4')