若是一切順利的話,就可使用命令控制攝像頭了html
pi@raspberrypi:~ $ raspistill -o Desktop/image.jpg
增長參數,還能夠更改圖片大小python
pi@raspberrypi:~ $ raspistill -o Desktop/image-small.jpg -w 640 -h 480
輸入raspistill直接回車可查詢raspistill命令其餘參數網絡
pi@raspberrypi:~ $ raspivid -o Desktop/video.h264 -t 5000 -w 1024 -h 768
使用Python程序控制攝像頭須要使用PiCamera庫
打開Raspberry Pi自帶的Thonny Python IDE,新建camera.py文件,文件命名不能用PiCamera.pyide
from picamera import PiCamera from time import sleep camera = PiCamera() # Rotate by 180 degrees when the camera is upside-down camera.rotation = 180 camera.start_preview(alpha=200) # Make the camera preview see-through by setting an alpha level from 0 to 255 sleep(5) camera.stop_preview()
上述代碼實現打開攝像頭預覽5秒鐘再關閉的功能。可是若是這個程序僅在Raspberry Pi接入了顯示器纔有效,SSH或是VNC訪問是無效的。spa
from picamera import PiCamera from time import sleep camera = PiCamera() # Rotate by 180 degrees when the camera is upside-down camera.rotation = 180 camera.start_preview(alpha=200) # Make the camera preview see-through by setting an alpha level from 0 to 255 sleep(5) # Take a picture and save as /home/pi/Desktop/image.jpg' camera.capture('/home/pi/Desktop/image.jpg') # Take 5 pictures every 5 seconds and save as /home/pi/Desktop/image0.jpg' ... image4.jpg for i in range(5): sleep(5) camera.capture('/home/pi/Desktop/image%s.jpg' % i) camera.stop_preview()
在拍照前,最好讓攝像頭sleep至少2秒,使之可以感光
將capture()改爲start_recording()和stop_recording()就可控制攝像頭拍攝錄像了code
from picamera import PiCamera, Color from time import sleep camera = PiCamera() camera.rotation = 180 camera.resolution = (1024, 768) camera.framerate = 15 camera.start_preview() camera.brightness = 70 camera.annotate_background = Color('blue') camera.annotate_foreground = Color('yellow') camera.annotate_text = "Hello world!" camera.annotate_text_size = 30 sleep(5) camera.capture('/home/pi/Camera/pic.jpg') camera.stop_preview() camera.start_preview() for i in range(5): camera.annotate_text = "Brightness: %s" % i camera.brightness = i*20 sleep(0.1) camera.capture('/home/pi/Camera/brightness%s.jpg' % i) camera.stop_preview() camera.start_preview() for i in range(5): camera.annotate_text = "Contrast: %s" % i camera.contrast = i*20 sleep(0.1) camera.capture('/home/pi/Camera/contrast%s.jpg' % i) camera.stop_preview()
$ sudo apt-get install vlc #Raspberry Pi系統自帶了 $ #-o - 輸出到stdout,-t 0不暫停當即獲取流, 640x360,25幀/s,-rot 180畫面旋轉180度(用了支架攝像頭畫面倒過來了) $ sudo raspivid -o - -rot 180 -t 0 -w 480 -h 360 -fps 25|cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8090}' :demux=h264
在與Raspberry Pi在同一局域網內的其餘設備上,用vlc打開網絡串流 http://Raspberry Pi的ip:8090
就播放看到攝像頭的畫面了,可是vlc的實施監控存在5s左右的延時,體驗並非很好。htm