我有一個Kivy小部件,我想充當攝像頭實時預覽的佔位符。我已經找到了網絡攝像頭流的一部分,但我無法完全獲取流來填充空的圖像部件(假設我甚至應該使用圖像部件而不是其他部件)。使用Numpy圖像陣列數據更新Kivy小部件
爲了記錄,我沒有使用相機小部件,因爲它不適用於Raspberry Pi相機,它不是USB網絡攝像機。
這裏是我的相機微件類的代碼:
class CV2Camera(BoxLayout):
def __init__(self, **kwargs):
super(CV2Camera, self).__init__(**kwargs)
self.vs = WebcamVideoStream(src=0).start()
def _finish_init(self, dt):
Clock.schedule_interval(self.update, .05)
def update(self, dt):
self.image = self.vs.read()
self.image = imutils.resize(self.image, width=1080)
self.image = np.fliplr(self.image)
self.video_texture = Texture.create(size=(self.image.shape[1], self.image.shape[0]), colorfmt='bgr')
self.video_texture.blit_buffer(self.image.tostring(), colorfmt='bgr', bufferfmt='ubyte')
self.video_panel = self.ids['camera_preview']
with self.video_panel.canvas as canvas:
Rectangle(texture=self.video_texture, pos=self.video_panel.pos, size=self.video_panel.size)
,這裏是我的佈局聲明:
<CV2Camera>
canvas.before:
PushMatrix
canvas.after:
PopMatrix
<PhotoBooth>:
Screen:
name: 'screen1'
GridLayout:
cols: 2
orientation: 'horizontal'
FloatLayout:
size_hint: 4, 1
CV2Camera:
id: camera
orientation: 'horizontal'
Image:
id: camera_preview
GridLayout:
cols: 1
Button:
text: "<-Previous Frame"
Button:
text: "Next Frame->"
Button:
text: "Take Picture!"
on_press: root.capture()
此代碼運行良好,但我得到的是一個灰色的廣場,在那裏我應該顯示圖像/相機預覽。我是Kivy的架構新手,但來自網絡的例子(如this SO thread和this Github project)似乎表明這應該起作用。謝謝。
你嘗試使用https://kivy.org/docs/api-kivy.uix.camera.html – PalimPalim
感謝PALIM,正如上文所述,內置Kivy相機微件不挑「開箱即用」地推出了Raspberry Pi相機,並且我花了很長時間才試圖讓其發揮作用,但沒有成功。 – John
對不起,沒有仔細閱讀。 – PalimPalim