Skip to content

Instantly share code, notes, and snippets.

@ExpandOcean
Created January 7, 2015 06:48
Show Gist options
  • Star 72 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save ExpandOcean/de261e66949009f44ad2 to your computer and use it in GitHub Desktop.
Save ExpandOcean/de261e66949009f44ad2 to your computer and use it in GitHub Desktop.
kivy and opencv work together demo
# coding:utf-8
from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2
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 CamApp(App):
def build(self):
self.capture = cv2.VideoCapture(1)
self.my_camera = KivyCamera(capture=self.capture, fps=30)
return self.my_camera
def on_stop(self):
#without this, app will not exit even if the window is closed
self.capture.release()
if __name__ == '__main__':
CamApp().run()
@pankaj-pundir
Copy link

Thanks it helped a lot ..

@da0c
Copy link

da0c commented Sep 1, 2018

Thank you for this amazing OpenCV example!
However, I have an issue for new android versions.
I got the following error when starting the .apk on Android 8.0:

Detected problems with app native libraries
(please consult log to details):
Libnative_camera_r4.4.0.so: unauthorized access to "libutils.so"

On my mind, the cause is security options of new Android, but I couldn't find workaround yet.
Did anyone solve similar problem?

@jordybayo
Copy link

thank you bro

@luigipacheco
Copy link

It works by itself but i have no clue on how to add it to a layout im not sure what im doing wrong

@bahaeelhmimdi
Copy link

@da0c after connecting your phone (wich is in debugging mode) to the computer your computer you will get notification on your phone there you must accept the connection

@rafaelscariot
Copy link

rafaelscariot commented Dec 19, 2019

after building this code with buildozer for android, when I open the App it shows a black screen with a small square in the left corner of the screen. Anyone can help me? I believe the camera is not being accessed. In buildozer.spec I gave the App permission to access the camera.

@garoskull8
Copy link

@rafaelscariot have u found the solution?

@zy890p
Copy link

zy890p commented Mar 5, 2020

after building this code with buildozer for android, when I open the App it shows a black screen with a small square in the left corner of the screen. Anyone can help me? I believe the camera is not being accessed. In buildozer.spec I gave the App permission to access the camera.

I have the same problem when I first tried the example. I then change cv2.VideoCapture(1) to cv2.VideoCapture(0) and it works.
If it doesn't, try cv2.VideoCapture(-1). Hope this helps.

@eva-ave
Copy link

eva-ave commented Mar 13, 2020

after building this code with buildozer for android, when I open the App it shows a black screen with a small square in the left corner of the screen. Anyone can help me? I believe the camera is not being accessed. In buildozer.spec I gave the App permission to access the camera.

I have the same problem when I first tried the example. I then change cv2.VideoCapture(1) to cv2.VideoCapture(0) and it works.
If it doesn't, try cv2.VideoCapture(-1). Hope this helps.

Nope, I tried both 0 and -1, but doesn't work

@Amreshhub
Copy link

can we run thin on mac
actually its coming blank window when i m running in vscode on mac ios

@FranMartiarena
Copy link

after building this code with buildozer for android, when I open the App it shows a black screen with a small square in the left corner of the screen. Anyone can help me? I believe the camera is not being accessed. In buildozer.spec I gave the App permission to access the camera.

I have the same problem when I first tried the example. I then change cv2.VideoCapture(1) to cv2.VideoCapture(0) and it works.
If it doesn't, try cv2.VideoCapture(-1). Hope this helps.

Nope, I tried both 0 and -1, but doesn't work
Hi! could you solve the problem?...I have the same issue

@lakshit77
Copy link

did anyone found the solution for black screen.?

@DunZek
Copy link

DunZek commented Jul 3, 2020

Here's my modification on it with just a few lines of code added, which works seamlessly from the original. Now the video feed is used to scan faces.

You can download the haarcascades here:
https://www.murtazahassan.com/wp-content/uploads/2020/03/haarcascades.zip

Place your .py and that folder in the same location and just edit the source destination in cv2.CascadeClassifer()

`
# coding:utf-8
from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2

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):
        faceCascade = cv2.CascadeClassifier("Resources/haarcascades/haarcascade_frontalface_default.xml")  # added

        ret, frame = self.capture.read()
        if ret:

            frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # added
            faces = faceCascade.detectMultiScale(frameGray, 1.1, 4)  # added

            for (x, y, w, h) in faces:  # added
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)  # added

            # 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 CamApp(App):
    def build(self):
        self.capture = cv2.VideoCapture(0)
        self.my_camera = KivyCamera(capture=self.capture, fps=30)
        return self.my_camera

    def on_stop(self):
        #without this, app will not exit even if the window is closed
        self.capture.release()


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

`

Now time to figure out how to make it work with the Launcher lol

@dineshkidd
Copy link

Here's my modification on it with just a few lines of code added, which works seamlessly from the original. Now the video feed is used to scan faces.

You can download the haarcascades here:
https://www.murtazahassan.com/wp-content/uploads/2020/03/haarcascades.zip

Place your .py and that folder in the same location and just edit the source destination in cv2.CascadeClassifer()

