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

on my Raspberry Pi, I come across unusual behavior regarding the use of the PiCamera module.

The following code runs smoothly when it is run from IDLE (F5) or from the command line ($ python test.py)

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

But when I put the camera object in a class, the code will only work when I start with 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 run the above code from the command line, I get the following error message:

mmal: mmal_vc_component_enable: failed to enable component: ENOSPC

Traceback (last last call): 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 = "Unable to enable camera component")

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

picamera.exc.PiCameraMMALError: the camera component cannot be turned on: From resources (except memory)

The camera module works correctly, I just stripped the code to the smallest possible size. Does anyone know this problem or a similar problem and can probably provide a solution? Python is 2.7 and the Raspberry Rasbiab system has been completely updated. Thanks in advance.

+5
source share
2 answers

I struggled with this alone for several hours and kept getting the error "out of resources". I finally realized that in my take-the-picture function, I needed to make sure that I did this as follows:

  camera = PiCamera ()
     (... camera settings here ...)
     camera.capture (myfileName)
     camera.close ()

If I did not close (), I would get this error every time. Therefore, make sure that camera.close () is called immediately after the "binding". He solved the problem for me.

+3
source

It turned out that the camera module was turned off incorrectly when the destructor was not explicitly called (the LED was off, so I did not see it).

IDLE processes the running camera by resetting it before running the script, but not the python interpreter.

So now everything is fine when the destructor is called before the script completes.

+2
source

Source: https://habr.com/ru/post/1208997/


All Articles