从命令提示符运行脚本时,PiCamera 无法初始化为类成员 [英] PiCamera cannot be initialized as a class member when the script is run from command prompt
问题描述
在我的 Raspberry Pi 上,我遇到了关于使用 PiCamera 模块的奇怪行为.
从 IDLE (F5) 或命令提示符 ($python test.py) 启动时,以下代码运行流畅
导入 picamera如果 __name__ == "__main__":相机=picamera.PiCamera()相机.关闭()
但是当我将相机对象放入一个类中时,代码只会在从 IDLE (F5) 启动时运行:
导入 picamera类 VF:def __init__(self):self.camera = picamera.PiCamera()def __del__(self):self.camera.close()如果 __name__ == "__main__":myvf = VF()
当我从命令提示符启动上述代码时,我收到以下错误消息:
<块引用>mmal:mmal_vc_component_enable:无法启用组件:ENOSPC
回溯(最近一次调用最后一次):文件test.py",第 14 行,在myvf = VF()
文件test.py",第 6 行,initself.camera = picamera.PiCamera()
文件/usr/lib/python2.7/dist-packages/picamera/camera.py",行379,在初始化camera_num, self.STEREO_MODES[stereo_mode],stereo_decimate)
文件/usr/lib/python2.7/dist-packages/picamera/camera.py",行505,在_init_cameraprefix="无法启用相机组件")
文件/usr/lib/python2.7/dist-packages/picamera/exc.py",第133行,在 mmal_check引发 PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError:无法启用相机组件:资源不足(内存除外)
相机模块工作正常,我只是将代码精简到尽可能小的尺寸.有没有人知道这个问题或类似的问题,并且可能提供解决方案?Python 版本是 2.7,Raspberry Rasbiab-System 是完全最新的.提前致谢.
我在这个问题上挣扎了几个小时,并不断收到资源不足"错误.我终于发现,在我的拍照功能中,我需要确保我是这样做的:
<前>相机 = PiCamera()(...此处的相机设置...)camera.capture(我的文件名)相机.关闭()如果我不执行 close(),我每次都会收到该错误.因此,请确保在捕捉"之后立即调用 camera.close().它为我解决了问题.
on my Raspberry Pi, I encounter a strange behaviour regarding the use of the PiCamera module.
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()
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:
mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
Traceback (most recent call last): File "test.py", line 14, in myvf = VF()
File "test.py", line 6, in init self.camera = picamera.PiCamera()
File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 379, in init camera_num, self.STEREO_MODES[stereo_mode], stereo_decimate)
File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 505, in _init_camera prefix="Camera component couldn't be enabled")
File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 133, in mmal_check raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Camera component couldn't be enabled: Out of resources (other than memory)
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.
I struggled with this one for hours, and kept getting the "out of resources" error. I finally figured out that in my take-the-picture function, I needed to make sure I did it like this:
camera = PiCamera() (...camera settings here...) camera.capture(myfileName) camera.close()
If I didn't do the close(), I'd get that error every time. So make sure that camera.close() is getting called right after the 'snap'. It solved the problem for me.
这篇关于从命令提示符运行脚本时,PiCamera 无法初始化为类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!