I'm pretty new to both Python and utilizing the GPIO pins on my Raspberry Pi. I am trying to make a "trail camera" that will take pictures of birds when they land on the branches of the trees just off of my back porch.
I have a PIR motion sensor and the camera module hooked up. I have written the following script, largely copied from here, to have the motion sensor wait for motion, then take a photograph, then reset to waiting for the next bit of motion.
import picamera from datetime import datetime from time import sleep from gpiozero import MotionSensor pir = MotionSensor(17) def generate_filename(): timestampe = datetime.strftime(datetime.now(), "%Y-%m-%d-%H-%M-%S-%f") filename = 'home/pi/trail_cam_photos/photo_%s' % timestamp return filename while True: pir.wait_for_motion() while pir.motion_detected: with picamera.PiCamera as camera: camera.resolution=(1024, 768) camera.start_preview() camera.sleep(2) file = generate_filename camera.capture(file) camera.stop_preview() file.close()
When I run the script and then wave my hand over the PIR sensor, I get the following error:
Traceback (most recent call last): File "trail_cam.py", line 19, in <module> with picamera.PiCamera as camera: AttributeError: __exit__
What can cause this error?