我是python的新手,而对kivy则是新手,事实证明他们帮助我创建了此代码(如下),事实证明,在我的代码似乎运行良好之后,许多人告诉我,出于良好的做法,我没有编写我将使用该.kv所存在的python创建图形环境,更清楚地表明我无法实现该方法,希望您能帮助我抄录它,谢谢我衷心的感谢,在代码下方,我只想抄录我所感谢的内容。
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.graphics.texture import Texture
from kivy.clock import Clock
import cv2
class CvCamera(App):
def build(self): # Construcción de interfaz de usuario, etc
self._cap = cv2.VideoCapture(0)
layout2 = BoxLayout(orientation='horizontal', size_hint=(1.0, 0.1))
self.img1 = Image(size_hint=(1.0, 0.7))
layout = BoxLayout(orientation='vertical')
layout.add_widget(self.img1)
layout.add_widget(layout2)
while not self._cap.isOpened():
pass
Clock.schedule_interval(self.update, 1.0 / 30.0)
return layout
def update(self, dt):
ret, img = self._cap.read()
img = cv2.flip(img, 0)
texture1 = Texture.create(size=(img.shape[1], img.shape[0]), colorfmt='bgr')
texture1.blit_buffer(img.tostring(), colorfmt='bgr', bufferfmt='ubyte')
self.img1.texture = texture1
if __name__ == '__main__':
CvCamera().run()
Here is the equivalent using
kv
language:Note that, in general, the arguments in
()
of theWidgets
become entries below and indented from theWidget
name.