PiCamera cannot be initialized as a class member when the script is run from command prompt

匿名 (未验证) 提交于 2019-12-03 00:52:01

问题:

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.

回答1:

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.



回答2:

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 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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!