lonesoac0
Posts: 6
Joined: Tue Jun 23, 2015 12:09 pm

piCamera not working consistently

Sun Jul 03, 2016 7:17 pm

Hello all,

I am getting the following error code:
mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
Traceback (most recent call last):
File "./test.py", line 57, in <module>
camera = picamera.PiCamera()
File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 488, in __init__
self.STEREO_MODES[stereo_mode], stereo_decimate)
File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 620, in _init_camera
prefix="Camera component couldn't be enabled")
File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 191, in mmal_check
raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Camera component couldn't be enabled: Out of resources (other than memory)
when I try to run the following Python code:

Code: Select all

#!/usr/bin/python
from __future__ import absolute_import, print_function
import picamera, tweepy, glob, datetime, RPi.GPIO as GPIO, time, sys

# == OAuth Authentication ==
#
# This mode of authentication is the new preferred way
# of authenticating with Twitter.

# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
consumer_key=""
consumer_secret=""

# The access tokens can be found on your applications's Details
# page located at https://dev.twitter.com/apps (located
# under "Your access token")
access_token=""
access_token_secret=""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

# If the authentication was successful, you should
# see the name of the account print out
#print(api.me().name)

# If the application settings are set for "Read and Write" then
# this line should tweet out the message to your account's
# timeline. The "Read and Write" setting is on https://dev.twitter.com/apps
#api.update_with_media(fullFilename, status='This person is at the door. Reference image name:' + fullFilename)

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
	input_state = GPIO.input(18)
	if input_state == False:
		test=str((datetime.datetime.today()))
		test=test.translate(None, '-:. ')
		sss='.'
		seq=(test, "jpg")
		fullFilename = (sss.join(seq))
		camera = picamera.PiCamera()
		camera.capture(fullFilename)
		api.update_with_media(fullFilename, status='This person is at the door. Reference image name:' + fullFilename)
		time.sleep(1.0)
#		sys.exit()
I press the button for my project once and it works fine. I press a second time and it fails every time. It seems obvious to me that the camera object needs to be unloaded and re-loaded according to the error code. I just have no idea how to do it. Please advise.

User avatar
bensimmo
Posts: 4622
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: piCamera not working consistently

Sun Jul 03, 2016 8:50 pm

Way beyond me,but I remember a mention to something like this in 3.2 of the API docs https://picamera.readthedocs.io/en/rele ... ipes1.html
Is it similar to that?

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: piCamera not working consistently

Sun Jul 03, 2016 10:31 pm

Code: Select all

while True:
   # ,,,
   if ... :
      # ...
      camera = picamera.PiCamera()
You're creating a picamera every time you press the button.
You only need to do that once.
Put the line

Code: Select all

      camera = picamera.PiCamera()
outside the while loop,

lonesoac0
Posts: 6
Joined: Tue Jun 23, 2015 12:09 pm

Re: piCamera not working consistently

Mon Jul 04, 2016 9:51 pm

That did it. I feel like an idiot for not coming up with that myself.

Return to “Beginners”