15

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)
    sleep(3)
    image2 = capture_stream(False)
    diff = compute_diff(image1, image2)
    print(diff)


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

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

        # stop the camera's preview
        if preview:
            camera.stop_preview()

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

        prepare_camera(preview)

        # save the image to the stream and return it
        camera.capture(stream, format='jpeg')
        # rewind the stream so we can read its contents
        stream.seek(0)
        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

print("running...")

pause()

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!

Justin
  • 432
  • 1
  • 4
  • 12
  • For my Raspberry Pi B and V3 Camera module I had to use the picamera2 library instead https://github.com/raspberrypi/picamera2 – 2080 May 07 '23 at 23:11

6 Answers6

16

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.

Justin
  • 432
  • 1
  • 4
  • 12
2

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!

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

I was running a Python program that used the camera and closed the program that I had created but left Python open. When testing a C++ program with the camera I was getting this error even though I had the Python program that I created closed.

After I closed Python itself then my camera was able to work in C++

234Mike432
  • 29
  • 2
0

1- Enable Camera in raspberry pi: Open raspberry pi configuration by typing this in the command terminal: "sudo raspi-config"
2- select interfacing options
Select Enable camera and hit Enter, then go to Finish and you'll be prompted to reboot.
3- after restarting raspberry pi, test your camera by typing in the terminal: vcgencmd get_camera You got returned : supported = 1 detected = 1 4- test your camera for few seconds by typing this in the terminal : "raspistill -o image.jpg"

0

On your camera module, there are two connectors. One is for the ribbon cable, and the other is for a snap connector. Flip the camera over and look at the top to see if your camera has a snap connector. On my AIY vision kit, there's a snap connector. Press down on that connector and make sure it's locked in.

D.Deriso
  • 101
0

For me all I did was add GPU memory from the Raspberry Pi configuration to 144.