Based on arnav's example I am trying to add this custom camera widget into a kv structure. Unfortunately this 'mechanism' is still not clear to me. So I have ended up with the following code. The light of the cam is active after click on the start button. However no 'video stream' is being displayed. When running just arnav's code with cv2.VideoCapture(0), the 'video stream' is displayed.
What am I doing wrong here?
qrtest.py:
from kivy.app import App
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
import cv2
import os
class KivyCamera(Image):
def dostart(self, capture, fps, **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
capture = None
class QrtestHome(BoxLayout):
def init_qrtest(self):
pass
def dostart(self, *largs):
global capture
capture = cv2.VideoCapture(0)
my_camera = KivyCamera(capture=capture, fps=10, )
my_camera.dostart(capture,10)
def doexit(self):
global capture
if capture != None:
capture.release()
os._exit(0)
class qrtestApp(App):
def build(self):
Window.clearcolor = (.4,.4,.4,1)
Window.size = (400, 300)
homeWin = QrtestHome()
homeWin.init_qrtest()
return homeWin
qrtestApp().run()
and the qrtest.kv file:
<QrtestHome>:
BoxLayout:
orientation: "vertical"
Label:
height: 20
size_hint_y: None
text: 'Testing the camera'
KivyCamera:
canvas:
Color:
rgb: (0, 0.6, 0.6)
Rectangle:
texture: self.texture
pos: (50, 30)
size: (300, 240)
height: 260
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()