-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig.py
1469 lines (1428 loc) · 61.3 KB
/
config.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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Blueprint, Response, flash, g, redirect, render_template, request, url_for, current_app
from flask import send_file
from werkzeug.exceptions import abort
from raspiCamSrv.camCfg import CameraCfg
from raspiCamSrv.camera_pi import Camera
from raspiCamSrv.version import version
from picamera2 import Picamera2
import os
import shutil
import json
from raspiCamSrv.auth import login_required
import logging
# Try to import platform, which does not exist in Bullseye Picamera2 distributions
try:
import picamera2.platform as Platform
usePlatform = True
except ImportError:
usePlatform = False
bp = Blueprint("config", __name__)
logger = logging.getLogger(__name__)
@bp.route("/config")
@login_required
def main():
g.hostname = request.host
g.version = version
# Although not directly needed here, the camara needs to be initialized
# in order to load the camera-specific parameters into configuration
cam = Camera().cam
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.curMenu = "config"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
def doSyncTransform(hflip: bool, vflip: bool, tgt: list) -> bool:
""" Synchronize the transform settings of target configurations with reference
Parameters:
hflip: horizontal flip
vflip: vertical flip
tgt : list of configurations for which to adjust the aspect ratio
Return:
True if transform settings for Live View was changed
"""
logger.debug("In doSyncTransform")
ret = False
cfg = CameraCfg()
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
for conf in tgt:
if conf == "Live View":
if cfglive.transform_hflip != hflip \
or cfglive.transform_vflip != vflip:
ret = True
cfglive.transform_hflip = hflip
cfglive.transform_vflip = vflip
elif conf == "Photo":
cfgphoto.transform_hflip = hflip
cfgphoto.transform_vflip = vflip
elif conf == "Raw Photo":
cfgraw.transform_hflip = hflip
cfgraw.transform_vflip = vflip
elif conf == "Video":
cfgvideo.transform_hflip = hflip
cfgvideo.transform_vflip = vflip
logger.debug("doSyncTransform %s", ret)
return ret
def doSyncAspectRatio(ref: tuple, tgt: list) -> bool:
""" Synchronize the aspect ratio of target configurations with reference
Parameters:
ref: reference size (width, height)
tgt: list of configurations for which to adjust the aspect ratio
Return:
True if Stream Size for Live View was changed
"""
logger.debug("In doSyncAspectRatio")
ret = False
cfg = CameraCfg()
aspRatioRef = ref[0] / ref[1]
for conf in tgt:
if conf == "Live View":
size = cfg.liveViewConfig.stream_size
elif conf == "Photo":
size = cfg.photoConfig.stream_size
elif conf == "Raw Photo":
size = cfg.rawConfig.stream_size
elif conf == "Video":
size = cfg.videoConfig.stream_size
else:
size = None
if not size is None:
log = f"Changed Stream Size for {conf} from {size} to "
aspRatio = size[0] / size[1]
if aspRatio != aspRatioRef:
width = size[0]
height = round(size[0] / aspRatioRef)
if not (height % 2) == 0:
height += 1
if height > cfg.cameraProperties.pixelArraySize[1]:
height = cfg.cameraProperties.pixelArraySize[1]
width = round(height * aspRatioRef)
if not (width % 2) == 0:
width += 1
size = (width, height)
sm = "custom"
for mode in cfg.sensorModes:
if mode.size[0] == width \
and mode.size[1] == height:
sm = str(mode.id)
break
logger.debug(log + str(size))
if conf == "Live View":
cfg.liveViewConfig.stream_size = size
cfg.liveViewConfig.sensor_mode = sm
ret = True
elif conf == "Photo":
cfg.photoConfig.stream_size = size
cfg.photoConfig.sensor_mode = sm
elif conf == "Raw Photo":
cfg.rawConfig.stream_size = size
cfg.rawConfig.sensor_mode = sm
elif conf == "Video":
cfg.videoConfig.stream_size = size
cfg.videoConfig.sensor_mode = sm
else:
pass
logger.debug("doSyncAspectRatio %s", ret)
return ret
@bp.route("/syncAspectRatio", methods=("GET", "POST"))
@login_required
def syncAspectRatio():
logger.debug("In syncAspectRatio")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
lastTab = sc.lastConfigTab
selTab = request.form.get("activecfgtab")
if selTab != "-":
lastTab = selTab
sc.lastConfigTab = lastTab
syncAspectRatio = not request.form.get("syncaspectratio") is None
sc.syncAspectRatio = syncAspectRatio
logger.debug("syncAspectRatio - lastTab: %s", lastTab)
if syncAspectRatio == True:
if lastTab == "cfglive":
aspRef = cfglive.stream_size
aspTgt = ["Photo", "Raw Photo", "Video"]
doSyncAspectRatio(aspRef, aspTgt)
elif lastTab == "cfgphoto":
aspRef = cfgphoto.stream_size
aspTgt = ["Live View", "Raw Photo", "Video"]
doSyncAspectRatio(aspRef, aspTgt)
elif lastTab == "cfgraw":
aspRef = cfgraw.stream_size
aspTgt = ["Live View", "Photo", "Video"]
doSyncAspectRatio(aspRef, aspTgt)
elif lastTab == "cfgvideo":
aspRef = cfgvideo.stream_size
aspTgt = ["Live View", "Photo", "Raw Photo"]
doSyncAspectRatio(aspRef, aspTgt)
else:
pass
Camera.resetScalerCropRequested = True
Camera().restartLiveStream()
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
def findTuningFile(tuning_file: str, dir=None) -> str:
"""Find the given tuning file and return its path
Code has been copied from Picamera2.load_tuning_file(...)
Args:
- tuning_file (str): filename of tuning file
- dir (str, optional): Directory to search. If None, search standard installation dirs
Returns:
- str: Path of tuning file; None, if not found
"""
tfPath = None
if dir is not None:
dirs = [dir]
else:
if usePlatform:
platform_dir = "vc4" if Picamera2.platform == Platform.Platform.VC4 else "pisp"
dirs = [os.path.expanduser("~/libcamera/src/ipa/rpi/" + platform_dir + "/data"),
"/usr/local/share/libcamera/ipa/rpi/" + platform_dir,
"/usr/share/libcamera/ipa/rpi/" + platform_dir]
else:
dirs = [os.path.expanduser("~/libcamera/src/ipa/rpi/vc4/data"),
"/usr/local/share/libcamera/ipa/rpi/vc4",
"/usr/share/libcamera/ipa/rpi/vc4"]
for directory in dirs:
file = os.path.join(directory, tuning_file)
if os.path.isfile(file):
tfPath = file
return tfPath
def isTuningFile(file: str, folder:str) -> bool:
logger.debug("In isTuningFile")
logger.debug("isTuningFile - file=%s", file)
logger.debug("isTuningFile - folder=%s", folder)
res = False
try:
tf = Picamera2.load_tuning_file(file, folder)
res = True
except RuntimeError as e:
res = False
logger.debug("isTuningFile - res=%s", res)
return res
def getTuningFiles(folder, defFile) -> list:
"""Create a list of all .json files in the given folder
Args:
- folder (str): Folder to search
- defFile (str): Name of default file
Returns:
- list: list with filenames of .json files
"""
tfl = []
defFileFound = False
if folder is not None:
if os.path.exists(folder):
for f in os.listdir(folder):
if os.path.isfile(os.path.join(folder, f)):
if f == defFile:
defFileFound = True
nam, ext = os.path.splitext(f)
if ext.lower() == ".json":
tfl.append(f)
if defFile:
if defFileFound == False:
tfl.append(defFile)
return tfl
@bp.route("/tuningCfg", methods=("GET", "POST"))
@login_required
def tuningCfg():
logger.debug("In tuningCfg")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfgtuning"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
err = None
msg = ""
restart = False
if sc.isTriggerRecording:
err = "Please go to 'Trigger' and stop the active process before changing the tuning configuration"
msg = err
if sc.isPhotoSeriesRecording:
err = "Please go to 'Photo Series' and stop the active process before changing the tuning configuration"
msg = err
if sc.isVideoRecording == True:
err = "Please stop video recording before changing the tuning configuration"
msg = err
if not err:
loadTuningFile = not request.form.get("loadtuningfile") is None
fd = request.form["tuningfolder"]
if fd == "":
fd = None
fn = request.form["tuningfile"]
if loadTuningFile:
if isTuningFile(fn, fd) == True:
tc.tuningFolder = fd
tc.tuningFile = fn
tc.loadTuningFile = loadTuningFile
restart = True
else:
msg = "Specify an existing tuning file before activating to load it"
else:
tc.tuningFolder = fd
tc.tuningFile = fn
if tc.loadTuningFile != loadTuningFile:
restart = True
tc.loadTuningFile = loadTuningFile
if restart:
Camera().restartLiveStream()
if len(msg) > 0:
flash(msg)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/customTuning", methods=("GET", "POST"))
@login_required
def customTuning():
logger.debug("In customTuning")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfgtuning"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
err = None
msg = ""
restart = False
if tc.loadTuningFile == True:
if sc.isTriggerRecording:
err = "Please go to 'Trigger' and stop the active process before changing the tuning configuration"
msg = err
if sc.isPhotoSeriesRecording:
err = "Please go to 'Photo Series' and stop the active process before changing the tuning configuration"
msg = err
if sc.isVideoRecording == True:
err = "Please stop video recording before changing the tuning configuration"
msg = err
if not err:
fd = tc.tuningFolder
fn = tc.tuningFile
fdCustom = current_app.static_folder + "/tuning"
if fd == fdCustom:
msg = "No changes. Custom folder was already set."
else:
try:
os.makedirs(fdCustom, exist_ok=True)
tc.tuningFolder = fdCustom
except Exception as e:
msg = "Error while creating custom folder " + fdCustom + ":" + e
if msg == "":
if fn != "":
if isTuningFile(fn, fdCustom) == True:
if tc.loadTuningFile == True:
msg = "Tuning file switched to custom file."
restart = True
else:
if isTuningFile(fn, None) == True:
fpCustom = os.path.join(fdCustom, fn)
fpDefault = findTuningFile(fn, None)
if fpDefault is not None:
try:
shutil.copyfile(fpDefault, fpCustom)
msg = "Tuning file " + fn + " copied to custom directory."
if tc.loadTuningFile == True:
restart = True
except Exception as e:
logger.debug("error while copying tuning file: %s", str(e))
msg = "Tuning file directory switched to custom directory, but tuning file " + fn + " could not be copied."
fn = ""
if tc.loadTuningFile == True:
restart = True
tc.loadTuningFile = False
else:
tc.tuningFile = ""
if tc.loadTuningFile == True:
restart = True
tc.loadTuningFile = False
else:
tc.tuningFile = ""
if tc.loadTuningFile == True:
restart = True
tc.loadTuningFile = False
if restart:
Camera().restartLiveStream()
if len(msg) > 0:
flash(msg)
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/defaultTuning", methods=("GET", "POST"))
@login_required
def defaultTuning():
logger.debug("In defaultTuning")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfgtuning"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
err = None
msg = ""
restart = False
if tc.loadTuningFile == True:
if sc.isTriggerRecording:
err = "Please go to 'Trigger' and stop the active process before changing the tuning configuration"
msg = err
if sc.isPhotoSeriesRecording:
err = "Please go to 'Photo Series' and stop the active process before changing the tuning configuration"
msg = err
if sc.isVideoRecording == True:
err = "Please stop video recording before changing the tuning configuration"
msg = err
if not err:
fd = tc.tuningFolder
fn = tc.tuningFile
fdDefault = tc.tuningFolderDef
if fd == fdDefault:
msg = "No changes. Default folder was already set."
else:
tc.tuningFolder = fdDefault
if fn != "":
if isTuningFile(fn, fd) == True:
if tc.loadTuningFile == True:
msg = "Tuning file switched to default file."
restart = True
else:
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, fd) == True:
tc.tuningFile = fn
if tc.loadTuningFile == True:
msg = "Tuning file switched to default file."
restart = True
else:
tc.tuningFile = ""
if tc.loadTuningFile == True:
restart = True
if restart:
Camera().restartLiveStream()
if len(msg) > 0:
flash(msg)
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/deleteTuningFile", methods=("GET", "POST"))
@login_required
def deleteTuningFile():
logger.debug("In deleteTuningFile")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfgtuning"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
restart = False
if tc.isDefaultFolder == True:
msg = "You cannot delete a tuning file from the default folder"
else:
fd = tc.tuningFolder
fn = tc.tuningFile
fp = findTuningFile(fn, fd)
if fp is not None:
os.remove(fp)
msg = f"Tuning File deleted: {fp}"
else:
msg = "Tuning file not found"
tc.tuningFile = ""
if tc.loadTuningFile == True:
restart = True
tc.loadTuningFile = False
tfl = getTuningFiles(tc.tuningFolder, None)
if len(tfl) > 0:
fn = sc.activeCameraModel + ".json"
found = False
for f in tfl:
if f == fn:
tc.tuningFile = fn
found = True
break
if found == False:
tc.tuningFile = tfl[0]
else:
logger.debug("deleteTuningFile - No more tuning files in custom folder")
fn = sc.activeCameraModel + ".json"
tc.tuningFolder = None
logger.debug("deleteTuningFile - fn=%s tuningFolder=%s isTuningFile=%s", fn, tc.tuningFolder, isTuningFile(fn, tc.tuningFolder))
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
logger.debug("deleteTuningFile - tc.tuningFile set to %s", tc.tuningFile)
else:
tc.loadTuningFile = False
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if restart:
Camera().restartLiveStream()
if msg != "":
flash(msg)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/downloadTuningFile", methods=("GET", "POST"))
@login_required
def downloadTuningFile():
logger.debug("In downloadTuningFile")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfgtuning"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
fd = tc.tuningFolder
fn = tc.tuningFile
fp = findTuningFile(fn, fd)
if fp is not None:
msg = f"Downloading {fn}"
flash(msg)
return send_file(
fp,
as_attachment=True,
download_name=fn
)
else:
msg = "Tuning file not found"
flash(msg)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/uploadTuningFile", methods=("GET", "POST"))
@login_required
def uploadTuningFile():
logger.debug("In uploadTuningFile")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfgtuning"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
msg = ""
if tc.tuningFolder is None:
msg = "You may only upload to a custom folder!"
else:
if os.path.exists(tc.tuningFolder) == False:
try:
os.makedirs(tc.tuningFolder)
except Exception as e:
msg = f"Error creating folder {tc.tuningFolder}: {str(e)}"
if msg == "":
if "tuningfile" not in request.files:
msg = "No file to save"
else:
files = request.files.getlist("tuningfile")
countSel = len(files)
#tf = request.files["tuningfile"]
logger.debug("uploadTuningFile - %s files selected", countSel)
countUp = 0
for tf in files:
fn = tf.filename
logger.debug("uploadTuningFile - selected file: %s", fn)
nam, ext = os.path.splitext(fn)
if ext.lower() == ".json":
fp = os.path.join(tc.tuningFolder, fn)
tf.save(fp)
msg = f"Tuning file saved as {fp}."
countUp += 1
if countSel > 1:
msg = f"{countUp} of {countSel} files uploaded."
if countUp < countSel:
msg = msg + " Not all files were .json files."
if msg != "":
flash(msg)
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/liveViewCfg", methods=("GET", "POST"))
@login_required
def liveViewCfg():
logger.debug("In liveViewCfg")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfglive"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
err = None
if sc.isTriggerRecording:
err = "Please go to 'Trigger' and stop the active process before changing the configuration"
msg = err
if not err:
transform_hflip = not request.form.get("LIVE_transform_hflip") is None
cfglive.transform_hflip = transform_hflip
transform_vflip = not request.form.get("LIVE_transform_vflip") is None
cfglive.transform_vflip = transform_vflip
colour_space = request.form["LIVE_colour_space"]
cfglive.colour_space = colour_space
buffer_count = int(request.form["LIVE_buffer_count"])
cfglive.buffer_count = buffer_count
queue = not request.form.get("LIVE_queue") is None
cfglive.queue = queue
stream = request.form["LIVE_stream"]
sensor_mode = request.form["LIVE_sensor_mode"]
if sensor_mode == "custom":
size_width = int(request.form["LIVE_stream_size_width"])
if not (size_width % 2) == 0:
err = "Stream Size (width, height) must be even"
size_height = int(request.form["LIVE_stream_size_height"])
if not (size_height % 2) == 0:
err = "Stream Size (width, height) must be even"
if stream == "lores":
if cfgphoto.stream == "main":
if size_width > cfgphoto.stream_size[0] \
or size_height > cfgphoto.stream_size[1]:
err = "lores Stream Size must not exceed main Stream Size (Photo)"
if not err \
and cfgvideo.stream == "main":
if size_width > cfgvideo.stream_size[0] \
or size_height > cfgvideo.stream_size[1]:
err = "lores Stream Size must not exceed main Stream Size (Video)"
if stream == "main":
if cfgphoto.stream == "lores":
if size_width < cfgphoto.stream_size[0] \
or size_height < cfgphoto.stream_size[1]:
err = "lores Stream Size (Photo) must not exceed main Stream Size"
if not err \
and cfgvideo.stream == "lores":
if size_width < cfgvideo.stream_size[0] \
or size_height < cfgvideo.stream_size[1]:
err = "lores Stream Size (Video) must not exceed main Stream Size"
if not err:
cfglive.stream = stream
cfglive.sensor_mode = sensor_mode
cfglive.stream_size = (size_width, size_height)
cfglive.stream_size_align = not request.form.get("LIVE_stream_size_align") is None
else:
mode = sm[int(sensor_mode)]
if stream == "lores":
if cfgphoto.stream == "main":
if mode.size[0] > cfgphoto.stream_size[0] \
or mode.size[1] > cfgphoto.stream_size[1]:
err = "lores Stream Size must not exceed main Stream Size (Photo)"
if not err \
and cfgvideo.stream == "main":
if mode.size[0] > cfgvideo.stream_size[0] \
or mode.size[1] > cfgvideo.stream_size[1]:
err = "lores Stream Size must not exceed main Stream Size (Video)"
if stream == "main":
if cfgphoto.stream == "lores":
if mode.size[0] < cfgphoto.stream_size[0] \
or mode.size[1] < cfgphoto.stream_size[1]:
err = "lores Stream Size (Photo) must not exceed main Stream Size"
if not err \
and cfgvideo.stream == "lores":
if mode.size[0] < cfgvideo.stream_size[0] \
or mode.size[1] < cfgvideo.stream_size[1]:
err = "lores Stream Size (Video) must not exceed main Stream Size"
if not err:
cfglive.stream = stream
cfglive.sensor_mode = sensor_mode
cfglive.stream_size = mode.size
cfglive.stream_size_align = not request.form.get("LIVE_stream_size_align") is None
format = request.form["LIVE_format"]
cfglive.display = None
cfglive.encode = cfglive.stream
if sc.syncAspectRatio == True:
doSyncAspectRatio(cfglive.stream_size, ["Photo", "Raw Photo", "Video"])
Camera.resetScalerCropRequested = True
doSyncTransform(transform_hflip, transform_vflip, ["Photo", "Raw Photo", "Video"])
Camera().restartLiveStream()
msg = ""
if err:
msg = err
if sc.raspiModelLower5:
if cfglive.stream == "lores":
if format == "YUV420":
cfglive.format = format
else:
if msg != "":
msg = msg + "\n"
msg = msg + "For Raspberry Pi models < 5, the lowres stream format must be YUV"
else:
cfglive.format = format
else:
cfglive.format = format
if cfglive.stream != "lores":
if msg != "":
msg = msg + "\n"
msg = msg + "WARNING: If you do not set Stream to 'lores', the Live Stream cannot be shown parallel to other activities!"
if len(msg) > 0:
flash(msg)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/addLiveViewControls", methods=("GET", "POST"))
@login_required
def addLiveViewControls():
logger.debug("In addLiveViewControls")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cc = cfg.controls
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfglive"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
err = None
if sc.isTriggerRecording:
err = "Please go to 'Trigger' and stop the active process before changing the configuration"
if not err:
for key, value in cc.dict().items():
if value[0] == True:
if key not in cfg.liveViewConfig.controls:
cfg.liveViewConfig.controls[key] = value[1]
Camera().restartLiveStream()
if err:
flash(err)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/remLiveViewControls", methods=("GET", "POST"))
@login_required
def remLiveViewControls():
logger.debug("In remLiveViewControls")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfglive"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
err = None
if sc.isTriggerRecording:
err = "Please go to 'Trigger' and stop the active process before changing the configuration"
if not err:
cnt = 0
for ctrl in cfg.liveViewConfig.controls:
logger.debug("Checking checkbox ID:" + "sel_LIVE_" + ctrl)
if request.form.get("sel_LIVE_" + ctrl) is not None:
cnt += 1
logger.debug("Nr controls: %s - selected: %s", len(cfg.liveViewConfig.controls), cnt)
if cnt > 0:
if cnt < len(cfg.liveViewConfig.controls):
while cnt > 0:
for ctrl in cfg.liveViewConfig.controls:
if request.form.get("sel_LIVE_" + ctrl) is not None:
ctrlDel = ctrl
break
del cfg.liveViewConfig.controls[ctrlDel]
cnt -= 1
Camera().restartLiveStream()
else:
msg="At least one control must remain in the configuration"
flash(msg)
else:
msg="No controls were selected"
flash(msg)
if err:
flash(err)
return render_template("config/main.html", sc=sc, tc=tc, cp=cp, sm=sm, rf=rf, cfglive=cfglive, cfgphoto=cfgphoto, cfgraw=cfgraw, cfgvideo=cfgvideo, cfgrf=cfgrf, cfgs=cfgs, tfl=tfl)
@bp.route("/photoCfg", methods=("GET", "POST"))
@login_required
def photoCfg():
logger.debug("In photoCfg")
g.hostname = request.host
g.version = version
cfg = CameraCfg()
cp = cfg.cameraProperties
sm = cfg.sensorModes
rf = cfg.rawFormats
sc = cfg.serverConfig
tc = cfg.tuningConfig
sc.lastConfigTab = "cfgphoto"
cfgs = cfg.cameraConfigs
cfglive = cfg.liveViewConfig
cfgphoto = cfg.photoConfig
cfgraw = cfg._rawConfig
cfgvideo =cfg.videoConfig
cfgrf = cfg.rawFormats
if tc.tuningFile == "":
fn = sc.activeCameraModel + ".json"
if isTuningFile(fn, tc.tuningFolder) == True:
tc.tuningFile = fn
tfl = getTuningFiles(tc.tuningFolder, tc.tuningFile)
if request.method == "POST":
err = None
if sc.isTriggerRecording:
err = "Please go to 'Trigger' and stop the active process before changing the configuration"
if not err:
transform_hflip = not request.form.get("FOTO_transform_hflip") is None
cfgphoto.transform_hflip = transform_hflip
transform_vflip = not request.form.get("FOTO_transform_vflip") is None
cfgphoto.transform_vflip = transform_vflip
colour_space = request.form["FOTO_colour_space"]
cfgphoto.colour_space = colour_space
buffer_count = int(request.form["FOTO_buffer_count"])
cfgphoto.buffer_count = buffer_count
queue = not request.form.get("FOTO_queue") is None
cfgphoto.queue = queue
stream = request.form["FOTO_stream"]
sensor_mode = request.form["FOTO_sensor_mode"]
if sensor_mode == "custom":
size_width = int(request.form["FOTO_stream_size_width"])
if not (size_width % 2) == 0:
err = "Stream Size (width, height) must be even"
size_height = int(request.form["FOTO_stream_size_height"])
if not (size_height % 2) == 0:
err = "Stream Size (width, height) must be even"
if stream == "lores":
if cfglive.stream == "main":
if size_width > cfglive.stream_size[0] \
or size_height > cfglive.stream_size[1]:
err = "lores Stream Size must not exceed main Stream Size (Live View)"
if not err \
and cfgvideo.stream == "main":
if size_width > cfgvideo.stream_size[0] \
or size_height > cfgvideo.stream_size[1]:
err = "lores Stream Size must not exceed main Stream Size (Video)"
if stream == "main":
if cfglive.stream == "lores":
if size_width < cfglive.stream_size[0] \
or size_height < cfglive.stream_size[1]:
err = "lores Stream Size (Live View) must not exceed main Stream Size"
if not err \
and cfgvideo.stream == "lores":
if size_width < cfgvideo.stream_size[0] \
or size_height < cfgvideo.stream_size[1]:
err = "lores Stream Size (Video) must not exceed main Stream Size"
if not err:
cfgphoto.stream = stream
cfgphoto.sensor_mode = sensor_mode
cfgphoto.stream_size = (size_width, size_height)
cfgphoto.stream_size_align = not request.form.get("FOTO_stream_size_align") is None
else:
mode = sm[int(sensor_mode)]
if stream == "lores":
if cfglive.stream == "main":
if mode.size[0] > cfglive.stream_size[0] \
or mode.size[1] > cfglive.stream_size[1]:
err = "lores Stream Size must not exceed main Stream Size (Live View)"
if not err \
and cfgvideo.stream == "main":
if mode.size[0] > cfgvideo.stream_size[0] \
or mode.size[1] > cfgvideo.stream_size[1]:
err = "lores Stream Size must not exceed main Stream Size (Video)"
if stream == "main":
if cfglive.stream == "lores":
if mode.size[0] < cfglive.stream_size[0] \
or mode.size[1] < cfglive.stream_size[1]:
err = "lores Stream Size (Live View) must not exceed main Stream Size"
if not err \
and cfgvideo.stream == "lores":
if mode.size[0] < cfgvideo.stream_size[0] \
or mode.size[1] < cfgvideo.stream_size[1]:
err = "lores Stream Size (Video) must not exceed main Stream Size"
if not err:
cfgphoto.stream = stream
cfgphoto.sensor_mode = sensor_mode
cfgphoto.stream_size = mode.size