【发布时间】:2020-02-19 14:03:21
【问题描述】:
我想在 Kivy 的盒子布局中显示 2 到 4 个 rtsp 流,但我似乎不知道如何使用 kv 语言来实现这一点。我可以使用此代码显示一台相机:
class KivyCamera(Image):
def __init__(self, capture, fps, **kwargs):
super(KivyCamera, self).__init__(**kwargs)
self.capture = capture
Clock.schedule_interval(self.update, 1.0 / fps)
def update(self, dt):
ret, frame = self.capture.read()
if ret:
# convert it to texture
buf1 = cv2.flip(frame, 0)
buf = buf1.tostring()
image_texture = Texture.create(
size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
# display image from the texture
self.texture = image_texture
class CamApp(App):
def build(self):
self.capture = cv2.VideoCapture('stream address removed for stack overflow')
self.my_camera = KivyCamera(capture=self.capture, fps=30)
return self.my_camera
def on_stop(self):
#without this, app will not exit even if the window is closed
self.capture.release()
if __name__ == '__main__':
CamApp().run()
不过,这只是使用 .py。我希望能够使用 kv 语言创建 KivyCamera 实例并为每个实例设置不同的 capture 值。
例如:
BoxLayout:
orientation: 'horizontal'
KivyCamera:
capture: 'myvalue'
pos_hint: {'left': 1}
KivyCamera:
capture: 'anothervalue'
pos_hint: {'right': 1}
【问题讨论】:
-
嗯,在 python 中,capture 属性显然是 VideoCapture,但在 .kv 中你使用字符串你认为它合乎逻辑吗?
-
我明白这一点。我的例子更像是一个占位符的想法。我如何将 VideoCapture 放在那里?