OpenCV with Kivy

13,568 views
Skip to first unread message

Richey

unread,
Jul 9, 2013, 9:49:46 AM7/9/13
to kivy-...@googlegroups.com
Has anyone done any serious computer vision applications with Kivy?  I've got a project that I've built in python that uses OpenCV pretty heavily and would like to port it to Android and iOS.  Here is a trivial example:

#!/usr/bin/python

import cv2
import random
from numpy import array

cap = cv2.VideoCapture(0)
while True:
    initial_thresh = random.randint(100,170)
    ret,im = cap.read()
    im_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    key = cv2.waitKey(10)&255
    cv2.imshow('gray',im_gray)

So, I looked at a Kivy example of getting the video stream found here: http://karanbalkar.com/2012/10/tutorial-6-working-with-camera-in-kivy/  This example doesn't show how to use cv2 to process the video stream through. Is this possible in Kivy? Will I be able to port a heavy OpenCV application to Android and iOS using Kivy?

Thanks,
Michael

mathieu....@gmail.com

unread,
Jul 17, 2013, 10:50:42 AM7/17/13
to kivy-...@googlegroups.com
Hi,

I'll be intereted in doing kivy + opencv too ! About your question, I've looked the source code of the camera_opencv.py in the kivy / core / camera folder.
There is the interaction with opencv ( the first python binding ). I don't think it'll be difficult to port it with the cv2 module.

Cheers !!

Mathieu Virbel

unread,
Jul 19, 2013, 5:34:44 AM7/19/13
to kivy-...@googlegroups.com
Hi,

I've done heavy computation using OpenCV within Kivy. No issues, it just works.
But never tried on mobile. Nobody wrote a recipe for android/ios yet, it need a hero here :)

--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jonathan Berte

unread,
Aug 17, 2014, 5:59:46 PM8/17/14
to kivy-...@googlegroups.com
Hi,

Did people write a recipe for opencv on android in the meanwhile?
Bye,
jonathan

Op dinsdag 9 juli 2013 15:49:46 UTC+2 schreef Richey:

Andreas Bunkahle

unread,
Nov 4, 2014, 1:59:27 AM11/4/14
to kivy-...@googlegroups.com
I have a piece of code which gets an image from the OpenCV camera and puts it into a kivy texture to display. This means you have the possibility to do all kind of OpenCV transformations on a picture and put it to a kivy output later. The code looks like this:

__author__ = 'bunkus'
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture

import cv2
import numpy as np

class CamApp(App):

    def build(self):
        self.img1 = Image(source='images/1.jpg')
        layout = BoxLayout()
        layout.add_widget(self.img1)
        #opencv2 stuffs
        self.capture = cv2.VideoCapture(0)
        ret, frame = self.capture.read()
        cv2.namedWindow("CV2 Image")
        cv2.imshow("CV2 Image", frame)
        Clock.schedule_interval(self.update, 1.0/33.0)
        return layout

    def CreateImage(self, (height, width), bits=np.uint8, channels=3, color=(0, 0, 0)): # (cv.GetSize(frame), 8, 3)
        """Create new image(numpy array) filled with certain color in RGB"""
        # Create black blank image
        if bits == 8:
            bits = np.uint8
        elif bits == 32:
            bits = np.float32
        elif bits == 64:
            bits = np.float64
        image = np.zeros((height, width, channels), bits)
        if color != (0, 0, 0):
            # Fill image with color
            image[:] = color
        return image

    def update(self, dt):
        # display image from cam in opencv window
        ret, frame = self.capture.read()
        cv2.imshow("CV2 Image", frame)
        # convert it to texture
        buf1 = cv2.flip(frame, 0)
        buf = buf1.tostring()
        texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
        texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
        # display image from the texture
        self.img1.texture = texture1

if __name__ == '__main__':
    CamApp().run()

My question is:
What is the way vice versa, to get a camera picture from the kivy Cam and transform it to opencv? Has anyone code for this?

Andreas Bunkahle

unread,
Nov 4, 2014, 4:40:24 AM11/4/14
to kivy-...@googlegroups.com
Hi, me again. Finally I found a way to get the buffer of the texture to translate into a numpy array but I still get errors in the display (it is threefold). So obviously there is an error in the numpy transformation. Can anyone help?

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.camera import Camera
from kivy.uix.widget import Widget
from kivy.clock import Clock
import cv2, numpy as np

class Cam(Camera):
    def build(self):
        pass
   
    def get_texture(self):
        # print "Texture", self.texture
        return self.texture
   
