如何使用覆盆子pi3中的距离传感器拍摄图像? [英] How can I take image using distance sensor in raspberry pi3?

查看:75
本文介绍了如何使用覆盆子pi3中的距离传感器拍摄图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误:

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攻击代码如果有人可以帮助我会感激



我有什么试过:





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

What I have tried:

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

推荐答案

您在中重复请求系统资源(PiCamera),而循环,直到系统不再有。在循环外移动以下行:

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:
camera = PiCamera()


这篇关于如何使用覆盆子pi3中的距离传感器拍摄图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