【问题标题】:Python-taking a picture with raspberry pi cameraPython——用树莓派相机拍照
【发布时间】:2023-03-11 06:57:01
【问题描述】:

我已经编写了这段代码来在检测到运动时拍照,但是当我运行代码时,它会打印“已拍摄照片”但不保存照片。 我知道我的相机可以正常工作,因为我在 LX 终端中使用 raspistill 命令对其进行了测试。我还尝试更改要保存的文件的路径。 如果您能看到我哪里出错了,我们将不胜感激。 谢谢

import RPi.GPIO as GPIO
import time
import picamera



GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, GPIO.PUD_DOWN)

cam = picamera.PiCamera()
time.sleep(1)
if GPIO.input(4):
    cam.capture('/home/pi/Eaglecam/surveillance.jpg')
print('picture taken')

【问题讨论】:

    标签: python-3.x camera raspberry-pi sensors gpio


    【解决方案1】:
    1. 尝试将print语句放到if GPIO.input(4)的范围内,看看是否成功接收到摄像头的信号。
    2. 可能不是原因,但您应该在完成后关闭相机。使用camera.close() 或使用with picamera.PiCamera() as camera: 初始化相机

    来自其documentaion 的示例:

    import time
    import picamera
    
    with picamera.PiCamera() as camera:
        camera.resolution = (1024, 768)
        camera.start_preview()
        # Camera warm-up time
        time.sleep(2)
        camera.capture('foo.jpg')
    

    【讨论】: