-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
510 lines (453 loc) · 18.1 KB
/
server.py
File metadata and controls
510 lines (453 loc) · 18.1 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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
from datetime import datetime
import io
import libcamera
import logging
import os
import re
import shutil
import threading
import time
from typing import Dict
from flask import Flask, Response, jsonify, render_template, request
from picamera2 import Picamera2, Controls
from picamera2.encoders import JpegEncoder
from picamera2.outputs import FileOutput
from threading import Condition
from settings import Settings
from timelapse import Timelapse, TimelapseGallery
from utils import check_directory_permissions, brightness, get_cpu_temp, get_cpu_usage, get_day, get_day_and_time, pretty_number, get_awb_mode, generate_pretty_exposure_times, create_folder_if_not_exists, make_thumbnail
from photo_repository import PhotoRepository, Photo
settings = Settings()
settings.load_from_json()
static_dir = "./static/"
settings.photo_directory = static_dir if settings.photo_directory is None else settings.photo_directory
target_dir = settings.photo_directory
static_photos_dir = os.path.join(static_dir, "photos/")
target_photos_dir = os.path.join(target_dir, "photos/")
thumbnails_dir = os.path.join(static_photos_dir, "thumbnails/")
static_timelapse_dir = os.path.join(static_dir, "timelapses/")
target_timelapse_dir = os.path.join(target_dir, "timelapses/")
create_folder_if_not_exists(static_photos_dir)
create_folder_if_not_exists(target_photos_dir)
create_folder_if_not_exists(thumbnails_dir)
create_folder_if_not_exists(static_timelapse_dir)
create_folder_if_not_exists(target_timelapse_dir)
create_folder_if_not_exists("./logs")
Picamera2.set_logging(Picamera2.ERROR)
logging.basicConfig(level=logging.INFO,
format='%(asctime)s :: %(levelname)s :: %(message)s', filename='logs/' + get_day() + '.log')
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"]
camera = Picamera2()
pretty_exposure_times_list = generate_pretty_exposure_times()
photo_repository = PhotoRepository(static_photos_dir)
photo_repository.load_from_json()
timelapse_galleries = TimelapseGallery(static_timelapse_dir)
is_timelapse_ongoing = False
timelapse: Timelapse = None
@app.route("/shoot")
def shoot():
""" Handles the display of the shoot page """
global camera
global logger
camera.stop()
return render_template('shoot.html', active=" shoot")
@app.route("/settings")
def showSettings():
""" Handles the display of the settings page """
return render_template('settings.html', active=" settings", settings=settings)
@app.route("/saveSettings", methods=['POST'])
def saveSettings():
"""
Handles the display of the settings page
Arguments (request body):
photosDirectory - the directory where the photos and timelapses will be saved
"""
toReturn = {"error": False}
try:
input = request.get_json(force=True)
photo_directory = input["photosDirectory"]
if not photo_directory.endswith('/'):
photo_directory += '/'
isPathOK = check_directory_permissions(photo_directory)
if isPathOK:
settings.photo_directory = photo_directory
settings.save_to_json()
else:
toReturn["error"] = True
toReturn["cause"] = "Directory doesn't exist or can't be read or written."
except RuntimeError as e:
logger.warning(str(e))
toReturn["error"] = True
toReturn["cause"] = "Error while saving the settings."
return jsonify(toReturn)
@app.route("/gallery")
def gallery():
""" Handles the display of the photo gallery page """
gallery = photo_repository.organize_photos_by_date()
return render_template('gallery.html', active=" photoGallery", gallery=gallery)
@app.route("/timelapse-gallery")
def timelapse_gallery():
""" Handles the display of the timelpase gallery page """
sorted_galleries = sorted(timelapse_galleries.galleries.items(
), key=lambda item: datetime.strptime(item[0], '%Y-%m-%d_%H-%M-%S'))
display_galleries = [gallery for _, gallery in sorted_galleries]
return render_template('timelapse-gallery.html', active=" timelapseGallery", gallery=display_galleries)
@app.route("/timelapse-gallery/view/<timelapse>")
def view(timelapse):
""" Handles the display of the timelapse gallery page """
display_timelapse = timelapse_galleries.galleries[timelapse]
logger.info("display_timelapse.thumbnails_files")
logger.info(display_timelapse.thumbnails_files)
return render_template('view-timelapse.html', active=" timelapseGallery", timelapse=display_timelapse)
@app.route("/")
@app.route("/preview")
def preview():
""" Handles the display of the preview page """
return render_template('preview.html', active=" preview")
@app.route("/timelapse")
def show_timelapse():
""" Handles the display of the timelapse page """
return render_template('timelapse.html', active=" timelapse")
@app.route("/is_timelapse_ongoing")
def is_timelapse_running():
""" Checks if the timelapse is still ongoing """
global is_timelapse_ongoing
to_return = {}
to_return["is_timelapse_ongoing"] = is_timelapse_ongoing
return jsonify(to_return)
@app.route("/stop_timelapse")
def stop_timelapse():
""" Stops the ongoing timelapse """
global is_timelapse_ongoing
is_timelapse_ongoing = False
to_return = {}
to_return["is_timelapse_ongoing"] = is_timelapse_ongoing
return jsonify(to_return)
@app.route("/update_timelapse")
def update_timelapse():
""" Updates the timelapse page. Returns the full stats so that the timelpase can be displayed on any device calling this API. """
global timelapse
global is_timelapse_ongoing
to_return = {}
if is_timelapse_ongoing and (timelapse is not None):
# to_return["reference_photo"] = "/ref.jpg"
to_return["photos"] = timelapse.photos_list
to_return["photos_to_take"] = timelapse.photos_to_take
to_return["photos_taken"] = len(timelapse.photos_list)
to_return["thumbs"] = timelapse.thumbs_list
to_return["cpu_temp"] = get_cpu_temp()
to_return["cpu_usage"] = get_cpu_usage()
to_return["is_timelapse_ongoing"] = is_timelapse_ongoing
return jsonify(to_return)
class StreamingOutput(io.BufferedIOBase):
""" Used for camera streaming on the preview page """
def __init__(self):
self.frame = None
self.condition = Condition()
def write(self, buf):
with self.condition:
self.frame = buf
self.condition.notify_all()
def genFrames():
""" Generates the frames to be streamed """
global camera
output = StreamingOutput()
camera.configure(camera.create_video_configuration(
main={"size": (1280, 960)}))
output = StreamingOutput()
camera.start_recording(JpegEncoder(), FileOutput(output))
while True:
with output.condition:
output.condition.wait()
frame = output.frame
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
""" Provides the source of the stream on the preview page """
return Response(genFrames(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/doshoot', methods=['POST'])
def do_shoot():
"""
Handles the shot button on the shoot page
Arguments (request body):
iso - the ISO to set
speed - the exposure time to set in ms
wb - the white balance to set
file_format - the file format to save the photo in
"""
global pretty_exposure_times_list
toReturn = {}
try:
input = request.get_json(force=True)
iso = input["iso"]
exposure_time = input["exposureTime"]
wb = input["wb"]
file_format = input["fileFormat"]
capture_config = camera.create_still_configuration(
raw={}, display=None, colour_space=libcamera.ColorSpace.Srgb())
if iso != "Auto":
camera.set_controls({"AnalogueGain": int(iso) / 100})
if exposure_time != -1:
camera.set_controls({"ExposureTime": int(exposure_time)})
if wb != "auto":
camera.set_controls({"AwbMode": get_awb_mode(wb)})
camera.start()
time.sleep(2)
r = camera.switch_mode_capture_request_and_stop(capture_config)
day = get_day()
day_and_time = get_day_and_time()
jpg_path = day_and_time + ".jpg"
jpg_full_path = os.path.join(target_photos_dir, jpg_path)
r.save("main", jpg_full_path)
thumbnail_full_path = os.path.join(thumbnails_dir, jpg_path)
make_thumbnail(jpg_full_path, thumbnail_full_path, 1000, 1000)
if "jpg" not in file_format:
do_delete_photo(jpg_full_path)
dng_path = None
if "dng" in file_format:
dng_path = day_and_time + ".dng"
r.save_dng(target_photos_dir + dng_path)
toReturn["dngPath"] = dng_path
toReturn["fileName"] = day_and_time
toReturn["iso"] = iso
toReturn["exposureTime"] = pretty_exposure_times_list[int(
exposure_time)]
toReturn["wb"] = wb.capitalize()
toReturn["jpgPath"] = jpg_path
toReturn["thumbPath"] = jpg_path
photo = Photo(name=day_and_time, iso=iso, speed=exposure_time, exposure_time=pretty_exposure_times_list[int(exposure_time)],
white_balance=wb.capitalize(), capture_date=day, jpg_path=jpg_path, dng_path=dng_path)
photo_repository.add_photo(photo)
except RuntimeError as e:
logger.warning(str(e))
toReturn["error"] = True
return jsonify(toReturn)
@app.route("/deletephoto", methods=['POST'])
def delete_photo():
"""
Deletes all versions of a photo - both JPG and DNG versions
Arguments (request body):
name - the name of the photo
"""
toReturn = {}
input = request.get_json(force=True)
try:
name = input["name"]
photo = photo_repository.get_photo(name)
except:
toReturn["error"] = True
return jsonify(toReturn)
try:
jpg_path = static_photos_dir + photo.jpg_path
except:
jpg_path = None
is_jpg_deletion_error = not do_delete_photo(jpg_path)
try:
dng_path = static_photos_dir + photo.dng_path
except:
dng_path = None
is_dng_deletion_error = not do_delete_photo(dng_path)
toReturn["error"] = is_jpg_deletion_error or is_dng_deletion_error
if not toReturn["error"]:
photo_repository.remove_photo(name)
thumbnail_path = os.path.join(thumbnails_dir, name + ".jpg")
do_delete_photo(thumbnail_path)
return jsonify(toReturn)
def do_delete_photo(photo_path) -> bool:
"""
Deletes a photo
Arguments:
photo_path - the path of the photo
Returns:
True if the deletion was a success or if the path was None.
"""
if photo_path != None and os.path.exists(photo_path):
try:
os.remove(photo_path)
logger.info("Deleted photo: " + photo_path)
return True
except:
logger.error("Error while deleting: " + photo_path)
return False
else:
return True
@app.route("/deletetimelapse", methods=['POST'])
def delete_timelapse():
"""
Deletes a timelapse
Arguments (request body):
timelapse - the name of the timelapse
"""
toReturn = {}
toReturn["error"] = False
input = request.get_json(force=True)
timelapse: str = input["timelapse"]
static_timelapse_path = os.path.join(static_timelapse_dir, timelapse)
if timelapse != None and os.path.exists(static_timelapse_path):
try:
shutil.rmtree(static_timelapse_path)
logger.info("Timelapse deleted: " + timelapse)
except:
toReturn["error"] = True
return jsonify(toReturn)
if not toReturn["error"]:
timelapse_galleries.remove(timelapse)
return jsonify(toReturn)
def run_timelapse(input):
"""
Runs the timelapse - is meant to be ran in a thread
Arguments:
input - the parameters of the timelapse - see the Timelapse class
"""
global is_timelapse_ongoing
global timelapse
logger.info("Start timelapse")
date_and_time = get_day_and_time()
static_working_dir = os.path.join(static_timelapse_dir, date_and_time)
# relative_tmp_dir = date_and_time + "/tmp/"
os.makedirs(static_working_dir, exist_ok=True)
tmp_dir = os.path.join(static_working_dir, "tmp")
logger.info(tmp_dir)
os.makedirs(tmp_dir, exist_ok=True)
target_working_dir = os.path.join(target_timelapse_dir, date_and_time)
os.makedirs(target_working_dir, exist_ok=True)
timelapse = Timelapse(input)
is_timelapse_ongoing = True
timelapse_galleries.add_timelapse(date_and_time)
preview_config = camera.create_preview_configuration()
capture_config = camera.create_still_configuration(
raw={"size": camera.sensor_resolution})
camera.stop()
camera.configure(preview_config)
camera.set_controls({"AnalogueGain": timelapse.iso / 100})
camera.set_controls({"ExposureTime": timelapse.exposure_time})
camera.set_controls({"AwbMode": timelapse.wb})
camera.start()
time.sleep(2)
reference_path = os.path.join(tmp_dir, "ref.jpg")
while is_timelapse_ongoing and timelapse.is_ongoing():
take_timelapse_photo(capture_config, reference_path,
target_working_dir, tmp_dir, date_and_time)
logger.info("Sleeping for: " + str(timelapse.get_sleep_time()))
time.sleep(timelapse.get_sleep_time())
camera.stop()
time.sleep(2)
os.remove(reference_path)
is_timelapse_ongoing = False
logger.info("Timelapse finished")
def take_timelapse_photo(capture_config: Dict, reference_path: str, working_dir: str, tmp_dir: str, date_and_time: str):
"""
Takes a photo for the ongoin timelapse
Arguments:
capture_config (Dict) - the capture configuration for the camera
reference_path (str) - the path to the reference file for brightness calculation
working_dir (str) - the path to the working directory
tmp_dir (str) - the path to the tmp directory
date_and_time (str) - the start date and time of the timelapse
"""
camera.stop()
camera.set_controls({"AnalogueGain": timelapse.iso / 100})
camera.set_controls({"ExposureTime": timelapse.exposure_time})
camera.start()
timelapse.photos_taken = timelapse.photos_taken + 1
logger.info("==================== Taking photo: " + str(timelapse.photos_taken) +
"/" + str(timelapse.photos_to_take))
filename = "tl_" + \
pretty_number(timelapse.photos_taken, timelapse.photos_to_take) + \
"_" + get_day_and_time() + "_ISO_" + str(timelapse.iso) + "_" + \
pretty_exposure_times_list[timelapse.exposure_time].replace(
'/', '-')
r = camera.switch_mode_capture_request_and_stop(capture_config)
r.save("main", reference_path)
if "dng" in timelapse.file_format:
dng_path = os.path.join(working_dir, filename + ".dng")
r.save_dng("main", dng_path)
timelapse_galleries.add_dng(date_and_time, dng_path)
jpg_path = os.path.join(working_dir, filename + ".jpg")
r.save("main", jpg_path)
photo_brightness = brightness(reference_path)
day_and_time = get_day_and_time()
timelapse.add_photo(filename, day_and_time, photo_brightness)
timelapse.update_settings(photo_brightness)
thumbnail_path = os.path.join(tmp_dir, filename + ".jpg")
make_thumbnail(jpg_path, thumbnail_path, 400, 400)
timelapse.add_thumbnail(
path=thumbnail_path, day_and_time=day_and_time, number=timelapse.photos_taken, iso=timelapse.iso, speed=pretty_exposure_times_list[timelapse.exposure_time], brightness=photo_brightness)
timelapse_galleries.add_thumbnail(date_and_time, filename + ".jpg")
if "jpg" not in timelapse.file_format:
do_delete_photo(jpg_path)
else:
timelapse_galleries.add_jpg(date_and_time, jpg_path)
@app.route('/start_timelapse', methods=['POST'])
def start_timelapse():
"""
Starts the timelapse in a dedicated thread
Arguments (request body):
input - the parameters of the timelapse - see the Timelapse class
"""
to_return = {}
to_return["started"] = False
if not is_timelapse_ongoing:
try:
input = request.get_json(force=True)
x = threading.Thread(target=run_timelapse,
args=(input,), daemon=True)
x.start()
to_return["started"] = True
except RuntimeError as e:
logger.warning(str(e))
to_return["error"] = str(e)
return jsonify(to_return)
def data_from_name(filename: str):
"""
Extracts data from a timelapse file name.
Args:
filename (str): The filename to be analysed.
Returns: A dict containing:
- number: the number of the photo in the sequence,
- date: the date the photo was taken,
- time: the time the photo was taken,
- iso: the ISO value,
- exposure_time: the exposure time.
"""
pattern = "tl_([0-9]+)_([0-9]{4}-[0-9]{2}-[0-9]{2})_([0-9]{2}-[0-9]{2}-[0-9]{2})_ISO_([0-9]+)_([0-9s-]+).jpg"
match = re.match(pattern, filename)
if match:
number, date, time, iso, exposure_time = match.groups()
return {
'number': number,
'date': date,
'time': time,
'iso': iso,
'exposure_time': exposure_time
}
pattern = r"tl_([0-9]+)\.jpg"
match = re.match(pattern, filename)
if match:
number = match.groups()
return {
'number': number,
'date': None,
'time': None,
'iso': None,
'exposure_time': None
}
return None
def format_exposure_time(exposure_time: str) -> str:
"""
Replaces all instances of '-' with '/' in the provided exposure_time.
Args:
exposure_time (str): The string in which to replace dashes with slashes.
Returns:
str: The modified string with dashes replaced by slashes.
"""
if exposure_time is not None:
modified_string = exposure_time.replace('-', '/')
return modified_string
return None
app.jinja_env.filters.update(data_from_name=data_from_name)
app.jinja_env.filters.update(format_exposure_time=format_exposure_time)