Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raspberry Pi camera GPIO...close statement causing error

I am not too familiar with programming GPIOs on the Pi, but I wrote this after looking at some tutorials on both the GPIO lib and picamera. I have a button which is connected to pin 4 and ground which when pushed should star the camera, take a picture, and then close the camera. My code below takes a picture but continuously keeps calling the close function. I don't quite understand why.

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

I got some code from here: http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/

If I remove the close() then I run out of resources...I tried doing an "event detect" but I still get the same issue above.

like image 829
ohbrobig Avatar asked Aug 25 '26 16:08

ohbrobig


1 Answers

These lines call the close() function itself, so it causes infinite calls.

def close(self):
    self.close()

You may want to call self.camera.close() instead?

like image 54
fbessho Avatar answered Aug 28 '26 07:08

fbessho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!