Here you go:
You were deriving Cam from Camera, it is a better fit to derive from Image and use the texture of Image for the video data. The build() method is a specail method of app. It is not a widget method. I used on_kv_post to run widget startup code. On_kv_post is a widget method that is run after the kv for the widget is processed.
import cv2 as cv
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.graphics.texture import Texture
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.screenmanager import Screen
from kivy.uix.widget import Widget
kv = """
#:kivy 1.11.0
<MenuScreen>:
MainWidget:
<SettingsScreen>:
BoxLayout:
Button:
text: 'My settings button'
Cam:
id: cam1
Button:
text: 'Back to menu'
on_press: root.manager.current = 'settings'
ScreenManager:
MenuScreen:
name: 'menu'
SettingsScreen:
name: 'settings'
"""
class MainWidget(Widget):
def __init__(self, **kwargs):
super(MainWidget, self).__init__(**kwargs)
print("\nmainwidget:")
btnnext = Button(text='go to next', pos=(200, 400))
btnnext.bind(on_press=self.gonext)
self.add_widget(btnnext)
# def savecard(self, btn_instance):
def gonext(self, btn_inst):
app = App.get_running_app()
app.root.current = "settings"
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
class Cam(Image):
def on_kv_post(self, base_widget):
self.capture = cv.VideoCapture(0)
# cv.namedWindow("CV2 Image")
Clock.schedule_interval(self.update, 1.0 / 33.0)
def update(self, dt):
# display image from cam in opencv window
ret, frame = self.capture.read()
# cv.imshow("CV2 Image", frame)
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# convert it to texture
adaptive_thresh = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 3)
buf1 = cv.flip(adaptive_thresh, 0)
buf = buf1.tobytes()
texture1 = Texture.create(size=(adaptive_thresh.shape[1], adaptive_thresh.shape[0]),
colorfmt='luminance') # in grayscale gibts kein bgr
# if working on RASPBERRY PI, use colorfmt='rgba' here instead, but stick with "bgr" in blit_buffer.
texture1.blit_buffer(buf, colorfmt='luminance', bufferfmt='ubyte') # replacing texture
# display image from the texture
self.texture = texture1
class TestApp(App):
def build(self):
Window.clearcolor = (0, 0, 0.3, 1)
return Builder.load_string(kv)
if __name__ == '__main__':
TestApp().run()
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/5e40cbfc-c128-439d-bf81-25fc8f9b48bcn%40googlegroups.com.
On Nov 7, 2021, at 9:40 AM, ShibaInu <playin...@gmail.com> wrote:
@ElliotG
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/fa92d1e6-0e71-4344-a955-9aae7c7beb39n%40googlegroups.com.