kivy camera application with opencv in android shows black screen

207 views
Skip to first unread message

Aswin A

unread,
Apr 24, 2020, 3:23:03 AM4/24/20
to Kivy users support
I'm trying to build an camera Android app based on OpenCv in Kivy:

My main.py is;
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.camera import Camera
import cv2
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import numpy as np

class KivyCamera(Image):
    def __init__(self, capture, fps, **kwargs):
        super(KivyCamera, self).__init__(**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

class MainApp(App):
    def build(self):
        self.capture = cv2.VideoCapture(0)
        self.camera = KivyCamera(capture=self.capture, fps=30)
        return self.camera

if __name__== "__main__":
    MainApp().run()
And my buildozer.spec is;
[app]
title = Test_app
package.name = myapp
package.domain = org.test
source.dir = .
source.include_exts = py,png,jpg,kv,atlas,xml
version = 0.1
requirements = python3,kivy,numpy,opencv
orientation = portrait

# Android specific
fullscreen = 0
android.permissions = INTERNET, ACCESS_FINE_LOCATION, WRITE_EXTERNAL_STORAGE, CAMERA
android.arch = armeabi-v7a
[buildozer]
log_level = 2
warn_on_root = 1

Code works successfully in windows. Then i have build the the code with buildozer for android, when I open the Android App it shows a black screen with a small square in the left corner of the screen. I think the cv2.VideoCapture() is not working properly.So I change cv2.VideoCapture(0) to cv2.VideoCapture(-1) and to cv2.VideoCapture(1). But both doesn't work.

Can anyone help me out with this ?

Robert Flatt

unread,
Apr 24, 2020, 12:24:24 PM4/24/20
to Kivy users support

Aswin A

unread,
Apr 24, 2020, 12:36:21 PM4/24/20
to kivy-...@googlegroups.com
Hi Robert,
Thank you for your reply 
I am still confused with my code. 
Do you find any missing information in my code ? Does the kivy support opencv camera? 

--
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/e60ee916-a47c-49da-b080-a49b390eb51e%40googlegroups.com.

Robert Flatt

unread,
Apr 24, 2020, 3:22:18 PM4/24/20
to Kivy users support
I didn't read your code, because as I posted this in the thread I referenced:

As shown by, this is always the case:
    video_capture.isOpened()  == False
 
Why this is the case is unclear to me.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Aswin A

unread,
Apr 24, 2020, 11:23:11 PM4/24/20
to kivy-...@googlegroups.com
Hi Robert,
Thank you again for the quick reply
My problem is, opencv can not read the camera device properly.
I couldn't find any proper document about the topic opencv on kivy.
Can anyone help me with this? 

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/45515057-0004-4cca-8294-13692d061fd2%40googlegroups.com.

Robert Flatt

unread,
Apr 25, 2020, 12:25:44 AM4/25/20
to Kivy users support
My problem is, opencv can not read the camera device properly.
 
Yes that is right because

As shown by, this is always the case:
    video_capture.isOpened()  == False

To find an approach that works read the thread I previously referenced.
Reply all
Reply to author
Forward
0 new messages