-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcameratrap.py
51 lines (42 loc) · 1.43 KB
/
cameratrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'''
This program will be used on raspberrypi to capture
images using a picamera once motion is detected.
'''
from gpiozero import MotionSensor
from picamera2 import Picamera2, Preview
import RPi.GPIO as GPIO
import logging
import os
import time
time.sleep(30)
logging.basicConfig(filename='cameratrap.log', level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s')
if not os.path.exists("/home/pi/cameratrap"):
os.mkdir("/home/pi/cameratrap")
try:
#set GPIO pin 17 as input from the Motion Sensor
#set GPIO pin 27 as an output for the LED
pir = MotionSensor(17)
camera = Picamera2()
camera_config = camera.create_still_configuration(main={"size": (1920, 1080)}, lores={"size": (640, 480)}, display="lores")
camera.configure(camera_config)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.HIGH)
GPIO.setup(27, GPIO.LOW)
while True:
if GPIO.input(17):
camera.start_preview()
camera.start()
filename = "/home/pi/cameratrap/" + (time.strftime("%Y-%m-%d-%H-%M-%S")) + ".jpg"
camera.capture_file(filename)
camera.stop_preview()
print("Motion detected!")
GPIO.output(27,1)
time.sleep(2)
else:
print("No motion detected!")
GPIO.output(27,0)
time.sleep(2)
except Exception as err:
logging.info(str(err))