-
-
Save ExpandOcean/de261e66949009f44ad2 to your computer and use it in GitHub Desktop.
# 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() |
how to increase capture display frame size
@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.
@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
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:
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:
Really Great.Helped me a lot
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
Nope. Trying to figure that out though.
@DunZek have you figured this one out?
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()
I tried running it on a Raspberry Pi and it runs pretty slow. Do you have any optimizations for that?
Threads?
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:
I couldn't get this to work. The code ran ok but only for one frame after which the Kivy window closed.
I am facing the same problem please help