class CameraApp(App):
    def build(self):
        s = 100
        size = (s, s)
        sh = (None, None)
        self.snapshot = None
        root = Widget()
        self.width = 640
        self.height = 480
        self.channels = 3
        self.camera = Cam(resolution=(self.width, self.height), size=(self.width, self.height), pos=(100,100), play=True)
        root.add_widget(self.camera)
        buttons = BoxLayout(orientation='horizontal')
        sn = Button(text='snapshot', pos=(self.width/2, 20), halign='center', size_hint=sh, size=size)
        sn.bind(on_press=self.get_texture1)
        buttons.add_widget(sn)
        root.add_widget(buttons)
        # print "Texture", self.camera.get_texture, type(self.camera.get_texture), dir(self.camera.get_texture)
        Clock.schedule_interval(self.get_texture1, 1.0/33.0)
        return root

    def get_texture1(self, event):
        self.snapshot = self.camera.get_texture()
        # print "self.snapshot", self.snapshot, type(self.snapshot), dir(self.snapshot)
        self.reg = self.snapshot.get_region(0, 0, 50, 50)
        # print "self.reg.pixels", type(self.reg.pixels)
        # print self.reg, type(self.reg), dir(self.reg)
        frame = np.fromstring(self.snapshot.pixels,
                dtype=np.uint8,
                count=self.width*self.height*self.channels)
        frame = frame.reshape(self.height, self.width, self.channels)
        # print dir(frame)
        # print type(frame)
        cv2.imshow("Cv2", cv2.flip(frame, 0))
    
if __name__ == '__main__':
    CameraApp().run()


llo...@gmail.com

unread,
Jan 3, 2015, 10:37:37 AM1/3/15
to kivy-...@googlegroups.com
Easy stuff for opencv proffies

first you have to change the kivi texture to a string,
then you must create a numpy array from this string,
then you must reshape the array to a (R,G,B, Alpha) array

just check out this piece of code :

        nparr = np.fromstring(self.cam.texture.pixels, dtype=np.uint8)
        a = np.reshape(nparr, (480,640,4))
        #a = cv2.cvtColor(a, cv2.cv.CV_RGB2GRAY)
        cv2.imshow("test", a)


Greetings from Poland :)

Danylo Ulianych

unread,
Oct 23, 2015, 2:33:46 PM10/23/15
to Kivy users support
@Andreas Bunkahle,
Thank you for sharing how to display OpenCV VideoCapture on Kivy! I'd only add

def close_settings(self, *largs):
Clock.unschedule(self.update)
del self.capture
App.close_settings(self, *largs)

to override close_setting() to release properly VideoCapture after hitting an escape button (without managing so it continues running with a message "Leaving application in progress...")

Do you know how to set Window size initially in order to fit in with VideoCapture size (there is a black margin between capture frame and a window rectangle during displaying)? I found only one way to do such a thing: add

h = int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
w = int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH))
Config.set('graphics', 'width', str(w))
Config.set('graphics', 'height', str(h))

before calling super().__init__(**kwards) in your CamApp.__init__(**kwards). But that's a bit nasty and I think there should be more elegant way to set a window size without changing default system window size.

rhavel salviano

unread,
Feb 17, 2016, 3:39:36 PM2/17/16
to Kivy users support
Hello guys, I'm studying Kivy and made some changes to a code posted here. I wonder why the modified code does not display the camera?

from kivy.app import App, Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
 
import cv2
import numpy as np

class test(Widget):
      
    def teste(self):      
        self.img1 = Image(source='images/1.jpg')
        layout =Widget()
        layout.add_widget(self.img1)
        #opencv2 stuffs
        self.capture = cv2.VideoCapture(0)
        ret, frame = self.capture.read()
        #cv2.namedWindow("CV2 Image")
        #cv2.imshow("CV2 Image", frame)
        Clock.schedule_interval(self.update, 1.0/33.0)
        return layout

    def update(self, dt):
        # display image from cam in opencv window
        ret, frame = self.capture.read()
        cv2.imshow("CV2 Image", frame)
        # convert it to texture
        buf1 = cv2.flip(frame, 0)
        buf = buf1.tostring()
        texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
        texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
        # display image from the texture
        self.img1.texture = texture1


class CamApp(App):
    def build(self):
        return test()

arnav goel

unread,
Jul 22, 2016, 10:39:51 AM7/22/16
to Kivy users support

Hello I have a kivy file and a python script. I would like to put the opencv video in the kivy file. Is there any way to do so . Also I tried using the camera widget but it says the resolution not supported. Whole problem on http://stackoverflow.com/questions/38446292/kivy-error-camera-webcam-gives-error-videocaptureresolution-not-found
If there is any way I could incorporate opencv video in my kivy application, It would be great. Thanks in advance. 

Eugene Bartosh

unread,
Oct 5, 2017, 4:28:37 PM10/5/17
to kivy-...@googlegroups.com
They support OpenCV 2.x only, no OpenCV 3.x support - 2.x is too old, that hardly makes sense (e.g. I use CLAHE in most of my projects which is missing in 2.x)

Mayank Dhiman

unread,
Aug 9, 2020, 6:44:12 AM8/9/20
to Kivy users support
hey andreas... were you able to solve this problem?
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages