【问题标题】:OPENCV+KIVY+BUILDOZER ON ANDROID PHONE安卓手机上的 OPENCV+KIVY+BUILDOZER
【发布时间】:2021-02-05 08:46:35
【问题描述】:

我正在尝试使用 opencv、kivy 和 buildzer 在我的 android 手机上构建应用程序。我可以成功调试应用程序并构建到 android 手机。但是当我打开应用程序时,相机无法正常工作。 但是在使用 buildozer for android 构建此代码后,当我打开应用程序时,它会显示一个黑屏,屏幕左角有一个小方块。任何人都可以帮助我吗?我相信相机没有被访问。

如果您有任何建议或其他方法,请告诉我。

谢谢!

这是我正在使用的代码:

# 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()

【问题讨论】:

    标签: python android opencv kivy buildozer


    【解决方案1】:

    我也一直在努力解决这个问题。我发现您需要添加几行才能从 android 设备请求权限。这些应该包含在构建中。目前我无法访问自拍摄像头,所以我认为您需要从self.capture = cv2.VideoCapture(1) 中删除 1。

    参考:https://github.com/kivy/buildozer/issues/1004

    # 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
    from android.permissions import request_permissions # Added import
    
    
    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):
            # Give the app permissions to communicate
            request_permissions([Permission.CAMERA,
                                 Permission.READ_EXTERNAL_STORAGE, 
                                 Permission.WRITE_EXTERNAL_STORAGE,])
            self.capture = cv2.VideoCapture() # Removed the 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多