Raspberry Pi camera. Out of resources

2019-09-04 20:56发布

问题:

Trying to launch my camera with motion sensor. Works fine like this:

import RPi.GPIO as GPIO
import time
import picamera
import datetime
import os

def getFileName():
    return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")

pin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
prevState = False
currState = False
camera = picamera.PiCamera()
while True:
    time.sleep(0.1)
    prevState = currState
    currState = GPIO.input(pin)
    if currState != prevState:
        newState = "HIGH" if currState else "LOW"
        print ("GPIO pin %s is %s" % (pin, newState))
        if currState:
            fileName = getFileName()
            print ("Starting Recording...")
            camera.start_preview()
            camera.start_recording(fileName)
            time.sleep(10)
            print (fileName)
        else:
            camera.stop_preview()
            time.sleep(1)
            camera.stop_recording()
            print ("Stopped Recording")

    else:
        print("No motions")

But then i try to def my function for Tornado server:

def secure_on(prevState, currState):
    pin = 4
    #GPIO.setmode(GPIO.BCM)
    #GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    #camera = picamera.PiCamera()
    time.sleep(0.1)
    prevState = currState
    #currState = GPIO.input(pin)
    if currState != prevState:
        newState = "HIGH" if currState else "LOW"
        return {'info': "GPIO pin %s is %s" % (pin, newState)}
        if currState:
            fileName = getFileName()
            print ("Starting Recording...")
            #camera.start_preview()
            #camera.start_recording(fileName)
            time.sleep(10)
            return {'info': fileName}
        else:
            #camera.stop_preview()
            time.sleep(1)
            #camera.stop_recording()
            return {'info': "Stopped Recording"}
    else:
        return {'info': "No motions"}

Tornado:

class SecureOnHandler(tornado.web.RequestHandler):
    @gen.coroutine
    def get(self):
        result = RosPi.secure_on(prevSec, currSec)

        self.write({"info": result['info']})

After that i get an error

"Camera component couldn't be enabled: Out of resources (other than memory) ERROR:tornado.access:500 GET /secure_on

What could be causing the error? Thanks in advance!

回答1:

I know this is an old question but I just wanted to post my solution to the Out of resources error in case it can save someone time and effort.

The problem for me was that I was initializing the camera module is two Python files at the same time (a script with a while loop that took a picture when motion was detected and another script that took a picture on demand).

As per the documentation: https://picamera.readthedocs.io/en/release-1.13/faq.html#camera-locks-up-with-multiprocessing you must ensure that only a single process creates an instance of PiCamera at one time.

My solution was simply to move initializing the camera module inside a function and call .close() after taking the picture to free up the process ready to be used by the next script that calls it.