Click here to Skip to main content
15,918,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting error:
Traceback (most recent call last):
  File "/home/pi/SensorImageTesting.py", line 51, in <module>
    camera = PiCamera()
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 433, in __init__
    self._init_preview()
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 513, in _init_preview
    self, self._camera.outputs[self.CAMERA_PREVIEW_PORT])
  File "/usr/lib/python3/dist-packages/picamera/renderers.py", line 558, in __init__
    self.renderer.inputs[0].connect(source).enable()
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 2212, in enable
    prefix="Failed to enable connection")
  File "/usr/lib/python3/dist-packages/picamera/exc.py", line 184, in mmal_check
    raise PiCameraMMALError(status, prefix)
picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources


i have attacked the code if someone can help i shall be thankful

What I have tried:

Python
<pre>#Libraries
import RPi.GPIO as GPIO
import time
from picamera import PiCamera
from time import sleep
import cv2

#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
 
def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)
 
    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2
 
    return distance
 
if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            if dist <= 10 or dist >= 9.5:
                camera = PiCamera()
                camera.start_preview()
                sleep(2)
                camera.capture ('/home/pi/Pictures/image.jpg')
                camera.stop_preview()
                img = cv2.imread('/home/pi/Pictures/image.jpg')
                #rs = cv2.resize(img, (500,350))
                cv2.startWindowThread()
                cv2.namedWindow("preview")
                cv2.imshow("preview", img)
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
            
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
Posted
Updated 26-May-19 21:53pm
v2

1 solution

You are repeatedly requesting a system resource (the PiCamera) inside your while loop until the system has no more. Move the following line outside the loop:
Python
camera = PiCamera()
 
Share this answer
 
Comments
Meharsyed 27-May-19 16:45pm    
thank @Richard MacCutchan. Issue Resolved. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900