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!