I am trying to run a simple script that listens for a button click and then takes a picture. Once two pictures have been taken it compares the two using OpenCV to calculate the difference. I am running into this error when running my script followed by the script itself:

mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
Traceback (most recent call last):
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/camera.py", line 456, in _init_camera
    self._camera = mo.MMALCamera()
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/mmalobj.py", line 2279, in __init__
    super(MMALCamera, self).__init__()
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/mmalobj.py", line 633, in __init__
    prefix="Failed to create MMAL component %s" % self.component_type)
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/exc.py", line 184, in mmal_check
    raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to create MMAL component b'vc.ril.camera': Out of memory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "camera-daemon.py", line 12, in <module>
    camera = PiCamera()
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/camera.py", line 431, in __init__
    self._init_camera(camera_num, stereo_mode, stereo_decimate)
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/camera.py", line 460, in _init_camera
    "Camera is not enabled. Try running 'sudo raspi-config' "
picamera.exc.PiCameraError: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.

from picamera import PiCamera
from io import BytesIO
from PIL import Image
from gpiozero import Button
from signal import pause
from time import sleep
from database import insert
from skimage.measure import structural_similarity as ssim
import cv2

button = Button(17)
camera = PiCamera()
camera.resolution = (400,400)

def capture():
    image1 = capture_stream(False)
    image2 = capture_stream(False)
    diff = compute_diff(image1, image2)

def prepare_camera(preview):
        global camera
        # if preview is passed we want to use the preview
        if preview:

        # these print statements help for debugging as well as
        # allowing time for the camera to warm up
        print("taking picture in")

        # stop the camera's preview
        if preview:

def capture_stream(preview):
        global camera
        stream = BytesIO()


        # save the image to the stream and return it
        camera.capture(stream, format='jpeg')
        # rewind the stream so we can read its contents
        return Image.open(stream)

# compute image diffs based on structural similarity
def compute_diff(A, B):
    imageA = cv2.imread(A)
    imageB = cv2.imread(B)

    imageA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
    imageB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

    return ssim(imageA, imageB)

button.when_pressed = capture



I have already read the FAQ and tried their suggestions. I have also tried most of the solutions on all of the other stack exchange questions with this same or similar issues. None have worked. Most recently I have been trying to slowly increase the memory split and it still is throwing this error.

EDIT: I think it's important to note that the camera works fine when I run a separate script with only camera related code in it so its not an issue with how the camera is connected. Double check your camera's connection Although my test scripts were working at first, at some point my camera got disconnected.

I am using Python 3.4, PiCamera 1.13, and OpenCV 3.4.

I will be happy to provide any more information is needed. Any help is greatly appreciated!


This error was resolved when I disconnected and reconnected my camera module.

Somehow my camera module must have been wiggled loose when I was working with it and the Pi was not able to detect the camera. I ran the command:

vcgencmd get_camera and got Returned supported=1 detected=0

Once the camera was reconnected, I was able to run

raspistill -o image.jpg to test and it worked.

Also, some info I didn't know: The collar on top of the CSI (Camera) port is meant to be lifted slightly and then the camera ribbon is to be inserted and the collar gently locked back down. You shouldn't have to jam your ribbon into the port.


The first thing that comes into my mind is that the camera is used at the same time by another script or program. Please check that the camera is not used. Also, be sure that you run only an instance of the script at a time. The general idea is that the camera cannot be accessed from multiple places at a time.

Hope it helps!

  • Do you know of any way to detect this? I've tried running ts -a and there are not other processes running. – Justin May 6 '18 at 15:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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