-
Notifications
You must be signed in to change notification settings - Fork 2
/
camera.timelapse.py
472 lines (380 loc) · 15.9 KB
/
camera.timelapse.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
from functions import Echo, Console
from libcamera import ColorSpace, controls, Transform
from picamera2 import MappedArray, Picamera2
from picamera2.controls import Controls
from picamera2.outputs import FileOutput
from PIL import Image
import argparse
import datetime
import glob
import fractions
import numpy
import os
import piexif
import shutil
import statistics
import subprocess
import sys
import threading
import time
version = '2024.02.05'
# Kill other camera script(s)
try:
cameraRemoteScript = "/home/pi/camera.remote/camera.py"
subprocess.check_call(['pkill', '-9', '-f', cameraRemoteScript])
except Exception as ex:
pass
console = Console()
echo = Echo()
camera = Picamera2()
camera.set_logging(Picamera2.ERROR)
stillConfiguration = camera.create_still_configuration(main={"size": (1920, 1080)})
try:
camera.set_controls({"AfMode": controls.AfModeEnum.Continuous})
except Exception as ex:
console.info('Camera does not support autofocus.')
time.sleep(3)
pass
# === Argument Handling ========================================================
parser = argparse.ArgumentParser()
parser.add_argument('--interval', dest='interval', help='Set the timelapse interval', type=int)
parser.add_argument('--framerate', dest='framerate', help='Set the output framerate', type=int)
parser.add_argument('--rotate', dest='rotate', help='Rotate the camera in 90* increments', type=int)
parser.add_argument('--exifFStop', dest='exifFStop', help='Set the numeric F-Stop value in the image EXIF data', type=float)
parser.add_argument('--exifFocalLength', dest='exifFocalLength', help='Set the numeric Focal Length value (mm) in the image EXIF data', type=float)
parser.add_argument('--exifFocalLengthEquivalent', dest='exifFocalLengthEquivalent', help='Set the numeric 35mm Focal Length value (mm) in the image EXIF data', type=float)
parser.add_argument('--outputFolder', dest='outputFolder', help='Set the folder where images will be saved', type=str)
parser.add_argument('--retention', dest='retention', help='Set the number of days to locally retain the captured files', type=int)
parser.add_argument('--waitUntilAnalysis', dest='waitUntilAnalysis', help='Set whether to perform an initial analysis', type=bool)
parser.add_argument('--renderVideo', dest='renderVideo', help='Set whether a video is generated every 24 hours', type=bool)
parser.add_argument('--uploadVideo', dest='uploadVideo', help='Set whether to automatically upload videos to YouTube', type=bool)
parser.add_argument('--privacy', dest='privacy', help='Set the privacy status of the YouTube video', type=str)
args = parser.parse_args()
interval = args.interval or 10
try:
interval = int(interval)
except:
interval = 10
framerate = args.framerate or 60
try:
framerate = int(framerate)
except:
framerate = 60
shutter = int((1/(int(framerate)*2)) * 10000000)
defaultFramerate = 30
rotate = args.rotate or 0
try:
rotate = int(rotate)
except:
rotate = 0
retention = args.retention or 7
try:
retention = int(retention)
except:
retention = 7
waitUntilAnalysis = args.waitUntilAnalysis or False
if waitUntilAnalysis != True:
waitUntilAnalysis = False
waitUntilAnalysisStatus = -1
else:
waitUntilAnalysisStatus = 1
renderVideo = args.renderVideo or True
if renderVideo != False:
renderVideo = True
renderingInProgress = False
uploadVideo = args.uploadVideo or False
if uploadVideo != True:
uploadVideo = False
outputFolder = args.outputFolder or '/home/pi/dcim/'
if outputFolder.endswith('/') == False:
outputFolder = outputFolder+'/'
privacy = args.privacy or 'public'
brightnessThreshold = 125
darknessThreshold = 35
# === Data Objects ============================================================
class EXIFData:
def __init__(self, Orientation = 1, FStop = None, FocalLength = None, FocalLengthEquivalent = None):
self.Orientation = Orientation
self.FStop = FStop
self.FocalLength = FocalLength
self.FocalLengthEquivalent = FocalLengthEquivalent
EXIFDataOverride = EXIFData()
EXIFDataOverride.FStop = args.exifFStop
EXIFDataOverride.FocalLength = args.exifFocalLength
EXIFDataOverride.FocalLengthEquivalent = args.exifFocalLengthEquivalent
# === Functions ================================================================
def getFileName(imageCounter = 1):
global waitUntilAnalysisStatus
now = datetime.datetime.now()
datestamp = now.strftime('%Y%m%d')
extension = '.jpg'
if waitUntilAnalysisStatus != -1:
extension = '~.jpg' # These images are only used for analysis and not kept.
return datestamp + '/' + str(imageCounter).zfill(8) + extension
# ------------------------------------------------------------------------------
def getFilePath(imageCounter = 1):
now = datetime.datetime.now()
datestamp = now.strftime('%Y%m%d')
try:
os.makedirs(outputFolder, exist_ok = True)
try:
os.makedirs(outputFolder + datestamp, exist_ok = True)
except OSError:
console.error('Creation of the output folder ' + outputFolder + datestamp + ' failed!')
echo.on()
quit()
except OSError:
console.error('Creation of the output folder ' + outputFolder + ' failed!' )
echo.on()
quit()
else:
return outputFolder + getFileName(imageCounter)
# ------------------------------------------------------------------------------
def postProcessImage(filePath, angle):
global EXIFDataOverride
try:
image = Image.open(filePath)
FileEXIFData = piexif.load(filePath)
if angle > 0:
newOrientation = 1
if angle == 90:
newOrientation = 6
image = image.rotate(-90, expand=True)
elif angle == 180:
newOrientation = 3
image = image.rotate(180, expand=True)
elif angle == 270:
newOrientation = 8
image = image.rotate(90, expand=True)
EXIFDataOverride.Orientation = newOrientation
FileEXIFData['Orientation'] = EXIFDataOverride.Orientation
try:
if EXIFDataOverride.FStop is not None:
FileEXIFData['Exif'][piexif.ExifIFD.FNumber] = (int(EXIFDataOverride.FStop * 100), 100)
if EXIFDataOverride.FocalLength is not None:
FileEXIFData['Exif'][piexif.ExifIFD.FocalLength] = (int(EXIFDataOverride.FocalLength * 100), 100)
if EXIFDataOverride.FocalLengthEquivalent is not None:
FileEXIFData['Exif'][piexif.ExifIFD.FocalLengthIn35mmFilm] = int(EXIFDataOverride.FocalLengthEquivalent)
except Exception as ex:
console.warn('Could not rotate apply additional EXIF data to image. Please check supplied data. ' + str(ex))
pass
EXIFBytes = piexif.dump(FileEXIFData)
image.save(filePath, exif=EXIFBytes)
except Exception as ex:
console.warn('Could not rotate ' + filePath + ' ' + str(angle) + ' degrees. ' + str(ex))
pass
# ------------------------------------------------------------------------------
def captureTimelapse():
try:
global interval
global outputFolder
global rotate
global waitUntilAnalysisStatus
started = datetime.datetime.now().strftime('%Y%m%d')
# Set counter start based on last image taken today (allows multiple distinct sequences to be taken in one day)
try:
latestImagePath = max(glob.iglob(outputFolder + started + '/*.jpg'),key=os.path.getmtime)
counter = int(latestImagePath.replace(outputFolder + started + '/', '').replace('.jpg', '')) + 1
except:
counter = 1
pass
preAnalysisCounter = counter
console.info('Starting timelapse sequence at an interval of ' + str(interval) + ' seconds...')
while True:
if waitUntilAnalysisStatus == 0:
waitUntilAnalysisStatus = -1
counter = preAnalysisCounter
if started != datetime.datetime.now().strftime('%Y%m%d'):
# It is a new day, reset the counter
started = datetime.datetime.now().strftime('%Y%m%d')
counter = 1
filePath = getFilePath(counter)
try:
console.info('Capturing ' + str(filePath) + ' ...')
request = camera.switch_mode_and_capture_request(stillConfiguration)
request.save('main', filePath)
request.release()
# Avoid 0 length or missing files from breaking ffmpeg encoding
time.sleep(1)
if os.path.exists(filePath):
if os.stat(filePath).st_size > 1000:
counter += 1 # Only increment if file exists and is greater than 1KB in size (no timelapse worthy mage is likely to be smaller).
postProcessImage(filePath, rotate)
else:
console.warn(str(filePath) + ' was too small (' + str(os.stat(filePath).st_size) + ')...')
os.remove(filePath); # Remove existing files that are too small
time.sleep(interval)
except Exception as ex:
console.warn('Could not capture most recent image. ' + str(ex))
except Exception as ex:
console.error('Error occurred during capture loop. ' + str(ex))
# ------------------------------------------------------------------------------
def analyzeLastImages():
global interval
global framerate
global waitUntilAnalysisStatus
global darknessThreshold
global brightnessThreshold
try:
time.sleep(interval * 1.5)
console.info('Starting image analysis... ')
measuredBrightnessList = []
while True:
try:
started = datetime.datetime.now().strftime('%Y%m%d')
latestImagePath = max(glob.iglob(outputFolder + started + '/*.jpg'),key=os.path.getmtime)
latestImage = Image.open(latestImagePath)
measuredBrightness = numpy.mean(latestImage)
measuredBrightnessList.append(float(measuredBrightness))
if len(measuredBrightnessList) >= (framerate * 0.25):
measuredAverageBrightness = statistics.mean(measuredBrightnessList)
console.info('Average brightness of ' + str(int(framerate * 0.25)) + ' recent images: ' + str(measuredAverageBrightness))
if waitUntilAnalysisStatus == 1:
console.info('Removing files created during initial analysis... ')
waitUntilAnalysisStatus = 0
for analysisFile in glob.iglob(outputFolder + started + '/*~.jpg'):
os.remove(analysisFile)
if measuredAverageBrightness < (darknessThreshold - 10) and measuredAverageBrightness > -1:
if camera.framerate >= 30:
console.info('Entering long exposure mode based on analysis of last image set... ')
slowFramerate = fractions.Fraction(1, 10)
try:
camera.framerate = slowFramerate
except Exception as ex:
console.warn('Unable to set framerate to ' + str(slowFramerate) + ' ' + str(ex))
pass
elif measuredAverageBrightness > (darknessThreshold + 10):
if camera.framerate < 30:
console.info('Exiting long exposure mode based on analysis of last image set... ')
camera.framerate = 30
if measuredAverageBrightness > (brightnessThreshold + 25) and measuredAverageBrightness > -1:
if camera.shutter_speed == 0 or camera.shutter_speed > 1000:
console.info('Increasing shutter speed based on analysis of last image set... [1000]')
camera.shutter_speed = 1000
elif camera.shutter_speed > 100:
console.info('Increasing shutter speed based on analysis of last image set... [50]')
camera.shutter_speed = 50
elif measuredAverageBrightness < (brightnessThreshold - 25):
if camera.shutter_speed < 100 and camera.shutter_speed != 0:
console.info('Decreasing shutter speed based on analysis of last image set... [1000]')
camera.shutter_speed = 1000
elif camera.shutter_speed > 900:
console.info('Setting shutter speed to "auto" based on analysis of last image set... ')
camera.shutter_speed = 0 # Auto
measuredBrightnessList.clear()
except:
# Ignore errors as sometimes a file will still be in use and can't be analyzed
pass
time.sleep(interval)
return True
except Exception:
console.warn('Could not analyze most recent image. ')
pass
return False
# ------------------------------------------------------------------------------
def convertSequenceToVideo(dateToConvert):
try:
global framerate
global renderingInProgress
global outputFolder
global uploadVideo
renderingInProgress = True
dateToConvertStamp = dateToConvert.strftime('%Y%m%d')
outputFilePath = dateToConvertStamp + '.mp4'
console.info('Converting existing image sequence to video... ')
# The following runs out of memory as it is not hardware accelerated, perhaps in the future?
# subprocess.call('cd ' + outputFolder + '&& ffmpeg -y -r 60 -fflags discardcorrupt -i '+dateToConvertStamp+'/%08d.jpg -s hd1080 -vcodec libx265 -crf 20 -preset slow '+ outputFilePath, shell=True)
# The following is not as an efficient codec, but encoding is hardware accelerated and should work for the transient purposes it is used for.
subprocess.call('cd ' + outputFolder + '&& ffmpeg -y -r 60 -fflags discardcorrupt -i '+dateToConvertStamp+'/%08d.jpg -s hd1080 -qscale:v 3 -vcodec mpeg4 '+ outputFilePath, shell=True)
console.info('Image conversion complete: ' + outputFilePath )
if uploadVideo == True:
try:
console.info('Uploading video to YouTube... ')
uploadDescription = 'Timelapse for ' + dateToConvert.strftime('%Y-%m-%d')
subprocess.call('python3 camera.timelapse/camera.timelapse.upload.py --file ' + outputFolder + outputFilePath + ' --title "' + dateToConvertStamp + '" --description "' + uploadDescription + '" --privacyStatus "' + privacy + '"' , shell=True)
except Exception as ex:
console.warn('YouTube upload may have failed! ' + str(ex) )
pass
else:
console.info('To upload the video to YouTube, start the program with the argument: --uploadVideo True ')
renderingInProgress = False
return True
except ffmpeg.Error as ex:
console.error('Could not convert sequence to video. ' + str(ex))
pass
return False
# ------------------------------------------------------------------------------
def cleanup():
while True:
try:
global outputFolder
global retention
now = time.time()
time.sleep(5)
console.info('Starting removal of files older than ' + str(retention) + ' days... ')
for item in os.listdir(outputFolder):
itemPath = os.path.join(outputFolder, item)
itemModified = os.stat(itemPath).st_mtime
itemCompare = now - (retention * 86400)
if itemModified < itemCompare:
if os.path.isdir(itemPath):
shutil.rmtree(itemPath, ignore_errors=True)
else:
os.remove(itemPath)
console.info('Cleanup complete')
time.sleep(86400)
except Exception as ex:
console.error(str(ex))
pass
# === Timelapse Capture ========================================================
try:
echo.clear()
os.chdir('/home/pi')
console.log('Camera (Timelapse) ' + version, '\n ')
console.print('----------------------------------------------------------------------', '\n ', '\n ')
while True:
try:
if keyboard.is_pressed('ctrl+c') or keyboard.is_pressed('esc'):
echo.on()
break
except:
# Keyboard commands will throw an exception in SSH sessions, so ignore
pass
camera.framerate = defaultFramerate
camera.shutter_speed = shutter
try:
camera.start(show_preview=False)
time.sleep(1)
except:
console.warn('Could not start camera. Is it already in use? ', '\n ')
pass
captureThread = threading.Thread(target=captureTimelapse)
captureThread.start()
analysisThread = threading.Thread(target=analyzeLastImages)
analysisThread.start()
if retention > 1:
cleanupThread = threading.Thread(target=cleanup)
cleanupThread.start()
else:
console.warn('Retaining captured files indefinitely! Please ensure that sufficient storage exists or set a retention value. ', '\n ')
while renderVideo:
if renderingInProgress == False:
time.sleep(120)
yesterday = (datetime.date.today() - datetime.timedelta(days = 1))
yesterdayStamp = yesterday.strftime('%Y%m%d')
firstFrameExists = os.path.exists(outputFolder + yesterdayStamp + '/00000001.jpg')
videoExists = os.path.exists(outputFolder + yesterdayStamp + '.mp4')
if firstFrameExists == True and videoExists == False:
convertThread = threading.Thread(target=convertSequenceToVideo, args=(yesterday,))
convertThread.start()
time.sleep(3600)
except KeyboardInterrupt:
camera.close()
echo.on()
sys.exit(1)
except Exception as ex:
console.error(ex)
echo.on()
else:
echo.on()
sys.exit(0)