5

I'm doing a project where I connect the raspberry pi Zero with arduino, I hhave two sensors that help me to know health data in the arduino, an a Pi NoIR V2 camera in raspberry. When the conditions is accomplishes, I make photos (14 pictures), the problem s the following: When the raspberry make the second round of photos, it launch the following error:

Traceback (most recent call last):
  File "/home/pi/Desktop/prueba_old.py", line 78, in <module>
    foto_segs(count)
  File "/home/pi/Desktop/prueba_old.py", line 14, in foto_segs
    camera = PiCamera()
  File "build/bdist.linux-armv6l/egg/picamera/camera.py", line 417, in __init__
    self._init_preview()
  File "build/bdist.linux-armv6l/egg/picamera/camera.py", line 496, in _init_preview
    self, self._camera.outputs[self.CAMERA_PREVIEW_PORT])
  File "build/bdist.linux-armv6l/egg/picamera/renderers.py", line 512, in __init__
    self.renderer.connect(source)
  File "build/bdist.linux-armv6l/egg/picamera/mmalobj.py", line 1467, in connect
    self._connection = MMALConnection(source, self.inputs[0])
  File "build/bdist.linux-armv6l/egg/picamera/mmalobj.py", line 1280, in __init__
    prefix="Failed to enable connection")
  File "build/bdist.linux-armv6l/egg/picamera/exc.py", line 157, in mmal_check
    raise PiCameraMMALError(status, prefix)
PiCameraMMALError: Failed to enable connection: Out of resources (other than memory)

In resume, I launch the script, this works well, printing in the screen the values receibed from the arduino, and when receives "camara", it makes the photos, finish and continue receiving data. But when returns to receive "camara" again, it launch the error.

The code is the following one:

import serial
import os
from picamera import PiCamera
from time import sleep

def foto_segs(i):
    j=0
    camera = PiCamera()
    for j in range(2):
        for i in range(i+8):
            camera.capture('%d.jpg' %i)
while True:
    #try:
    txt = arduino.readline()
    print txt.rstrip('\r\n')
    if (txt.rstrip('\r\n') == 'camara'):
        foto_segs(count)
        count =count+18
        print count 
1
  • 3
    Instantiate the camera once at the top of your script. That error usually occurs when two or more processes attempt to use the camera simultaneously.
    – Dave Jones
    Commented Mar 12, 2017 at 11:37

1 Answer 1

3

As @Dave Jones commented, problem is in function foto_segs():

import serial
import os
from picamera import PiCamera
from time import sleep

# Initialize camera global amd only once
camera = PiCamera()

def foto_segs(i):
    j=0
    for j in range(2):
        for i in range(i+8):
            camera.capture('%d.jpg' %i)
while True:
    #try:
    txt = arduino.readline()
    print txt.rstrip('\r\n')
    if (txt.rstrip('\r\n') == 'camara'):
        foto_segs(count)
        count =count+18
        print count 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.