本文介绍了当从命令提示符运行脚本时,不能将PiCamera初始化为类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的树莓派,我遇到一个奇怪的行为,使用PiCamera模块。

on my Raspberry Pi, I encounter a strange behaviour regarding the use of the PiCamera module.

当从IDLE(F5)或从命令提示符($ python test.py)启动时,以下代码运行顺畅

The following code runs smoothly when either started from IDLE (F5) or from the command prompt ($python test.py)

import picamera

if __name__ == "__main__":
    camera=picamera.PiCamera()
    camera.close()

但是当我把camera对象放到类中时,代码将只在从IDLE (F5):

But when I put the camera object into a class the code will run only when started from IDLE (F5):

import picamera

class VF:
    def __init__(self):
        self.camera = picamera.PiCamera()

    def __del__(self):
        self.camera.close()


if __name__ == "__main__":
    myvf = VF()

从命令提示符上面的代码,我得到以下错误消息:

When I start the above code from the command prompt, I get the following error message:

跟踪(最近一次调用):文件test.py,第14行,在

myvf = VF()

Traceback (most recent call last): File "test.py", line 14, in myvf = VF()

init 中的文件test.py第6行:
self.camera = picamera.PiCamera()

File "test.py", line 6, in init self.camera = picamera.PiCamera()

文件/usr/lib/python2.7/dist-packages/picamera/camera.py,行
379,在 init
camera_num中, self.STEREO_MODES [stereo_mode],stereo_decimate)

File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 379, in init camera_num, self.STEREO_MODES[stereo_mode], stereo_decimate)

文件/usr/lib/python2.7/dist-packages/picamera/camera.py,行
505,in _init_camera
prefix =Camera component could not be enabled)

File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 505, in _init_camera prefix="Camera component couldn't be enabled")

文件/usr/lib/python2.7/dist-packages /picamera/exc.py,第133行,
in mmal_check
raise PiCameraMMALError(status,prefix)

File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 133, in mmal_check raise PiCameraMMALError(status, prefix)

picamera.exc.PiCameraMMALError:Camera组件无法启用:
缺少资源(内存除外)

picamera.exc.PiCameraMMALError: Camera component couldn't be enabled: Out of resources (other than memory)

相机模块工作正常,I只是把代码下降到最小的可能的大小。有人知道这个问题,或类似的问题,并可能提供一个解决方案? Python版本是2.7,Raspberry Rasbiab系统是完全最新的。
提前感谢。

The camera module is working correct, I just stripped the code down to the least possible size. Does anybody know this problem, or a similar problem, and can probably provide a solution? The Python Version is 2.7 and the Raspberry Rasbiab-System is completely up to date.Thanks in advance.

推荐答案

发现,当析构函数是没有明确调用(LED已关闭,因此没有看到这个)。

Found out, that the camera-module is not properly shut down when the destructor is not explicitly called (had LED turned off, so didn't see this).

IDLE通过某种方式在脚本启动之前重置它,而不是python解释器。

IDLE handles a running camera by somehow resetting it before the script starts, but not the python interpreter.

现在当脚本结束之前调用析构函数时一切正常。

So everything is ok now when the destructor is called before the script ends.

这篇关于当从命令提示符运行脚本时,不能将PiCamera初始化为类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 07:53