Raspberry Pi camera GPIO…close语句导致

2024-10-01 22:32:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我不太熟悉在Pi上编程GPIO,但是我在看过GPIO lib和picamera的一些教程之后写了这篇文章。我有一个连接到4号脚和地的按钮,当按下它时,应该启动相机,拍照,然后关闭相机。我下面的代码会拍一张照片,但会不断地调用close函数。我不太明白为什么。在

import picamera
import RPi.GPIO as GPIO
import datetime
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)

class OpenCamera:
    def __init__(self):
        self.camera = picamera.PiCamera()

    def setres(self):
        self.camera.resolution = (640, 480)
        self.camera.brightness = 50
        self.camera.sharpness = 10

    def takepic(self):
        currenttime = time.localtime()
        day = time.strftime('%m-%d-%Y', currenttime)
        exacttime = time.strftime('%H:%M:%S', currenttime)
        self.camera.capture(day + exacttime + '.jpg')

    def close(self):
        self.close()


while True:
    inputstate = GPIO.input(4)  
    if inputstate == False:         
        startcam = OpenCamera()         
        startcam.setres()       
        time.sleep(4)       
        print('5 4 3 2...cheese!')      
        startcam.takepic()      
        startcam.close()

我从这里得到一些代码:http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/

如果我删除close()则资源不足…我尝试执行“事件检测”,但仍然遇到上述相同的问题。在


Tags: 代码importselfclosegpiotimedefcamera

热门问题