Meharsyed Ответов: 1

Как я могу сделать снимок с помощью датчика расстояния в raspberry pi3?


я получаю ошибку:
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


я атаковал код, если кто-то может помочь, я буду благодарен

Что я уже пробовал:

<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()

1 Ответов

Рейтинг:
9

Richard MacCutchan

Вы постоянно запрашиваете системный ресурс (PiCamera) внутри вашего компьютера. while петля до тех пор, пока система не будет иметь больше. Переместите следующую строку за пределы цикла:

camera = PiCamera()


Meharsyed

спасибо @Richard MacCutchan. Вопрос Решен. :)