Raspberry Piに取り付けたカメラをプレビューするソフトがないので、探してみたらちょうどよいのがありました。
このままではうまく動作しなかったので少し変更しました。
ルーツのインストール
$ sudo apt install -y dpkg-dev build-essential python3.7-dev libpython3.7-dev $ sudo apt install -y freeglut3-dev libgl1-mesa-dev libglu1-mesa-dev libgstreamer-plugins-base1.0-dev libgtk-3-dev $ sudo apt install -y libjpeg-dev libnotify-dev libpng-dev libsdl2-dev libsm-dev libtiff-dev libwebkit2gtk-4.0-dev $ sudo apt install -y libxtst-dev libgtk2.0-dev libwebkitgtk-dev python3-scipy $ sudo apt install -y gpac
$ pip3 install wxPython |
コード
#
import os
import wx
import time
import picamera
class MyFrame(wx.Frame):
def __init__(self):
# common
#(w, h) = wx.DisplaySize()
(self.w, self.h) = (int(1280/2),int(720/2))
font = wx.Font(18, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
wx.Frame.__init__(self, None, -1, "Camera Control", pos=(0,0), size=(self.w, self.h) )
# window
self.Bind( wx.EVT_ACTIVATE, self.Child_activate )
self.Bind(wx.EVT_SIZE, self.on_resize)
self.Bind(wx.EVT_MOVE, self.on_move)
# btnRecord
self.btnPhoto = wx.Button(self, -1, "Pht", pos=(0,0+30),size=(80, 80))
self.btnPhoto.Bind(wx.EVT_BUTTON, self.OnPhotoButton)
self.btnPhoto.SetFont(font)
# btnRecord
self.btnRecord = wx.Button(self, -1, "Rec", pos=(0,80+30),size=(80, 80))
self.btnRecord.Bind(wx.EVT_BUTTON, self.OnRecordButton)
self.btnRecord.SetFont(font)
# btnZoom
self.btnZoom = wx.Button(self, -1, "Zoom", pos=(0,160+30),size=(80, 80))
self.btnZoom.Bind(wx.EVT_BUTTON, self.OnZoomButton)
self.btnZoom.SetFont(font)
# btnClose
self.btnClose = wx.Button(self, -1, "Close", pos=(0,240+30),size=(80, 80))
self.btnClose.Bind(wx.EVT_BUTTON, self.OnCloseButton)
self.btnClose.SetFont(font)
# lblText
self.lblText = wx.StaticText(self, wx.ID_ANY, 'Cam',pos=(0,0))
self.lblText.SetFont(font)
# Camera
self.camera = picamera.PiCamera()
self.camera.vflip = True
self.camera.hflip = True
self.camera.resolution = (1280,720)
self.camera.start_preview(fullscreen=False,window=(85, 5, self.w - 100, self.h + 40))
# Flg
self.isRecord = False
self.isZoom = False
def OnPhotoButton(self, event):
self.Photo()
def OnRecordButton(self, event):
self.isRecord = not self.isRecord
if self.isRecord == True:
self.btnRecord.SetLabel('Stop')
self.Record()
else:
self.btnRecord.SetLabel('Record')
self.Stop()
def OnZoomButton(self, event):
self.isZoom = not self.isZoom
if self.isZoom == True:
self.camera.zoom = (0.2, 0.2, 0.6, 0.6)
else:
self.camera.zoom = (0.0, 0.0, 1.0, 1.0)
def OnCloseButton(self, event):
if self.isRecord == True:
self.Stop()
self.camera.stop_preview()
self.camera.close()
self.Close()
def Photo(self):
self.lblText.SetLabel('take photo...')
self.camera.capture('photo.jpg')
os.system('mv photo.jpg ~/Desktop/photo.jpg')
self.lblText.SetLabel('take photo...ok')
def Record(self):
self.lblText.SetLabel('recording...')
self.camera.start_recording('movie.h264')
def Stop(self):
self.lblText.SetLabel('output...')
self.camera.stop_recording()
# print('start...')
os.system('MP4Box -fps 30 -add movie.h264 movie.mp4')
os.system('rm movie.h264')
os.system('mv movie.mp4 ~/Desktop/movie.mp4')
self.lblText.SetLabel('output...movie.mp4')
# print('end')
def Child_activate(self,event):
pos = self.GetScreenPosition()
(x,y) = pos
size = self.GetSize()
(w,h) = size
if( event.GetActive()):
self.camera.start_preview(fullscreen=False,window=(x+85, y+5, w - 100, h + 40))
else:
self.camera.stop_preview()
def on_resize(self, event):
pos = self.GetScreenPosition()
(x,y) = pos
size = self.GetSize()
(w,h) = size
self.camera.stop_preview()
self.camera.start_preview(fullscreen=False,window=(x+85, y+5, w - 100, h + 40))
def on_move(self, event):
pos = self.GetScreenPosition()
(x,y) = pos
size = self.GetSize()
(w,h) = size
self.camera.stop_preview()
self.camera.start_preview(fullscreen=False,window=(x+85, y+5, w - 100, h + 40))
if __name__ == '__main__':
app = wx.App()
myapp = MyFrame()
myapp.Show()
app.MainLoop()
参考