【问题标题】:Kivy Camera free resources issueKivy 相机免费资源问题
【发布时间】:2020-02-15 21:06:31
【问题描述】:

我想在我的 Android 应用中使用相机拍摄照片和扫描条形码。我在kv中添加了它:

<CameraForm>:
    FloatLayout:
        Camera:
            id: camera
            size_hint: (1, 0.8)
            pos_hint: {"center_x": 0.5, "top": 0.95}
            canvas.before:
                PushMatrix
                Rotate:
                    angle: -90 if app.isAndroid() else 0
                    origin: self.center
            canvas.after:
                PopMatrix

        MDRaisedButton:
            text: "Capture"
            size_hint: (None, None)
            height: "40dp"
            pos_hint: {"x": 0.2, "top": 0.1}
            on_press: root.capturePhoto()

还有python代码:

class CameraForm(Screen):
    def __init__(self, *args, **kwargs):
        super(CameraForm, self).__init__(*args, **kwargs)
        self.fileName = None
        self.camera = None

    def initCamera(self):
        self.camera = self.ids.camera
        self.camera.resolution = (720, 480)
        self.camera.keep_ratio = True
        self.camera.play = True
        self.camera.allow_stretch = True

    def on_enter(self, *args):
        self.initCamera()

    def capturePhoto(self):
        imgTime = time.strftime("%Y%m%d_%H%M%S")
        self.fileName = MDApp.get_running_app().imagePath + "IMG_{}.png".format(imgTime)  # store image file
        self.camera.export_to_png(self.fileName)
        msgBox = MessageBox()
        msgBox.showMsg("Information", "Image has been successfully captured!", "OK", False)

相机扫描条形码的类似代码。问题是当我尝试从相机切换到条形码相机时,我遇到了以下问题:

[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (1113) SourceReaderCB::OnReadSample videoio(MSMF): OnReadSample() is called with error status: -1072873821
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (1125) SourceReaderCB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error status: -1072873821
[ WARN:1] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (1159) CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -1072873821
[ERROR  ] [OpenCV      ] Couldn't get image from Camera
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\kivy\core\camera\camera_opencv.py", line 144, in _update
    self._buffer = frame.imageData
AttributeError: 'NoneType' object has no attribute 'imageData'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\kivy\core\camera\camera_opencv.py", line 148, in _update
    self._buffer = frame.reshape(-1)
AttributeError: 'NoneType' object has no attribute 'reshape'
[ WARN:1] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (1159) CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638
[ERROR  ] [OpenCV      ] Couldn't get image from Camera
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\kivy\core\camera\camera_opencv.py", line 144, in _update
    self._buffer = frame.imageData
AttributeError: 'NoneType' object has no attribute 'imageData'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\kivy\core\camera\camera_opencv.py", line 148, in _update
    self._buffer = frame.reshape(-1)
AttributeError: 'NoneType' object has no attribute 'reshape'

应用程序崩溃了。所以,我认为它需要释放相机资源。

def on_leave(self, *args):
    self.camera.stop()

当我尝试离开相机屏幕时,它会报告错误:

AttributeError: 'Camera' object has no attribute 'stop'

但来自 kivy 文档:https://kivy.org/doc/stable/api-kivy.core.camera.html#kivy.core.camera.CameraBase.stop

1.0.0 新增stop() 释放相机

那么,为什么stop() 不适用于Camera?如何释放Camera 资源?谢谢你的帮助。

【问题讨论】:

标签: python android camera kivy


【解决方案1】:

kivy.core.camera.CameraBase 类有一个stop() 方法,但Camera 小部件(在您的.kv 文件中)是kivy.uix.camera.Camera 类的一个实例,它没有stop() 方法。

如果要停止摄像头,可以使用Cameraplay 属性。 Camera's doc.

否则你可以使用self.camera._camera直接访问Camera中的CoreCamera实例(但我不确定是否推荐)。

我建议您查看Camera sources 以获得更好的理解,尤其是102 行和on_play() 的定义。

【讨论】:

  • 您好!我已经尝试了Cameraplay 属性,但它仍然崩溃。我检测到它在self._buffer = frame.reshape(-1) 上崩溃,因为在初始化第二个相机对象时缓冲区溢出。我唯一能做的就是将这些相机功能合并在一起(相机+条形码相机),那么就不会出现这样的问题。
  • 如果你能给出一个最小的、可重现的例子,我可以测试一下,看看我是否找到了什么。
  • 您好!您可以复制我的代码两次并将屏幕类名称更改为其他,或者您可以创建两个相机屏幕并尝试在它们之间切换几次,您会遇到这个问题。谢谢。
  • 我用完全相同的代码创建了两个类CameraFormBareCode(在.py.kv文件中),我尝试在Screen之间切换几次,我没问题,一切正常。我在Debian 10上测试了代码,不知道能不能改变什么。
  • 我正在 Windows 10 上开发它。在 Android 上不存在这样的问题。我合并了相机功能,然后禁用/启用了适当的小部件,它运行良好。谢谢。
猜你喜欢
  • 2015-08-06
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多