2016-11-29 67 views
0

我正在使用Kivy的网络摄像头。 @Arnav使用opencv来跟踪this example,以形成并显示相机作为小部件。我已经“扩展”了python中的布局,添加了两个按钮作为测试,为更复杂的布局做准备。Kivy相机作为KV语言部件

class CamApp(App): 
    def build(self): 
     self.capture = cv2.VideoCapture(0) 
     self.my_camera = KivyCamera(capture=self.capture, fps=30,resolution=(1920,1080)) 
     root = BoxLayout(orientation = 'vertical') 
     root.add_widget(self.my_camera,1) 
     box2 = BoxLayout(orientation = 'vertical') 
     btn1 = Button(text='Hello world 1') 
     btn2 = Button(text='Hello world 2') 
     box2.add_widget(btn1) 
     box2.add_widget(btn2) 
     root.add_widget(box2, 0) 
     return root 
     #return Builder.load_string(kv) 

虽然这工作我宁愿UI组件迁出蟒蛇,并成为一个kv language文件。 问题是知道如何“描述”kv文件中的self.my_camera

我不知道是否继承KivyCamera类作为widgetkv文件中即

kv = ''' 
<[email protected]>: 
    texture: self.my_camera 
    resolution: (1920, 1080) 
    pos: self.pos 
    size: self.size 

或者是否使用canvas部件

<MyWidget>: 
    canvas: 
     Rectangle: 
      source: self.my_camera 
      pos: self.pos 
      size: self.size 

我已经尝试过其他“黑客”但在所有情况下,问题都通过self.my_camera链接到kv文件中。

有什么建议吗?

回答

0

也许这个例子可以帮助你。

# Import 'kivy.core.text' must be called in entry point script 
# before import of cv2 to initialize Kivy's text provider. 
# This fixes crash on app exit. 

import kivy.core.text 
import cv2 
from kivy.app import App 
from kivy.base import EventLoop 
from kivy.uix.image import Image 
from kivy.clock import Clock 
from kivy.graphics.texture import Texture 
from kivy.uix.boxlayout import BoxLayout 
from kivy.core.window import Window 


class KivyCamera(Image): 

    def __init__(self, **kwargs): 
     super(KivyCamera, self).__init__(**kwargs) 
     self.capture = None 

    def start(self, capture, fps=30): 
     self.capture = capture 
     Clock.schedule_interval(self.update, 1.0/fps) 

    def stop(self): 
     Clock.unschedule_interval(self.update) 
     self.capture = None 

    def update(self, dt): 
     return_value, frame = self.capture.read() 
     if return_value: 
      texture = self.texture 
      w, h = frame.shape[1], frame.shape[0] 
      if not texture or texture.width != w or texture.height != h: 
       self.texture = texture = Texture.create(size=(w, h)) 
       texture.flip_vertical() 
      texture.blit_buffer(frame.tobytes(), colorfmt='bgr') 
      self.canvas.ask_update() 


capture = None 


class QrtestHome(BoxLayout): 

    def init_qrtest(self): 
     pass 

    def dostart(self, *largs): 
     global capture 
     capture = cv2.VideoCapture(0) 
     self.ids.qrcam.start(capture) 

    def doexit(self): 
     global capture 
     if capture != None: 
      capture.release() 
      capture = None 
     EventLoop.close() 


class qrtestApp(App): 

    def build(self): 
     Window.clearcolor = (.4,.4,.4,1) 
     Window.size = (400, 300) 
     homeWin = QrtestHome() 
     homeWin.init_qrtest() 
     return homeWin 

    def on_stop(self): 
     global capture 
     if capture: 
      capture.release() 
      capture = None 

qrtestApp().run() 

和KV文件:

<QrtestHome>: 

    BoxLayout: 
     orientation: "vertical" 

     Label: 
      height: 20 
      size_hint_y: None 
      text: 'Testing the camera' 

     KivyCamera: 
      id: qrcam 

     BoxLayout: 
      orientation: "horizontal" 
      height: 20 
      size_hint_y: None 

      Button: 
       id: butt_start 
       size_hint: 0.5,1 
       text: "start" 
       on_press: root.dostart() 

      Button: 
       id: butt_exit 
       text: "quit" 
       size_hint: 0.5,1 
       on_press: root.doexit() 
+0

只是想你的建议,但是,它只是产生一个浅灰色的屏幕,没有别的。假设这是来自'Window.clearcolor =(.4,.4,.4,1)' – tomdertech

+0

这可能是由你的文件名造成的。 py和.kv文件。只需命名这些qrtest.py和qrtest.kv。然后再试一次。 –

+0

工作正常!虽然有时候我得到了一个内存异常。 '[警告] [GL] BGRA质地支持不适 异常异常:异常(“无法为纹理分配存储器(大小为921600)”,)在“kivy.graphics.texture.Texture.allocate” ignored'应该我提出另一个问题? – tomdertech