`

coding:utf-8

from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2

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):
        faceCascade = cv2.CascadeClassifier("Resources/haarcascades/haarcascade_frontalface_default.xml")  # added

        ret, frame = self.capture.read()
        if ret:

            frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # added
            faces = faceCascade.detectMultiScale(frameGray, 1.1, 4)  # added

            for (x, y, w, h) in faces:  # added
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)  # added

            # 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 CamApp(App):
    def build(self):
        self.capture = cv2.VideoCapture(0)
        self.my_camera = KivyCamera(capture=self.capture, fps=30)
        return self.my_camera

    def on_stop(self):
        #without this, app will not exit even if the window is closed
        self.capture.release()


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

`

Now time to figure out how to make it work with the Launcher lol

Is it working in android?

@DunZek
Copy link

DunZek commented Aug 12, 2020

Nope. Trying to figure that out though.

@YounGCoder09
Copy link

YounGCoder09 commented Aug 16, 2020

@DunZek

Nope. Trying to figure that out though.

Hi is it working with kivy launcher without buildozer

@JefNtungila
Copy link

after building this code with buildozer for android, when I open the App it shows a black screen with a small square in the left corner of the screen. Anyone can help me? I believe the camera is not being accessed. In buildozer.spec I gave the App permission to access the camera.

I have the same problem when I first tried the example. I then change cv2.VideoCapture(1) to cv2.VideoCapture(0) and it works.
If it doesn't, try cv2.VideoCapture(-1). Hope this helps.

Thanks for that.

@samyxdev
Copy link

Has anyone managed to actually make it work ? I have been trying a lot but without success and it goes the same with opening video files with cv2.VideoCapture.

@lkostakis
Copy link

@DunZek @eva-ave @rafaelscariot @lakshit77
Have you managed solve black screen problem ? I created apk file with buildozer but I have the black screen with the white square in left corner.

@deepaksharma01
Copy link

@DunZek @eva-ave @rafaelscariot @lakshit77
Have you managed solve black screen problem ? I created apk file with buildozer but I have the black screen with the white square in left corner.

I am facing the same problem please help

@sagarhm
Copy link

sagarhm commented May 30, 2021

how to increase capture display frame size

@AmitNikhade
Copy link

AmitNikhade commented Jun 3, 2021

@DunZek I tried your code it gave me a full white screen, I also tried running and debugging in the android studio it does not have any error.. where might be the problem.

@Nagi0
Copy link

Nagi0 commented Jul 19, 2021

@DunZek @eva-ave @rafaelscariot @lakshit77
Have you managed solve black screen problem ? I created apk file with buildozer but I have the black screen with the white square in left corner.

I am facing the same problem please help

This guy got the solution:
https://stackoverflow.com/questions/61122285/kivy-camera-application-with-opencv-in-android-shows-black-screen

@4ndr3aR
Copy link

4ndr3aR commented Nov 25, 2021

I don't know if anyone is still interested in this, but since it took me three days to get there, here is a "minimal" example of OpenCV VideoCapture displaying frame by frame on Android:

https://github.com/4ndr3aR/kivy-opencv-demo

@reesha-ram
Copy link

I don't know if anyone is still interested in this, but since it took me three days to get there, here is a "minimal" example of OpenCV VideoCapture displaying frame by frame on Android:

https://github.com/4ndr3aR/kivy-opencv-demo

Really Great.Helped me a lot

@Nagi0
Copy link

Nagi0 commented Jan 9, 2022

I found this video tutorial about building kivy apps with opencv codes using Buildozer, it worked for me. I've tried using some simple functions of opencv and some other libraries as well and worked like a charm. If you face some errors I recommend using logcat filter to troubleshoot:
https://www.youtube.com/watch?v=c4tuSxSoERY&t=245s
Here is a simple working code using opencv to display the phone's camera image on the screen:
https://stackoverflow.com/questions/61122285/kivy-camera-application-with-opencv-in-android-shows-black-screen

@RichardN25
Copy link

Nope. Trying to figure that out though.

@DunZek have you figured this one out?

@zeroche72
Copy link

zeroche72 commented Feb 14, 2023

Has anyone succeeded in making an Android camera with Python using OpenCv?
Below is the code that succeeded in another way.

-- below --

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import time

from android.permissions import request_permissions, Permission
request_permissions([Permission.CAMERA])

Builder.load_string('''
:
orientation: 'vertical'
Camera:
id: camera
resolution: (1280, 720)
play: False
ToggleButton:
text: 'Play'
on_press: camera.play = not camera.play
size_hint_y: None
height: '48dp'
Button:
text: 'Capture'
size_hint_y: None
height: '48dp'
on_press: root.capture()
''')

class CameraClick(BoxLayout):
def capture(self):
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
print("Captured")

class TestCamera(App):
def build(self):
return CameraClick()

TestCamera().run()

@StarkGang
Copy link

I tried running it on a Raspberry Pi and it runs pretty slow. Do you have any optimizations for that?

Threads?

@ambrosecodes
Copy link

I don't know if anyone is still interested in this, but since it took me three days to get there, here is a "minimal" example of OpenCV VideoCapture displaying frame by frame on Android:

https://github.com/4ndr3aR/kivy-opencv-demo

I couldn't get this to work. The code ran ok but only for one frame after which the Kivy window closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment