我对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(),那么我的资源就用尽了......我试着做一个"事件检测"但我仍然遇到同样的问题。
答案 0 :(得分:3)
这些行调用close()函数本身,因此会导致无限次调用。
def close(self):
self.close()
您可能想要拨打self.camera.close()
而不是?