Kivy and OpenCV

190 views
Skip to first unread message

ShibaInu

unread,
Nov 7, 2021, 10:18:46 AM11/7/21
to Kivy users support
Hello everybody,

so I am currently working with kivy and openCV. 

And i dont exactly understand how to include my video filter into my app. 

The KivyCamera Class ignores my opencv code I guess

#MYPOST#



Anyone could help me out? 

Thank you.




Elliot Garbus

unread,
Nov 7, 2021, 11:26:25 AM11/7/21
to kivy-...@googlegroups.com

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.

 

ShibaInu

unread,
Nov 7, 2021, 11:39:05 AM11/7/21
to Kivy users support
 @ElliotG

Thank you so much <3. You really helped me alot. And you explained it very well. 

Can i donate you like 10 Euros? Or something idk. 

Im glad ppl like you are here. Wish you just the best.

And if i can send you some cash just say it

Elliot Garbus

unread,
Nov 7, 2021, 11:42:39 AM11/7/21
to kivy-...@googlegroups.com
No cash required. Make a donation to your favorite charity. 

Happy to help. 😎

Sent from my iPhone

On Nov 7, 2021, at 9:40 AM, ShibaInu <playin...@gmail.com> wrote:

 @ElliotG

ShibaInu

unread,
Nov 7, 2021, 12:07:46 PM11/7/21
to Kivy users support
Thats sweet. Ill donate here https://www.wwf.de/spenden-helfen/allgemeine-spende   for us haha. Thank you again my friend. 

ShibaInu

unread,
Nov 7, 2021, 12:11:49 PM11/7/21
to Kivy users support
Donation.PNG
Reply all
Reply to author
Forward
0 new messages