I am using Kivy with a webcam. I have followed this example by #Arnav of using opencv to form and display the camera as a widget. I have "extended" the layout within python it to add two buttons as a test, in preparation for a more complicated layout.
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)
While this works I would prefer to move the UI components out of python and into a kv language file.
The problem is knowing how to "describe" the self.my_camera in the kv file?
I am not sure whether to inherit the KivyCamera class as a widget within the kv file i.e.
kv = '''
<Cam1#KivyCamera>:
texture: self.my_camera
resolution: (1920, 1080)
pos: self.pos
size: self.size
Or whether to use the canvas widget
<MyWidget>:
canvas:
Rectangle:
source: self.my_camera
pos: self.pos
size: self.size
I have tried other "hacked" implementations, but in all cases the problem is linking through the self.my_camera into the kv file.
Any suggestions?
Perhaps this example may help you.
# 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()
and the kv file:
<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()
Related
My app display file names with path in a Label and sometimes the file name is to big to fit in the screen so my first thought was to use a Marquee but couldn't find any widget with that functionality.
Any way to do it?
Interesting problem. Here is a first cut at a Marquee widget:
from kivy import Config
import os
from ast import literal_eval
from kivy.animation import Animation
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty, NumericProperty, BooleanProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
from kivy.core.text import Label as CoreLabel
if 'KIVY_DOC' not in os.environ:
_default_font_paths = literal_eval(Config.get('kivy', 'default_font'))
DEFAULT_FONT = _default_font_paths.pop(0)
else:
DEFAULT_FONT = None
Builder.load_string('''
<Marquee>:
StencilView:
id: sten
pos: root.pos
size_hint: None, None
size: root.size
Image:
id: label
texture: root.texture
pos: root.pos
size_hint: None, None
size: self.texture_size
''')
class Marquee(FloatLayout):
texture = ObjectProperty()
text = StringProperty()
duration = NumericProperty(2)
font_name = StringProperty(DEFAULT_FONT)
font_size = NumericProperty(12)
bold = BooleanProperty(False)
italic = BooleanProperty(False)
padding = NumericProperty(0)
def __init__(self, **kwargs):
super(Marquee, self).__init__(**kwargs)
self.anim = None
self.x_original = None
fbind = self.fbind
redraw = self.redraw
fbind('text', redraw)
fbind('duration', redraw)
fbind('font_name', redraw)
fbind('font_size', redraw)
fbind('bold', redraw)
fbind('italic', redraw)
fbind('padding', redraw)
def on_x(self, *args):
self.x_original = self.x
Clock.schedule_once(self.redraw)
def redraw(self, *args):
if self.x_original is None:
return
if self.text == '':
self.texture = None
return
label = CoreLabel(text=self.text, font_name=self.font_name, font_size=self.font_size,
bold=self.bold, italic=self.italic, padding=self.padding)
label.refresh()
self.texture = label.texture
Clock.schedule_once(self.do_anim)
def do_anim(self, *args):
if self.anim is not None:
self.anim.cancel(self.ids.label)
self.anim = None
self.ids.label.x = self.x_original
x_end = self.ids.label.x - self.ids.label.width
self.anim = Animation(x=x_end, duration=self.duration)
self.anim.bind(on_complete=self.do_anim)
self.anim.start(self.ids.label)
if __name__ == "__main__":
from kivy.app import App
class TestApp(App):
def build(self):
return Builder.load_string('''
FloatLayout:
Marquee:
text: 'This is a long text for testing the Marquee widget.'
font_name: 'DejaVuSans'
duration: 7
padding: 10
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size_hint: None, None
size: 100, 50
''')
TestApp().run()
No guarantees.
I am a newbie to Kivy and I want to know how can we create the circular or rounded button using Kivy (with and without .kv file).
There are many ways to create a circular Button. Here is just one way:
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.floatlayout import FloatLayout
class CircularButton(FloatLayout):
button = ObjectProperty()
def __init__(self, **kwargs):
super(CircularButton, self).__init__()
# all size and position attributes get assigned to the CircularButton
# the below is not an exhaustive list
if 'size_hint' in kwargs:
self.size_hint = kwargs.pop('size_hint', (1,1))
if 'size' in kwargs:
self.size = kwargs.pop('size', (100, 100))
if 'width' in kwargs:
self.width = kwargs.pop('width', 100)
if 'height' in kwargs:
self.height = kwargs.pop('height', 100)
if 'pos_hint' in kwargs:
self.pos_hint = kwargs.pop('pos_hint', (None, None))
if 'pos' in kwargs:
self.pos = kwargs.pop('pos', (0,0))
if 'x' in kwargs:
self.x = kwargs.pop('x', 0)
if 'y' in kwargs:
self.y = kwargs.pop('y', 0)
# remaining args get applied to the Button
self.butt_args = kwargs
Clock.schedule_once(self.set_button_attrs)
def set_button_attrs(self, dt):
for k,v in self.butt_args.items():
setattr(self.button, k, v)
Builder.load_string('''
<CircularButton>:
button: button
canvas.before:
StencilPush
Ellipse:
# self.pos and self.size should work here
# adjustments are just hacks to get it to look right
pos: self.x+1, self.y+2
size: self.width-2, self.height-2
StencilUse
canvas.after:
StencilUnUse
Ellipse:
pos: self.x+1, self.y+2
size: self.width-2, self.height-2
StencilPop
Button:
id: button
pos_hint: {'center_x':0.5, 'center_y':0.5}
size_hint: None, None
size: root.size
''')
if __name__ == '__main__':
from kivy.app import App
class TestApp(App):
def build(self):
return CircularButton(text='Circular Button', size_hint=(None, None), size=(150, 150), pos_hint={'right':1, 'top':1}, on_release=self.butt)
def butt(self, *args):
print('button pressed')
TestApp().run()
I have been trying to create a kivy camera scanner from a number of sources (I would use the zbarcam if I could, but the garden.xcamera module will not import, therefore I am trying to create something similar).
Problem
The problem is the camera does not read or update the texture continuously nor is there a way that I can find to capture frame-for-frame from the camera. This means I only get the texture on initialization.
Tried
Firstly, I have tried scheduling an event that will update the texture instance every 0.5 seconds. I could not get the texture instance of the camera because there is some delay in the camera to load, which caused an error.
Secondly, I created an on_texture event in my kv string, but it only reads the texture on initialization.
Thirdly, I tried binding the on_texture event a bit later in the python script, by creating a binding function and calling it as a scheduled event. It did not even get the instance.
Fourthly, I created_triggers and ask_update(callbacks) to the _on_texture() event, but again the script loads to fast before the camera can instantiate crashing the script.
Fifthly, I noticed there is a kivy.core.video module that contains a on_frame attribute. Did re-write my script to use it in conjunction with the kivy.uix.video module, but noticed that the video cannot run without first loading a video file.
Code
import kivy
import gi
kivy.require('1.11.1')
gi.require_version('Gst', '1.0')
from collections import namedtuple
from PIL import Image
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty, ObjectProperty
from kivy.uix.camera import Camera
import time
from gi.repository import Gst
import pyzbar.pyzbar
from kivy.uix.modalview import ModalView
Builder.load_string('''
#: import Window kivy.core.window.Window
<ScanPreview>:
auto_dismiss: False
size_hint_x: 0.6
size_hint_y: None
height: Window.height / 9
pos_hint: {'top':0.7, 'x': 0.1}
background_normal: ''
background_color: (1, 1, 1, 0)
background: 'white.png'
Label:
id: sc_data
text: 'See me...'
<ScanCamera>:
orientation: 'vertical'
The_Camera:
id: camera
resolution: root.resolution
on_texture: root._on_texture(camera)
ToggleButton:
text: 'Stop'
on_press: camera.play = not camera.play
size_hint_y: None
height: '48dp'
''')
class ScanPreview(ModalView):
pass
class The_Camera(Camera):
pass
class ScanCamera(BoxLayout):
resolution = ListProperty([640, 480])
symbols = ListProperty([])
code_types = ListProperty(set(pyzbar.pyzbar.ZBarSymbol))
cam_cam = ObjectProperty(The_Camera())
the_preview = ObjectProperty(ScanPreview())
Symb = namedtuple('Symb', ['type','data'])
def __init__(self, **kwargs):
super(ScanCamera, self).__init__(**kwargs)
self.cam_cam.play = True
def _on_texture(self, instance):
#source: https://github.com/kivy-garden/garden.zbarcam/blob/develop
#/zbarcam/zbarcam.py
print(instance)
if not instance.texture == None:
print(instance.texture)
self.symbols = self._detect_qrcode_frame(
texture=instance.texture, code_types=self.code_types)
def _detect_qrcode_frame(cls, texture, code_types):
image_data = texture.pixels
size = texture.size
#source: https://github.com/kivy-garden/garden.zbarcam/blob/develop
#/zbarcam/zbarcam.py
# Fix for mode mismatch between texture.colorfmt and data returned
#by
# texture.pixels. texture.pixels always returns RGBA, so that
#should
# be passed to PIL no matter what texture.colorfmt returns. refs:
# https://github.com/AndreMiras/garden.zbarcam/issues/41
pil_image = Image.frombytes(mode='RGBA', size=size,
data=image_data)
symbols = []
print(pil_image)
print(size)
print(texture.tex_coords)
print(texture.target)
codes = pyzbar.pyzbar.decode(pil_image, symbols=code_types)
for code in codes:
symbol = CameraClick.Symb(type=code.type, data=code.data)
symbols.append(symbol)
print(symbols)
return symbols
class TestCamera(App):
title = 'Scan Camera'
def build(self):
return ScanCamera()
def on_stop(self):
cc = The_Camera()
print('Stop')
cc.play = False
def on_pause(self):
return True
def on_resume(self):
pass
TestCamera().run()
Desired result
The camera's texture must continuously update, which will allow the pyzbar and PIL module to decode the texture?
I don't know if this is how its done, but as I answered my own question I am posting the answer and marking it as such.
Answer
So I managed to solve my question by using the example code here: https://kivy-fork.readthedocs.io/en/latest/_modules/kivy/uix/camera.html and mixing in some functions of zbarcam.
By using self.canvas.ask_update() in the on_texture call it updates the texture. I have added to the code and it now updates the texture continuously and prints the barcode to a togglebutton. At the moment I have just tested it on Ubuntu Bionic Beaver. Will test it on android this weekend.
Answer code
import kivy
import gi
kivy.require('1.11.1')
gi.require_version('Gst', '1.0')
from collections import namedtuple
from PIL import Image as Img
from kivy.app import App
from gi.repository import Gst
import pyzbar.pyzbar
from kivy.uix.image import Image
from kivy.core.camera import Camera as CoreCamera
from kivy.properties import NumericProperty, ListProperty, \
BooleanProperty, ObjectProperty
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
Builder.load_string('''
#: import Window kivy.core.window.Window
<ScanCamera>:
<BarcWin>:
ActionBar:
pos_hint: {'top': 1, 'right': 1}
color: (1,1,1,1)
canvas.before:
Color:
rgba: (0,0,0,1)
Rectangle:
pos: self.pos
size: self.size
ActionView:
use_separator: True
ActionPrevious:
title: ''
with_previous: False
app_icon: ''
ActionButton:
color: (0,0,0,1)
background_normal: ''
Image:
source: 'gear_2.png'
center_y: self.parent.center_y
center_x: self.parent.center_x
size: self.parent.width /1.7, self.parent.height/ 1.7
allow_stretch: True
ActionButton:
color: (0,0,0,1)
size_hint_x: 0.09
background_normal: ''
Image:
source: 'dustbin_backgrnd_792521.png'
center_y: self.parent.center_y
center_x: self.parent.center_x
size: self.parent.width /1.7, self.parent.height/ 1.7
allow_stretch: True
ScanCamera:
pos_hint: {'top': 0.9, 'right': 1}
size_hint: [1, 0.8]
canvas.before:
PushMatrix
Rotate:
angle: 0
origin: self.center
canvas.after:
PopMatrix
Line:
width: 2.
rectangle: (self.x + 40, self.y + 40, self.width/1.1, self.height/1.12)
ToggleButton:
id: show_bcode
pos_hint: {'bottom': 1, 'right': 1}
size_hint: [1, 0.1]
color: (1,1,1,1)
background_color: (0,0,0,0)
background_normal: ''
canvas.before:
Color:
rgba: (.18,.36,.61,1) if self.state=='down' else (0,0,0,1)
Rectangle:
pos: self.pos
size: self.size
text: 'Hier kom die barcode...'
''')
class BarcWin(FloatLayout):
cam_cam = ObjectProperty(None)
def __init__(self, **kwargs):
super(BarcWin, self).__init__(**kwargs)
self.cam_cam = ScanCamera()
def accept_in(self):
print('In')
def accept_out(self):
print('Out')
class ScanCamera(Image):
play = BooleanProperty(True)
index = NumericProperty(-1)
resolution = ListProperty([Window.width, Window.height])
symbols = ListProperty([])
code_types = ListProperty(set(pyzbar.pyzbar.ZBarSymbol))
Symb = namedtuple('Symb', ['type','data'])
app_ini_ = ObjectProperty(None)
got_bcode = BooleanProperty(False)
def __init__(self, **kwargs):
self._camera = None
super(ScanCamera, self).__init__(**kwargs)
if self.index == -1:
self.index = 0
on_index = self._on_index
fbind = self.fbind
fbind('index', on_index)
fbind('resolution', on_index)
on_index()
self.app_ini_ = App.get_running_app()
def on_tex(self, *l):
self.canvas.ask_update()
if not self.texture == None:
self.symbols = self._detect_qrcode_frame(texture=self.texture, code_types=self.code_types)
if not self.symbols == []:
for s in self.symbols:
if s.data:
if s.data.decode('utf-8') != "":
self.app_ini_.root.ids.show_bcode.text = s.data.decode('utf-8')
def _on_index(self, *largs):
self._camera = None
if self.index < 0:
return
if self.resolution[0] < 0 or self.resolution[1] < 0:
return
#first init of corecamera object
self._camera = CoreCamera(index=self.index,
resolution=self.resolution, stopped=True)
#when camera loads call _camera_loaded method to bind corecamera method with uix.image texture
self._camera.bind(on_load=self._camera_loaded)
if self.play:
self._camera.start()
self._camera.bind(on_texture=self.on_tex)
def _camera_loaded(self, *largs):
#bind camera texture with uix.image texture that is still equal to None
self.texture = self._camera.texture
def on_play(self, instance, value):
if not self._camera:
return
if value:
self._camera.start()
else:
self._camera.stop()
def _detect_qrcode_frame(self, texture, code_types):
if not self.got_bcode:
image_data = texture.pixels
size = texture.size
# Fix for mode mismatch between texture.colorfmt and data returned by
# texture.pixels. texture.pixels always returns RGBA, so that should
# be passed to PIL no matter what texture.colorfmt returns. refs:
# https://github.com/AndreMiras/garden.zbarcam/issues/41
pil_image = Img.frombytes(mode='RGBA', size=size,
data=image_data)
bcode = []
codes = pyzbar.pyzbar.decode(pil_image, symbols=code_types)
#print(pil_image, type(pil_image), dir(pil_image))
if codes != []:
for code in codes:
symbol = self.Symb(type=code.type, data=code.data)
bcode.append(symbol)
return bcode
else:
self.got_bcode = False
return []
class TestCamera(App):
title = 'Scan Camera'
def build(self):
return BarcWin()
def on_stop(self):
cc = ScanCamera()
print('Stop')
cc._camera.stop()
def on_pause(self):
return True
def on_resume(self):
pass
TestCamera().run()
I would like to give all of the widgets on the screen a white canvas. I know how to create the intended canvas in KV but in this case I must use Python code.
In my code, I tried self.cavas.add(...) and in the code below, I use with self.canvas:. Both attempts resulted in a canvas being drawn in the corner of the screen, rather than inside of the widgets.
How do I put a canvas to be inside of every widget using Python code?
Code:
from kivy.app import App
from kivy.core.window import Window
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Rectangle
from kivy.lang import Builder
from random import random
from kivy.uix.button import Button
from kivy.animation import Animation
from kivy.clock import Clock
class LittleButtons(Button):
dur = 2
def reup(self, *args):
Animation.cancel_all(self)
Animation(pos_hint = {'center_x': random(), 'center_y': random()}, duration = self.dur).start(self)
def __init__(self, **kwargs):
super(LittleButtons, self).__init__(**kwargs)
self.pos_hint = {'center_x': random(), 'center_y': random()}
self.size_hint = None, None
self.width = random() * (Window.width / 20)
self.height = self.width
self.background_color = [0,0,0,.05]
with self.canvas:
Color(rgba = [1,1,1,.2])
Rectangle(pos = self.pos, size = self.size)
Animation(pos_hint = {'center_x': random(), 'center_y': random()}, duration = self.dur).start(self)
Clock.schedule_interval(self.reup, self.dur)
KV = Builder.load_string("""
#:import Factory kivy.factory.Factory
Screen:
FloatLayout:
on_parent:
(lambda ltext: [self.add_widget(Factory.LittleButtons(text=ltext)) for i in range (150)])('hi!')
Button:
background_color: 0,0,0,0
canvas:
Color:
rgba: 0,1,1,1
Rectangle:
pos: self.pos
size:self.size
""")
class MyApp(App):
def build(self):
return KV
if __name__ == '__main__':
MyApp().run()
The problem is that in the LittleButtons __init__() method, its position is still the default of (0,0), so the position of the Rectangle is set to (0,0). When you use KV, it cleverly binds the Rectangle position to the LittleButtons position when you reference self.pos. Unfortunately, in a .py file, you must provide that binding yourself. So, here is a modification of your LittleButtons that should handle the position changes:
class LittleButtons(Button):
dur = 2
def reup(self, *args):
Animation.cancel_all(self)
Animation(pos_hint = {'center_x': random(), 'center_y': random()}, duration = self.dur).start(self)
def __init__(self, **kwargs):
self.rect = None
super(LittleButtons, self).__init__(**kwargs)
self.pos_hint = {'center_x': random(), 'center_y': random()}
self.size_hint = None, None
self.width = random() * (Window.width / 20)
self.height = self.width
self.background_color = [0,0,0,.05]
with self.canvas:
Color(rgba = [1,1,1,.2])
self.rect = Rectangle(pos = self.pos, size = self.size)
Animation(pos_hint = {'center_x': random(), 'center_y': random()}, duration = self.dur).start(self)
Clock.schedule_interval(self.reup, self.dur)
def on_pos(self, instance, pos):
if self.rect is not None:
self.rect.pos = pos
The changes are the addition of a self.rect attribute and a on_pos method.
You can add something similar if you need to handle changing size.
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()