-
Notifications
You must be signed in to change notification settings - Fork 3
/
timelapse.py
124 lines (105 loc) · 4.12 KB
/
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
#!/usr/bin/env python3
# [START includes]
from subprocess import check_output
from re import findall
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from video_stitcher import stitch_video
from timestamper import write_timestamps
import argparse
from datetime import datetime
import time
import picamera
import os
import logging
import boto3
import glob
import json
import argparse
parser=argparse.ArgumentParser()
parser.add_argument('-length', '-l', action="store", dest="length", default=60, help='Length in seconds')
parser.add_argument('-interval', '-i', action="store", dest="interval", default=1, help='Interval in seconds')
parser.add_argument('-rotation', '-r', action="store", dest="rotation", default=0, help='Rotation 0, 90, 180, 270')
parser.add_argument('-output', '-o', action="store", dest="output", default="/home/pi/Camera", help='Output full path')
parser.add_argument('-night', '-n', action="store_true", dest="night", help='Night mode')
parser.add_argument('-day', '-d', action="store_false", dest="night", help='Day mode')
args = parser.parse_args()
logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.INFO)
__default_rotation__ = 0
__night_mode__ = bool(args.night)
__length__ = int(args.length)
__interval__ = int(args.interval)
__rotation__ = int(args.rotation)
__output__ = str(args.output)
__output_folder_name__= __output__ + '/output'
def clean_directory():
os.system('rm -R -f '+__output_folder_name__)
def capture_images(length_in_seconds,interval_in_seconds, rotation, n_mode):
count = length_in_seconds / interval_in_seconds
logging.info('Taking {} shots...'.format(count))
print(n_mode)
with picamera.PiCamera() as camera:
if(n_mode):
print("Trying to set exposure to night")
camera.exposure_mode = 'night'
camera.iso = 800
else:
print("No night mode")
camera.start_preview()
print(camera.exposure_mode)
camera.rotation = rotation
time.sleep(2)
for filename in camera.capture_continuous(__output_folder_name__+'/img{counter:06d}.jpg'):
time.sleep(interval_in_seconds) # wait <interval_in_seconds> seconds
count -= 1
if count <= 0:
break
def upload_to_s3(video_folder):
with open("crds.json") as f:
setup = json.load(f)
f.close()
AWS_ACCESS_KEY_ID = setup["AWS_ACCESS_KEY_ID"]
AWS_SECRET_ACCESS_KEY = setup["AWS_SECRET_ACCESS_KEY"]
session = boto3.Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY
)
# Let's use Amazon S3
s3 = session.resource('s3')
bucket_name='pi-camera-images'
for file in list(glob.glob(video_folder + '/*.mp4')):
file_ob = open(file,'r')
file_name=os.path.basename(file_ob.name)
file_ob.close()
data = open(file,'rb')
s3.Bucket(bucket_name).put_object(Key=file_name, Body=data)
# [START]
def main():
# Start with cleanup
start_time = time.time()
if not os.path.exists(__output_folder_name__):
os.makedirs(__output_folder_name__)
logging.info('Running clean script...')
# Check for rotation
if (__rotation__ != None):
rotation = __rotation__
else:
rotation = __default_rotation__
# Take pictures
logging.info('Opening camera...')
capture_images(__length__,__interval__, rotation,__night_mode__)
logging.info('Writing timestamps...')
# Write timestamps to images
write_timestamps(72,32,__output_folder_name__)
logging.info('Captured images {} seconds to run'.format(str(time.time() - start_time)))
# Create video
logging.info('Making video...')
timestamp = str(datetime.now().strftime('%Y-%m-%d_%H%M%S'))
stitch_video('timelapse_'+timestamp+'.mp4','warning',__output_folder_name__,'video','img%06d-resized.jpg')
logging.info('Done!')
logging.info('Creating video {} seconds to run'.format(str(time.time() - start_time)))
upload_to_s3(__output_folder_name__ + '/video')
clean_directory()
if __name__ == '__main__':
main()