forked from al2k/Naturebytes-RaspPi-Dev
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (124 loc) · 5.5 KB
/
Copy pathmain.py
File metadata and controls
157 lines (124 loc) · 5.5 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
# Naturebytes Wildlife Cam Kit | V1.01
# Based on the excellent official Raspberry Pi tutorials and a little extra from Naturebytes
import os
import csv
import time
import arrow
import signal
import logging
import RPi.GPIO as GPIO
from log import log
from subprocess import run
from multiprocessing import shared_memory
# Logging all of the camera's activity to the "naturebytes_camera_log" file. If you want to watch what your camera
# is doing step by step you can open a Terminal window and type "cd /Naturebytes/Scripts" and then type
# "tail -f naturebytes_camera_log" - leave this Terminal window open and you can view the logs live
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
logging.info('Naturebytes Wildlife Cam Kit started up successfully')
# Assigning a variable to the pins that we have connected the PIR to
SENSOR_PIN = 13
BATTERY_PIN = 15
GPIO.setmode(GPIO.BOARD)
GPIO.setup(SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(BATTERY_PIN, GPIO.IN)
def what_os():
path = "/etc/os-release"
with open(path) as stream:
reader = csv.reader(stream, delimiter="=")
os_release = dict(reader)
return os_release
def take_photo(command, save_to, use_overlay, video):
"""
Take a photo and if necessary add overlay
:param command: str: command string to send
:param use_overlay: bool: use an overlay or not
:param video: bool: this is video if True else still
:return: None
"""
# Recording that a PIR trigger was detected and logging the battery level at this time
logging.info('PIR trigger detected')
# logging.info('Battery level is %(get_batt_level)s', { 'get_batt_level': batt_state })
# Assigning a variable so we can create a photo JPG file that contains the date and time as its name
now = arrow.now().format('YYYY-MM-DD_HH:mm:ss')
if video:
photo = now + '.h264'
else:
photo = now +'.png'
# Using the raspistill library to take a photo and show that a photo has been taken in a small preview box on the desktop
cmd = f'{command} -o {photo}'
logging.info(f"cmd:{cmd}")
# Log that we have just taking a photo"
run(cmd.split())
# Log that a photo was taken successfully and state the file name so we know which one"
logging.info(f'Photo {photo} taken successfully')
if os.path.exists(photo):
if use_overlay:
# Log that we are about to attempt to write the overlay text"
logging.info('About to write the overlay text')
overlay = "/usr/bin/convert " + photo + " "
# Use ImageMagick to write text and meta data onto the photo.
# overlay += " -gravity north -background black -extent +0+40 +repage -box black -fill white -pointsize 24 -gravity southwest -annotate +6+6 'Naturebytes Wildlife Cam Kit | Date & Time: " + get_date + '" '" + get_time '" -gravity southeast -annotate +6+6 'Camera 1 " "'" + photo_location
overlay += " -gravity north -background black -extent +0+40 +repage -box black -fill white -pointsize 24 -gravity southwest -annotate +6+6 'Naturebytes Wildlife Cam Kit | Date & Time: " \
+ now + "' -gravity southeast -annotate +6+6 'Camera 1' " + photo
# Log that we the text was added successfully"
logging.info('Added the overlay text successfully')
run(overlay.split())
logging.info('Logo added successfully')
run(["mv",f"./{photo}",f"{save_to}"])
log.info(f"Saved:{save_to}/{photo}")
quit = False
def handle_signal(signum, frame):
"""
Clean close
:param signum:
:param frame:
:return:
"""
global quit
log.info(f"Signal: {signum}")
quit = True
def camera(save_to='./', use_overlay=False, video=False):
"""
Main camera process
:param save_to: where to save photos
:param use_overlay: use the overlay
:param video: bool: still or short video
:return:
"""
signal.signal(signal.SIGINT, handle_signal)
# Starting with Bookworm the cammand name changed
os_release = what_os()
version = os_release.get('VERSION')
shm = None
while not shm:
try:
shm = shared_memory.SharedMemory('camera_control',create=False, size=1)
log.info(f"SM:{shm.buf[0]}")
except Exception as e:
log.error("No shared memory")
time.sleep(1)
while not quit:
# Map the state of the camera to our input pins (jumper cables connected to your PIR)
if GPIO.input(SENSOR_PIN):
log.info(f"SM:{shm.buf[0]}")
if shm.buf[0]:
if '12' in version:
cam_command = 'rpicam-still -e png' if not video else 'rpicam-vid -t 10s'
else:
cam_command = 'libcamera-still -e png' if not video else 'libcamera-vid -t 10s'
video = False if shm.buf[0] == 1 else True
take_photo(cam_command, save_to, use_overlay, video)
time.sleep(10)
shm.close()
if __name__ == "__main__":
import argparse
args = argparse.ArgumentParser( prog='Capture camera images')
save_to = '/usr/local/src/static/photos'
overlay = True
video = False
args.add_argument('-s', '--save_to', type=str, default=save_to)
args.add_argument('-o', '--overlay', action='store_true', default=False)
args.add_argument('-v', '--video', action='store_true', default=False)
values = args.parse_args()
camera(values.save_to, values.overlay, values.video)