我不太熟悉在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()则资源不足…我尝试执行“事件检测”,但仍然遇到上述相同的问题。在
这些行调用close()函数本身,因此它导致无限次调用。在
您可能想改为调用
self.camera.close()
?在相关问题 更多 >
编程相关推荐