This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timelapse.py
executable file
·262 lines (221 loc) · 7.73 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
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
#!/usr/bin/env /usr/local/bin/python3.9
from time import sleep
from datetime import datetime, date, time
from picamera import PiCamera
from fractions import Fraction
from shutil import copyfile
import os
import threading
import sys
import yaml
from logger import *
import locale
import subprocess as sp
from localStoragePy import localStoragePy
from virtualTimer import getCurrentExposure
# Define variables
red = "\033[1;31;38m"
green = "\033[1;32;38m"
endcolor = "\033[0m"
currentDir = os.getcwd()
filePath = ""
total_images = 0
annotation = ""
homedir = sys.path[0]
#shutter_cmd = os.path.join(homedir, 'Timer.py')
shutter_speed = getCurrentExposure()
localStorage = localStoragePy('ekstremedia-timelapse-exposure', 'sqlite')
def infoMsg(text):
now = datetime.now()
timePrint = str('%02d' % now.hour) + ":" + str('%02d' %
now.minute) + ":" + str('%02d' % now.second)
print(greenText("*** ["+timePrint+'] ')+text)
# Welcome screen
infoMsg(redText("Raspberry")+greenText("Pi")+"-timelapse is loading...")
# Load configuration if present
try:
config = yaml.safe_load(open(os.path.join(sys.path[0], "config.yml")))
loadedConf = True
except OSError as e:
errorMsg("Found no configuration file!")
successMsg(str(e))
loadedConf = False
if loadedConf:
infoMsg("Configuration file " + greenText("loaded") + "!")
else:
print("Configuration file "+redText("not") +
" loaded, loading default values.")
if loadedConf:
try:
annotation = config['annotation']
except KeyError:
annotation = ""
try:
filePath = config['filePath']
except KeyError:
filePath = currentDir+"/timelapse/"
try:
filePrefix = config['filePrefix']
except KeyError:
filePrefix = ""
infoMsg("Filename prefix: "+greenText(filePrefix))
try:
iso = config['iso']
except KeyError:
iso = 200
try:
awb = config['white_balance']
except KeyError:
awb = 'cloudy'
try:
interval = config['interval']
except KeyError:
interval = 10
# try:
# shutter_speed = config['shutter_speed']
# except KeyError:
# shutter_speed = 0
# shutter_speed = sp.getoutput(shutter_cmd)
try:
copy_last = config['copy_last']
except KeyError:
copy_last = False
try:
status_filename = config['status_filename']
except KeyError:
status_filename = False
try:
metering = config['metering']
except KeyError:
metering = None
try:
exposure_mode = config['exposure_mode']
except KeyError:
exposure_mode = None
if copy_last:
infoMsg("Copy image to status image " + greenText("on: ") +
"=> " + greenText(str(status_filename)))
def capture():
if not os.path.exists(filePath):
os.makedirs(filePath)
infoMsg("Created folder: " + greenText(filePath))
infoMsg("Set timelapse-folder to: "+greenText(filePath))
infoMsg("")
infoMsg("Starting timelapse in "+greenText(str(interval)) + " ... :) ")
infoMsg("Shutter: " + greenText(str(shutter_speed)) + " microseconds, ISO: " + greenText(str(iso)) + ", taking photo every " + greenText(
str(interval)) + " seconds")
infoMsg("White balance: " + greenText(str(awb)))
if metering:
infoMsg("Metering: " + greenText(str(metering)))
if exposure_mode:
infoMsg("Exposure mode: " + greenText(str(exposure_mode)))
now = datetime.now()
today = os.path.join(filePath, str(now.year), str(
'%02d' % now.month), str('%02d' % now.day))
time = str('%02d' % now.hour) + "_" + str('%02d' %
now.minute) + "_" + str('%02d' % now.second)
timePrint = str('%02d' % now.hour) + ":" + str('%02d' %
now.minute) + ":" + str('%02d' % now.second)
if not os.path.exists(today):
os.makedirs(today)
infoMsg("Created folder: " + greenText(today))
fileName = os.path.join(today + "/" + filePrefix + "_" + time + ".jpg")
take(fileName)
while not sleep(interval):
# Date and time settings
now = datetime.now()
today = os.path.join(filePath, str(now.year), str(
'%02d' % now.month), str('%02d' % now.day))
time = str('%02d' % now.hour)+"_"+str('%02d' %
now.minute)+"_"+str('%02d' % now.second)
if not os.path.exists(today):
os.makedirs(today)
infoMsg("Created folder: " + greenText(today))
fileName = os.path.join(today+"/"+filePrefix+"_"+time+".jpg")
take(fileName)
def set_camera_options(camera):
# Set camera resolution.
# if config['metering']:
# camera.meter_mode = config['metering']
if config['resolution']:
camera.resolution = (
config['resolution']['width'],
config['resolution']['height']
)
# camera.iso = iso
camera.framerate = Fraction(1, 6)
# if config['shutter_speed']:
# camera.shutter_speed = config['shutter_speed']
# else:
# camera.shutter_speed = 0
# printint(sp.getoutput(shutter_cmd))
# shutter = int(sp.getoutput(shutter_cmd))
shutter = getCurrentExposure()
iso = 100
# if (shutter < 2000000):
# iso = 100
# if (shutter >= 2000000):
# iso = 200
# if (shutter >= 4000000):
# iso = 400
if (shutter >= 5000000):
iso = 600
camera.iso = iso
localStorage.setItem('iso', iso)
log(f"Got shutterspeed: {shutter}")
log(f"Set iso: {iso}")
logLastShutterSpeed(shutter)
camera.shutter_speed = int(shutter)
# camera.shutter_speed = 0
if config['white_balance']:
camera.awb_mode = config['white_balance']
else:
camera.awb_mode = 'cloudy'
if config['white_balance_gain']:
camera.awb_gains = (
config['white_balance_gain']['red_gain'],
config['white_balance_gain']['blue_gain']
)
# if config['exposure_mode']:
# camera.exposure_mode = config['exposure_mode']
# infoMsg("Preparing camera...")
sleep(7)
# infoMsg("Camera ready, starting!")
return camera
def take(fileName):
camera = PiCamera()
set_camera_options(camera)
# Capture a picture.
infoMsg("Capturing...")
now = datetime.now()
today = str('%02d' % now.day) + "." + str('%02d' % now.month) + " " + str(now.year) + " "
timeprint = today + str('%02d' % now.hour) + ":" + str('%02d' % now.minute)
# + ":" + str('%02d' % now.second)
#camera.annotate_text = annotation + " - " + timeprint
#camera.annotate_text_size = 20
ass = camera.shutter_speed
ex = camera.exposure_speed
log(f"Actual shutter speed: {ass}")
localStorage.setItem('actualShutterSpeed', ass)
localStorage.setItem('exposure_speed', ex)
# log(f"Actual exposure: {cax}")
camera.capture(fileName)
global total_images
total_images = total_images+1
infoMsg('Captured ' + fileName + ' (#' + str(total_images) + ')')
loglastfile(fileName)
camera.close()
global copy_last
global status_filename
cmd = os.path.join(homedir, 'imgConvert.py')
if os.path.exists(fileName):
log(f"imgconvert_cmd: {cmd}")
cmd_output = sp.getoutput(cmd)
else:
log("Could not find file before imgconvert_cmd, aborted")
if (copy_last and os.path.exists(fileName)):
copyfile(fileName, status_filename)
else:
log("Aborted copying of file")
log("Pausing camera...")
capture()