microberry
Posts: 1
Joined: Fri Sep 24, 2021 7:02 am

Error: Camera module in for loop

Fri Sep 24, 2021 7:10 am

intent : All I'm trying to do is find the average pixel as time progresses
observation 1: code runs without error when the range value is 1
observation 1: code errors out when the range value is 10. See error below:

Code: Select all

ERROR
mmal: mmal_vc_port_enable: failed to enable port vc.null_sink:in:0(OPQV): ENOSPC
mmal: mmal_port_enable: failed to enable connected port (vc.null_sink:in:0(OPQV))0x2610170 (ENOSPC)
mmal: mmal_connection_enable: output port couldn't be enabled

Backend terminated or disconnected. Use 'Stop/Restart' to restart.[/color]

Any idea why this is happening?


Code: Select all

My Code:
myavg = []
mytime = []
for i in range(10):
    camera = PiCamera()
    camera.resolution = (640,480)
    imageCapture = PiRGBArray(camera,size = (640,480))    
    camera.capture(imageCapture,format="bgr")
    image = imageCapture.array
    average_value = np.average(image)
    myavg.append(average_value)
    mytime.append(i)
    time.sleep(0.5)

plt.plot(mytime,myavg,'o')
plt.title("pixel average")
plt.xlabel("time")
plt.ylabel("average")
plt.grid(True)
plt.show()

User avatar
B.Goode
Posts: 12974
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Error: Camera module in for loop

Sat Sep 25, 2021 6:58 am

microberry wrote:
Fri Sep 24, 2021 7:10 am
intent : All I'm trying to do is find the average pixel as time progresses
observation 1: code runs without error when the range value is 1
observation 1: code errors out when the range value is 10. See error below:

Code: Select all

ERROR
mmal: mmal_vc_port_enable: failed to enable port vc.null_sink:in:0(OPQV): ENOSPC
mmal: mmal_port_enable: failed to enable connected port (vc.null_sink:in:0(OPQV))0x2610170 (ENOSPC)
mmal: mmal_connection_enable: output port couldn't be enabled

Backend terminated or disconnected. Use 'Stop/Restart' to restart.[/color]

Any idea why this is happening?


Code: Select all

My Code:
myavg = []
mytime = []
for i in range(10):
    camera = PiCamera()
    camera.resolution = (640,480)
    imageCapture = PiRGBArray(camera,size = (640,480))    
    camera.capture(imageCapture,format="bgr")
    image = imageCapture.array
    average_value = np.average(image)
    myavg.append(average_value)
    mytime.append(i)
    time.sleep(0.5)

plt.plot(mytime,myavg,'o')
plt.title("pixel average")
plt.xlabel("time")
plt.ylabel("average")
plt.grid(True)
plt.show()


My hunch, untested, is that the camera is a resource that is limited to one single use at any given time. So you can probably only have a single instance of a PiCamera() object.

Code: Select all

for i in range(10):
    camera = PiCamera()
   [ .. other stuff .. ]
    time.sleep(0.5)

So when range is 1, all is well. But for any greater value perhaps the second attempt to use PiCamera() errors, because the resource hasn't been closed, destroyed, de-allocated or 'handed-back; after the previous pass through the loop. Possible solutions: close() the camera after you have used it - https://picamera.readthedocs.io/en/rele ... mera.close or move the camera = PiCamera() call outside the loop.


Ref: https://picamera.readthedocs.io/en/rele ... mera.close
After successfully constructing a PiCamera object, you should ensure you call the close() method once you are finished with the camera




The "Backend terminated or disconnected. Use 'Stop/Restart' to restart." message is probably a consequence of running your failing script under the control of the Thonny IDE. It will go away once you have a reliable script.

Return to “Python”