forked from nicolashahn/pi-helmet-cam
-
Notifications
You must be signed in to change notification settings - Fork 3
/
camera.py
executable file
·407 lines (349 loc) · 12.7 KB
/
camera.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python
"""Recording script for a Raspberry Pi powered motorcycle helmet camera.
"""
import os
import json
import datetime
import subprocess
import logging
import logging.handlers
import time
import pickle
import multiprocessing
import googleapiclient.discovery
import googleapiclient.http
import googleapiclient.model
import httplib2
import functools
import socket
try:
import picamera
except ImportError:
print('Couldn\'t import picamera: running as is for debug purposes.')
formatter = logging.Formatter('%(asctime)s [%(processName)s] [%(levelname)-5.5s] %(message)s')
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.DEBUG)
fileHandler = logging.handlers.RotatingFileHandler(
filename=os.path.join(os.path.dirname(__file__), 'camera.log'),
maxBytes=1 * (10 ** 6), backupCount=1)
fileHandler.setFormatter(formatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)
rootLogger.addHandler(consoleHandler)
logging.getLogger('googleapiclient.discovery').setLevel(logging.CRITICAL)
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.CRITICAL)
VIDEO_DIR = os.path.join(os.path.dirname(__file__), 'video')
UPLOADS_DIR = os.path.join(os.path.dirname(__file__), 'uploads')
CREDENTIALS = os.path.join(os.path.dirname(__file__), '.credentials')
FORMAT = 'h264'
MAX_VIDEO_SIZE = 5000 * (10 ** 6) # ~45 minutes
MIN_VIDEO_SIZE = 50 * (10 ** 6) # ~30 seconds
VIDEO_MIN_INTERVALS = 12
UPLOAD_CHUNK_SIZE = 50 * (10 ** 6)
UPLOAD_MAX_WORKERS = 1
# how many 0s to put in front of counter number
ZFILL_DECIMAL = 3
# 8mp V2 camera
RESOLUTION = (1640, 1232)
FRAMERATE = 30
STABILIZATION = False
# number of seconds to flush on disk
INTERVAL = 5
# check for enough disk space every N seconds
SPACE_CHECK_INTERVAL = 30
# what % of disk space must be free to start a new video
REQUIRED_FREE_SPACE_PERCENT = 15 # about an hour with 64gb card
YOUTUBE_TITLE_PREFIX = 'Helmet Camera'
DATE_FORMAT = '%Y-%m-%d_%H-%M'
START_TIME = time.time()
queue = []
class throttle(object):
"""Decorator that prevents a function from being called more than once every
time period.
To create a function that cannot be called more than once a minute:
@throttle(minutes=1)
def my_fun():
pass
"""
def __init__(self, seconds=0, minutes=0, hours=0):
self.throttle_period = datetime.timedelta(
seconds=seconds, minutes=minutes, hours=hours)
self.time_of_last_call = datetime.datetime.min
def __call__(self, fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
now = datetime.datetime.now()
time_since_last_call = now - self.time_of_last_call
if time_since_last_call > self.throttle_period:
self.time_of_last_call = now
self.last_result = fn(*args, **kwargs)
return self.last_result
else:
return self.last_result
return wrapper
def use_led(status):
"""Control on-board green LED status, True for on, False for off.
"""
with open('/sys/class/leds/led0/brightness', 'w') as f:
f.write('%s\n' % int(not status))
@throttle(seconds=5)
def is_connected(host='8.8.8.8', port=53, timeout=1):
"""Returns True if we have internet connection.
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
result = True
except socket.error:
result = False
finally:
socket.setdefaulttimeout(None)
return result
def make_room():
"""Clear oldest video.
"""
sorted_videos = sorted(os.listdir(VIDEO_DIR))
if sorted_videos:
oldest_video = sorted_videos[0]
logging.info('Removing oldest video: %s', oldest_video)
# may not have permission if running as pi and video was created by root
try:
os.remove(os.path.join(VIDEO_DIR, oldest_video))
except OSError:
logging.error('Must run as root otherwise script cannot clear out old videos')
else:
logging.error('No videos in directory %s, cannot make room', VIDEO_DIR)
time.sleep(SPACE_CHECK_INTERVAL)
def measure_temp():
temp = os.popen('vcgencmd measure_temp').readline()
return temp.replace('temp=', '').strip()
def enough_disk_space():
"""Return true if we have enough space to start a new video.
"""
df = subprocess.Popen(['df', '/'], stdout=subprocess.PIPE)
output = df.communicate()[0]
percent_used_str = output.split("\n")[1].split()[4]
percent_used = int(percent_used_str.replace('%', ''))
enough = 100 >= REQUIRED_FREE_SPACE_PERCENT + percent_used
logging.debug('%s%% of disk space used. Enough: %s', percent_used, enough)
logging.debug('Device temperature: %s', measure_temp())
return enough
def upload(filename):
"""Upload given filename on YouTube using saved credentials.
For each video file we will create a JSON file in `uploads` dir with upload
progress. This way we can resume upload if it was interrupted and avoid
duplicates.
"""
try:
credentials = pickle.load(open(CREDENTIALS))
except IOError:
logging.error('Unable to read .credentials file to perform youtube upload.')
return
service = googleapiclient.discovery.build(
'youtube', 'v3', credentials=credentials)
name_parts = os.path.split(filename)[1].split('.')
title = '%s %s' % (
YOUTUBE_TITLE_PREFIX,
':'.join(name_parts[0].replace('_', ' ').rsplit('-', 1)))
part_num = int(filename.split('.')[1])
if part_num:
title = '%s Part %s' % (title, part_num + 1)
body = dict(snippet=dict(title=title, tags=['helmet'], categoryId=2),
status=dict(privacyStatus='unlisted'))
logging.info('Preparing to upload %s...', filename)
request = service.videos().insert(
part=','.join(body.keys()),
body=body,
media_body=googleapiclient.http.MediaFileUpload(
filename, chunksize=UPLOAD_CHUNK_SIZE, resumable=True)
)
progress_filename = os.path.join(UPLOADS_DIR, '%s.json' % os.path.split(
filename)[1])
try:
with open(progress_filename) as f:
progress = json.load(f)
except IOError:
progress = None
except ValueError:
logging.warning('Invalid upload progress in %s', progress_filename)
os.remove(progress_filename)
progress = None
if progress is not None:
logging.info('Resuming existing upload from %s...', progress_filename)
request.resumable_progress = progress['resumable_progress']
request.resumable_uri = progress['resumable_uri']
response = None
try:
_prev_percent = 0
while response is None:
status, response = request.next_chunk(num_retries=3)
if status:
with open(progress_filename, 'w') as f:
json.dump({
'resumable_progress': request.resumable_progress,
'resumable_uri': request.resumable_uri}, f)
_percent = status.progress()
if _percent - _prev_percent > 0.01:
logging.info('Uploading at [%s]', '{:.2%}'.format(_percent))
_prev_percent = _percent
except googleapiclient.errors.HttpError as e:
if 'The number of bytes uploaded' in e.content or e.resp.status == 404:
logging.warning(e.content)
logging.info('Removing upload progress and starting again.')
os.remove(progress_filename)
else:
raise googleapiclient.errors.HttpError(e.resp, e.content)
except IOError as e:
logging.exception('Removing upload progress and starting again.')
os.remove(progress_filename)
except httplib2.ServerNotFoundError:
logging.debug('Couldn\'t upload %s since no connection is available.')
else:
logging.info('Successfully uploaded %s', response)
try:
os.remove(progress_filename)
except OSError:
pass
os.remove(filename)
def watch():
"""Background watcher which removes old videos and tries to perform an upload.
"""
while True:
while not enough_disk_space():
make_room()
time.sleep(1)
for i in reversed([i for i, p in enumerate(queue) if not p.is_alive()]):
queue.pop(i)
if queue:
logging.debug('Upload queue: %s', queue)
if is_connected():
for video in sorted(os.listdir(VIDEO_DIR)):
filename = os.path.join(VIDEO_DIR, video)
if filename in [i.name for i in queue]:
continue
if os.stat(filename).st_size < MIN_VIDEO_SIZE:
continue
if len(queue) < UPLOAD_MAX_WORKERS:
p = multiprocessing.Process(target=upload, name=filename, args=[filename])
logging.debug('Starting background process %s', p)
p.start()
queue.append(p)
time.sleep(SPACE_CHECK_INTERVAL)
class OutputShard(object):
def __init__(self, filename):
self.filename = filename
self.is_new = self.size == 0
self.stream = open(filename, 'ab')
def __repr__(self):
return '<OutputShard:%s>' % self.filename
def write(self, buf):
self.stream.write(buf)
def close(self):
self.stream.close()
def remove(self):
os.remove(self.filename)
@property
def size(self):
try:
return os.stat(self.filename).st_size
except OSError:
return 0
def uptime():
"""Get current uptime in seconds.
"""
return time.time() - START_TIME
def record():
"""Start recording if/after no connection is avilable and stop when connected.
The idea is to stop recording so the upload can be completed without
generating any new videos, and you are very unlikely to need a recordding when
you are near WiFi anyway.
"""
with picamera.PiCamera() as camera:
# make sure that camera is connected
pass
should_log = True
is_led_on = False
while is_connected():
if should_log:
logging.debug('Still connected to the network...')
# blink the LED once in a while to know that we are ready to record
# blink every second for first 5 minutes, then once in 5 seconds
if uptime() < 60 * 5:
for i in range(5):
use_led(bool(i % 2))
time.sleep(1)
else:
use_led(is_led_on)
is_led_on = not is_led_on
time.sleep(5)
should_log = False
now = datetime.datetime.now()
# guard against writing into old files if system time is incorrect
old_videos = [datetime.datetime.strptime(
i.split('.')[0], DATE_FORMAT) for i in sorted(os.listdir(VIDEO_DIR))]
for old_video in old_videos:
if old_video >= now:
shards = len([i for i in old_videos if i == old_video])
if shards > 1:
logging.critical('Existing video file %s is newer from current time %s. This is likely caused by incorrent system time. Trying again shortly...', old_video, now)
time.sleep(10)
return record()
with picamera.PiCamera() as camera:
camera.resolution = RESOLUTION
camera.framerate = FRAMERATE
camera.video_stabilization = STABILIZATION
logging.debug('Recording with %s@%s FPS', RESOLUTION, FRAMERATE)
camera.annotate_background = picamera.Color('black')
counter = 0
timestamp = now.strftime(DATE_FORMAT)
filename = os.path.join(VIDEO_DIR, '%s.{}.%s' % (timestamp, FORMAT))
shard = OutputShard(filename.format(str(counter).zfill(ZFILL_DECIMAL)))
is_new = shard.is_new
camera.start_recording(shard, format=FORMAT, intra_period=INTERVAL * FRAMERATE)
intervals_recorded = 0
use_led(True)
while True:
camera.annotate_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
camera.split_recording(shard)
camera.wait_recording(INTERVAL)
intervals_recorded += 1
if intervals_recorded % 10 == 0:
logging.debug('Recorded %s intervals...', intervals_recorded)
if shard.size > MAX_VIDEO_SIZE:
counter += 1
logging.debug('Using next shard %s for video file', counter)
if is_connected():
use_led(False)
logging.info('Connected to WiFi. Not recording anymore.')
camera.stop_recording()
shard.close()
if is_new and intervals_recorded < VIDEO_MIN_INTERVALS:
logging.debug('Cleaning up short video %s (%s intervals)', shard, intervals_recorded)
shard.remove()
break
shard = OutputShard(filename.format(str(counter).zfill(ZFILL_DECIMAL)))
logging.info('Trying to start recording again...')
record()
def main():
logging.info('Powered on at %s', datetime.datetime.now())
# Set the PWR LED to GPIO mode (set 'off' by default)
with open('/sys/class/leds/led0/trigger', 'w') as f:
f.write('gpio\n')
if not os.path.isdir(VIDEO_DIR):
logging.debug('Creating directory %s', VIDEO_DIR)
os.mkdir(VIDEO_DIR)
if not os.path.isdir(UPLOADS_DIR):
logging.debug('Creating directory %s', UPLOADS_DIR)
os.mkdir(UPLOADS_DIR)
p = multiprocessing.Process(target=watch, name='watcher')
logging.debug('Starting background process %s', p)
p.start()
record()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
exit('Command killed by keyboard interrupt')
except Exception as e:
logging.exception(e)