-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipcamcontrol_webinterface.py
1294 lines (1120 loc) · 46.2 KB
/
ipcamcontrol_webinterface.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
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 24 18:22:54 2014
@author: chuong, gareth
"""
import io
import os
# import celerypano
from datetime import datetime
from urllib import request as urllib_request
import string
import numpy as np
import yaml
from flask import Flask, flash, session, request, render_template, redirect, jsonify, Response
# from flask_debugtoolbar import DebugToolbarExtension
app = Flask(__name__)
app.debug = True
app.secret_key = "e739b9c6a6aaf27cf44bc86330975ad8edb65a65b009c4c0c3469e9082cf0b8a6e902af10e5d31a160291935f48262114a31fc"
app.config.update({
"CELERY_BROKER_URL": "mongodb://localhost:27017/brokerdb",
"CELERY_RESULT_BACKEND": 'mongodb://localhost:27017',
"CELERY_MONGODB_BACKEND_SETTINGS": {
'database': 'backenddb',
'taskmeta_collection': 'celery_taskmeta_collection',
},
"CELERY_TIMEZONE": 'Australia/Canberra',
"CELERY_DISABLE_RATE_LIMITS": True,
"CELERY_IGNORE_RESULT": False
})
# toolbar = DebugToolbarExtension(app)
def prepare():
required_keys = ["scan_order"]
@app.before_request
def initialise_session():
if not "camera_config" in session.keys():
session['camera_config'] = {}
if not "ptz_config" in session.keys():
session['ptz_config'] = {}
if not "pano_config" in session.keys():
session['pano_config'] = {}
if not "pano_config_fn" in session.keys():
session['pano_config_fn'] = None
if not "camera_config_fn" in session.keys():
session['camera_config_fn'] = None
if not "ptz_config_fn" in session.keys():
session['ptz_config_fn'] = None
def remove_submit(d):
if "submit" in d.keys():
del d["submit"]
remove_submit(session['ptz_config'])
remove_submit(session['pano_config'])
remove_submit(session['camera_config'])
@app.route("/scan-orders")
def scan_orders():
from flask import send_file
return send_file(os.path.join("static", "ScanOrders.png"))
def set_zoom(zoom):
url = session['camera_config']["URL_set_zoom"].format(zoom=zoom)
executeURL(url)
return zoom
@app.route('/apply-zoom')
def apply_zoom():
values = list(request.args.keys())
values.extend(list(request.args.values()))
def can_int_cast(s):
try:
int(float(s))
return True
except:
return False
value = next((obj for obj in values if can_int_cast(obj)), None)
if not value:
return "FAIL", 500
set_zoom(value)
@app.route("/api/set-corner/<int:corner>", methods=["POST"])
def set(corner):
if corner in [1, 2]:
try:
pt = get_pan_tilt()
except Exception as e:
print(str(e))
return jsonify(messages=[{"message": "Couldnt access the url: {}".format(
str(e).replace("<", "").replace(">", "")),
"classsuffix": "danger"}])
if corner == 1:
session["1st_corner"] = pt
return jsonify(messages=[{"message": "Set first corner to {},{}".format(*pt),
"classsuffix": "success"}])
elif corner == 2:
session["2nd_corner"] = pt
return jsonify(messages=[{"message": "Set second corner to {},{}".format(*pt),
"classsuffix": "success"}])
else:
return jsonify(messages=[{"message": "mystery corner?",
"classsuffix": "danger"}])
def get_pan_tilt():
# for test
import random
return round(random.uniform(-4, 180)), round(random.uniform(-45, 45))
url = session['ptz_config']["URL_get_pan_tilt"]
ret = session['ptz_config']["RET_get_pan_tilt"]
pan, tilt = executeURL(url, ret)
return pan, tilt
def executeURL(url, return_string=None):
if "http://" not in url:
url = "http://" + url
if return_string is None:
stream = urllib_request.urlopen(url)
elif return_string == "RAW_JPG" or return_string == "RAW_BMP":
try:
import PIL.Image
stream = urllib_request.urlopen(url)
byte_array = io.BytesIO(stream.read())
image = np.array(PIL.Image.open(byte_array))
return image
except:
import tempfile
from scipy import misc
image_filename = os.path.join(tempfile.gettempdir(), "image.jpg")
urllib_request.urlretrieve(url, image_filename)
image = misc.imread(image_filename)
return image
else:
stream = urllib_request.urlopen(url)
output = stream.read(1024).strip()
string_list = return_string.split("*")
string_list = [s for s in string_list if len(s) > 0]
values = []
for s in string_list:
word_list = s.split("{}")
word_list = [w for w in word_list if len(w) > 0]
if len(word_list) == 1:
pos = output.find(word_list[0])
if pos >= 0:
v = output[pos + len(word_list[0]):]
temp_values = v.split("\n")
values.append(temp_values[0].strip())
elif len(word_list) == 2:
pos1 = output.find(word_list[0])
pos2 = output.find(word_list[1], pos1 + len(word_list[0]))
if 0 <= pos1 <= pos2:
values.append(output[pos1 + len(word_list[0]):pos2])
else:
flash('Something went horribly wrong accessing the url.', "error")
if len(values) == 1:
return values[0]
return values
def convert_config(config_in):
if type(config_in) == str:
with open(config_in, 'r') as f:
config_in = yaml.load(f.read())
if "ip" in config_in.keys() or "first_corner" in config_in.keys():
return config_in
def to_numlist(inp):
if type(inp) == list:
return inp
inp = inp.replace("}", "").replace("{", "")
if "x" in inp:
return inp.split("x")
if "," in inp:
return inp.split(",")
return [inp]
translate = {
"IPVAL": "ip",
"USERVAL": "username",
"PASSVAL": "password",
"ImageSizeList": "image_size_list",
"ZoomRange": "zoom_range",
"Zoom_HorFoVList": "horizontal_fov_list",
"Zoom_VirFoVList": "vertical_fov_list",
"ZoomListOut": "zoom_list_out",
"ZoomVal": "zoom_pos",
"FocusVal": "focus_val",
"FocusMode": "focus_mode",
"URL_SetImageSize": "URL_set_image_size",
"URL_SetZoom": "URL_set_zoom",
"URL_SetFocus": "URL_set_focus",
"URL_SetFocusAuto": "URL_set_focus_mode",
"URL_SetFocusManual": "URL_set_focus_manual",
"URL_GetImage": "URL_get_image",
"URL_GetImageSize": "URL_get_image_size",
"URL_GetVideo": "URL_GetVideo",
"URL_GetZoom": "URL_get_zoom",
"URL_GetFocus": "URL_get_focus",
"RET_GetImage": "RET_get_image",
"RET_SetImageSize": "RET_set_image_size",
"RET_SetZoom": "RET_set_zoom",
"RET_SetFocus": "RET_set_focus",
"RET_GetImageSize": "RET_get_image_size",
"RET_GetZoom": "RET_get_zoom",
"RET_GetFocus": "RET_get_focus",
"1stCorner": "first_corner",
"2ndCorner": "second_corner",
"CameraConfigFile": "camera_config_file",
"PanTiltConfigFile": "ptz_config_file",
"CameraName": "camera_name",
"FieldOfView": "camera_fov",
"LocalFolder": "spool_dir",
"MaxPanoNoImages": "max_no_pano_images",
"MinFreeSpace": "min_free_space",
"Overlap": "overlap",
"PanoEndHour": "pano_end_hour",
"PanoFallbackFolder": "pano_fallback_folder",
"PanoGridSize": "pano_grid_size",
"PanoLoopInterval": "pano_loop_interval",
"PanoMainFolder": "pano_main_folder",
"PanoStartHour": "pano_start_hour",
"PanoStartMin": "pano_start_min",
"PanoWaitMin": "pano_wait_min",
"RemoteFolder": "remote_folder",
"RemoteStorageAddress": "remote_storage_address",
"RemoteStoragePassword": "remote_storage_password",
"RemoteStorageUsername": "remote_storage_username",
"ScanOrder": "scan_order",
"UseFocusAtCenter": "use_focus_at_center",
"Zoom": "zoom",
"Type": "type",
"PanRange": "pan_range",
"TiltRange": "tilt_range",
"PanTiltScale": "pan_tilt_scale",
"URL_SetPanTilt": "URL_set_pan_tilt",
"URL_GetPanTilt": "URL_get_pan_tilt",
"RET_GetPanTilt": "RET_get_pan_tilt"
}
needslist = {
"first_corner",
"second_corner",
"pano_grid_size",
"pano_grid_size"
}
scanorders_map = {
"colsright": 0,
"colsleft": 1,
"rowsdown": 2,
"rowsup": 3
}
needsformatstring = {
"URL_set_image_size",
"URL_set_zoom",
"URL_set_focus",
"URL_set_focus_mode",
"URL_set_focus_manual",
"URL_get_image",
"URL_get_image_size",
"URL_GetVideo",
"URL_get_zoom",
"URL_get_focus",
"URL_set_pan_tilt",
"URL_get_pan_tilt"
}
fixstring_map = {
"USERVAL": "{user}",
"PASSVAL": "{pass}",
"IPVAL": "{ip}",
"ZOOMVAL": "{zoom}",
"FOCUSVAL": "{focus}",
"WIDTHVAL": "{width}",
"HEIGHTVAL": "{height}",
"PANVAL": "{pan}",
"TILTVAL": "{tilt}",
"ZOOM_POSITION": "{zoom_position}",
"FOCUS_POSITION": "{focus_position}",
"AUTO":"{mode}"
}
dict_config = {}
for k, v in config_in.items():
if k in translate.keys():
dict_config[translate[k]] = v
for k, v in dict_config.items():
if k in needslist:
dict_config[k] = to_numlist(dict_config[k])
if k in needsformatstring:
for match, replacement in fixstring_map.items():
dict_config[k] = dict_config[k].replace(match, replacement)
if k == "scan_order":
for c in string.punctuation:
v = v.replace(c, "")
dict_config[k] = scanorders_map.get(v.lower(), 0)
return dict_config
def sort_validate_configs(configs_filepaths):
panorama_configs = []
camera_configs = []
ptz_configs = []
failed = []
for config in configs_filepaths:
try:
with open(config, 'r') as f:
d = yaml.load(f)
if "URL_get_image" in d.keys() or "URL_GetImage" in d.keys():
camera_configs.append(config)
elif "camera_name" in d.keys() or "CameraName" in d.keys():
panorama_configs.append(config)
elif "URL_set_pan_tilt" in d.keys() or "URL_SetPanTilt":
ptz_configs.append(config)
else:
failed.append(config)
except Exception as e:
flash(u'Exception {}'.format(str(e)), "error")
if len(failed):
flash(u'configs that didnt fit into any category: {}'.format(", ".join(failed)), 'warning')
return panorama_configs, camera_configs, ptz_configs
from glob import glob
@app.route("/set-focus-mode")
def set_focus_mode():
values = list(request.args.keys())
values.extend(list(request.args.values()))
value = next((obj for obj in values if str(obj).upper() in ['AUTO', 'MANUAL']), None)
if session['camera_config'] is not None and value is not None:
if str(value).upper() == "AUTO":
url = session['camera_config']["set_focus_auto"]
executeURL(url)
elif str(value).upper() == "MANUAL":
url = session['camera_config']["set_focus_manual"]
executeURL(url)
from PIL import Image, ImageDraw
import math
import time
import cv2
def stream_image(fn):
"""Video streaming generator function."""
while True:
frame = ""
try:
img = cv2.imread(fn,cv2.IMREAD_COLOR)
b,frame = cv2.imencode(".jpg", img)
frame = frame.tostring()
# with open(fn, 'rb') as f:
# frame = f.read()
except Exception as e:
print(str(e))
img = Image.new("RGB", (512, 256))
draw = ImageDraw.Draw(img)
pos = float(time.time() * 1000) / 1000
pos = (1 + math.sin(pos)) / 2
pos = pos * (img.size[0] - 50)
draw.multiline_text((pos, img.size[1] / 2),
"NO IMAGE",
fill="white")
del draw
b = io.BytesIO()
img.save(b, "JPEG")
b.seek(0)
frame = b.read()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route("/calibrate")
def calibrate():
return render_template("calibrate.html")
@app.route("/calibration-stream")
def calibration_image():
return Response(stream_image("matches.jpg"),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route("/overview-stream")
def stream_overview():
return Response(stream_image("overview.jpg"),
mimetype='multipart/x-mixed-replace; boundary=frame')
def stream_video():
def frame():
import PIL.Image
# import cv2
url = session['camera_config']["get_video"]
if "http://" not in url:
url = "http://" + url
stream = urllib_request.urlopen(url)
byte = ''
while True:
byte += stream.read(1024)
a = byte.find('\xff\xd8')
b = byte.find('\xff\xd9')
if a != -1 and b != -1:
jpg = byte[a:b + 2]
byte = byte[b + 2:]
byte_array = io.BytesIO(jpg)
image = np.array(PIL.Image.open(byte_array))
# what is going on here?
# Image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
yield image
def generate():
while True:
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame() + b'\r\n')
return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=frame')
def get_savable_pano_config():
"""
returns a yml string of the session variables that are included here
:return:
"""
keys_to_copy = set(session.keys())
return yaml.dump(keys_to_copy)
@app.route("/clear-session")
def clearsesh():
session.clear()
return ""
def get_filename(p):
filename = None
filename = request.args.get('filename', None)
filename = request.args.get('file', None)
while not filename:
values = list(request.args.keys()) + list(request.args.values())
if "filename" in values:
values.remove("filename")
if "file" in values:
values.remove("file")
if len(values):
filename = str(values[0])
break
if p == "pano":
if "camera_name" in session['pano_config'].keys():
filename = str(session['pano_config']['camera_name'])
else:
filename = datetime.now().strftime("pano-%y_%m_%d_%H_%M ")
else:
filename = datetime.now().strftime(p + "-%y_%m_%d_%H_%M ")
if filename == "":
if p == "pano":
if "camera_name" in session['pano_config'].keys():
filename = str(session['pano_config']['camera_name'])
else:
filename = datetime.now().strftime("pano-%y_%m_%d_%H_%M ")
else:
filename = datetime.now().strftime(p + "-%y_%m_%d_%H_%M ")
if not filename.endswith(".yml") and not filename.endswith(".yaml"):
filename = filename + ".yml"
return filename
@app.route("/download-<any('pano','camera','ptz'):p>")
def export_pano_config(p):
from flask import send_file
from io import BytesIO
str_io = BytesIO()
y = bytes(yaml.dump(session[p + '_config'], default_flow_style=False), "utf-8")
str_io.write(y)
str_io.seek(0)
return send_file(str_io, attachment_filename=get_filename(p), as_attachment=True)
@app.route("/save-<any('pano','camera','ptz'):p>")
def save_pano_config(p):
"""
saves a panorama config file to the local disk from the session vars.
:return:
"""
filename = get_filename(p)
with open(filename, 'w') as yml_fh:
yml_fh.write(yaml.dump(session[p + '_config'], default_flow_style=False))
return redirect("/export")
def allowed_file(fn, types):
"""
validates a filename of allowed types
:param fn:
:param types: iterable of extensions
:return:
"""
if fn.split(".")[-1] in (x.replace(".", "") for x in types):
return True
return False
from wtforms import Field
from wtforms import *
class CSVListField(Field):
"""
Comma separated numbers field.
Fails if any of the comma separated values are not comma separated.
Should return a list
"""
widget = widgets.TextInput()
def _value(self):
if self.data:
return "[" + u', '.join([str(x) for x in self.data]) + "]"
else:
return u''
def process_formdata(self, valuelist):
if valuelist:
numbracks = valuelist[0].count("]") + valuelist[0].count("[")
if numbracks > 2 or numbracks == 1:
raise ValueError(self.gettext("Not a comma separated list."))
self.data = [x.strip() for x in valuelist[0].replace("[", "").replace("]", "").split(',')]
else:
self.data = []
try:
iterator = iter(self.data)
except TypeError:
raise ValueError(self.gettext("Not a comma separated list."))
else:
try:
for v in self.data:
v = float(v)
except ValueError:
self.data = None
raise ValueError(self.gettext('One or more values is not a number'))
from pprint import pformat
class CSVListOfListsField(Field):
"""
Comma separated numbers field.
Fails if any of the comma separated values are not comma separated.
Should return a list
"""
widget = widgets.TextArea()
def _value(self):
if self.data:
return pformat(self.data, width=90, indent=2)
else:
return u''
def process_formdata(self, valuelist):
if len(valuelist):
strs = valuelist[0].replace('[', '').split('],')
self.data = [map(float, s.replace(']', '').split(',')).strip() for s in strs]
# self.data = [x.strip() for x in valuelist[0].split(',')]
else:
self.data = []
try:
iterator = iter(self.data)
except TypeError:
raise ValueError(self.gettext("Not a comma separated list."))
else:
try:
for v in self.data:
v = float(v)
except ValueError:
self.data = None
raise ValueError(self.gettext('One or more values is not a number'))
class MustContain(object):
def __init__(self, *args, message=None):
for x in args:
try:
a = str(x)
except:
raise ValueError('Only stringifiable objects may be checked')
self.req_list = [str(x) for x in args]
if not message:
message = u'Field must contain %s'
self.message = message
def __call__(self, form, field):
for check in self.req_list:
if check not in str(field.data):
raise ValidationError(self.message % check)
class IPAddressWithPort(validators.IPAddress):
"""
Validates an IP address. with a port
only works with ipv4
:param message:
Error message to raise in case of a validation error.
"""
def __init__(self, message=None):
self.message = message
def __call__(self, form, field):
value = field.data
valid = False
if value:
valid = self.check_ipv4(value)
if not valid:
message = self.message
if message is None:
message = field.gettext('Invalid IP address or port.')
raise ValidationError(message)
@classmethod
def check_ipv4(cls, value):
if value.count(":") > 1:
return False
if ":" in value:
value, port = value.split(":")
try:
port = int(port)
except:
return False
# ban some ports, and any outside of the max.
if not 1 <= port <= 65535 or port in [21, 20, 53, 68, 123]:
return False
parts = value.split('.')
if len(parts) == 4 and all(x.isdigit() for x in parts):
numbers = list(int(x) for x in parts)
return all(num >= 0 and num < 256 for num in numbers)
return False
class SelectListField(SelectField):
def pre_validate(self, form):
for v, _ in self.choices:
if self.data == v:
self.data = _
break
else:
raise ValueError(self.gettext('Not a valid choice'))
class UserInputForm(Form):
image_size = SelectListField("Image Size",
validators=[validators.Optional()],
choices=[(0, [1920, 1080]), (1, [1280, 720]), (2, [640, 480]), (3, [320, 240])],
coerce=int)
overlap = FloatField("Overlap (%)", validators=[validators.Optional()])
scan_order = SelectField('Scan order',
choices=[(0, 'Cols, right'),
(1, 'Cols, left'),
(2, 'Rows, down'),
(3, 'Rows, up')],
validators=[validators.Optional()],
coerce=int)
submit = SubmitField()
# user_editable = ["1st_corner", "2nd_corner", "overlap", "scan_order", "image_height", "image_width"]
class PanoConfigForm(Form):
camera_name = StringField("Pano/Camera name")
camera_config_file = StringField('Camera config filename')
ptz_config_file = StringField("PTZ config filename")
camera_fov = StringField("Field of View", default="10.9995,6.2503")
overlap = FloatField("Overlap %", default=50.0)
zoom = FloatField("Zoom", default=800.0)
first_corner = CSVListField('First Corner', default=[113, 9])
second_corner = CSVListField('Second Corner', default=[163, -15])
interval = IntegerField("Panorama interval (s)", default=60,
validators=[validators.number_range(max=1440, min=2), validators.optional()])
starttime = IntegerField("Start Time (HHMM)",
validators=[validators.number_range(max=23, min=0), validators.optional()])
stoptime = IntegerField("Stop Time (HHMM)",
validators=[validators.number_range(max=23, min=1), validators.optional()])
spool_dir = StringField("Spooling directory", default='/home/GigaVision/spool')
upload_dir = StringField("Upload directory", default='/home/GigaVision/upload')
server_dir = StringField("Remote Dir", default='/')
server = StringField("Remote storage address", default='sftp.traitcapture.org')
username = StringField("Remote storage username", default="picam")
password = PasswordField("Remote storage password", default="DEFAULT")
max_no_pano_images = IntegerField("Max number of pano images", default=2000)
scan_order = SelectField('Scan order',
choices=[(0, 'Cols, right'),
(1, 'Cols, left'),
(2, 'Rows, down'),
(3, 'Rows, up')],
validators=[validators.Optional()],
coerce=int)
use_focus_at_center = BooleanField('Use focus at center?', default=True)
submit = SubmitField()
class PTZConfigForm(Form):
ip = StringField("IP Address", default="192.168.1.101:81", validators=[IPAddressWithPort(), validators.optional()])
username = StringField("Username", default="admin")
password = PasswordField("Password", default="admin")
HTTP_login = StringField("HTTP login format string",
default="USER={user}&PWD={password}",
validators=[MustContain('{user}', '{password}'),
validators.optional()])
format_url = StringField("URL format",
default="http://{ip}{command}&{HTTP_login}",
validators=[MustContain('{ip}',"{command}",'{HTTP_login}'),
validators.optional()])
type = StringField("Type", default="ServoMotors")
pan_range = CSVListField("Pan Range", default=[0, 356])
tilt_range = CSVListField("Tilt Range", default=[-89, 29])
pan_tilt_scale = FloatField("Pan/Tilt scaling", default=1.0)
URL_set_pan_tilt = StringField("URL_set_pan_tilt", default="/Bump.xml?GoToP={pan}&GoToT={tilt}",
validators=[MustContain("{pan}", '{tilt}'), validators.optional()])
URL_get_pan_tilt = StringField("URL_get_pan_tilt", default="/CP_Update.xml",
validators=[MustContain("{ip}"), validators.optional()])
RET_get_pan_tilt = StringField("RET_get_pan_tilt", default="*<PanPos>{}</PanPos>*<TiltPos>{}</TiltPos>*")
submit = SubmitField()
class CameraConfigForm(Form):
ip = StringField("IP Address", default="192.168.1.101:81", validators=[IPAddressWithPort(), validators.optional()])
username = StringField("Username", default="admin")
password = PasswordField("Password", default="admin")
HTTP_login = StringField("HTTP_login",
default="USER={user}&PWD={password}",
validators=[MustContain('{user}', '{password}'),
validators.optional()])
format_url = StringField("URL format",
default="http://{ip}{command}&{HTTP_login}",
validators=[MustContain('{ip}',"{command}",'{HTTP_login}'),
validators.optional()])
image_size_list = CSVListOfListsField("Image Size list", default=[[1920, 1080], [1280, 720], [640, 480]])
zoom_range = CSVListField("Zoom Range", default=[30, 1000])
zoom_pos = IntegerField("Zoom Value", default=800,
validators=[validators.number_range(max=20000, min=1), validators.optional()])
horizontal_fov_list = CSVListOfListsField('', default=[[50, 150, 250, 350, 450, 550, 650, 750, 850, 950, 1000],
[71.664, 58.269, 47.670, 40.981, 33.177, 25.246, 18.126,
12.782, 9.217, 7.050, 5.824]])
vertical_fov_list = CSVListOfListsField('', default=[[50, 150, 250, 350, 450, 550, 650, 750, 850, 950, 1000],
[39.469, 33.601, 26.508, 22.227, 16.750, 13.002, 10.324,
7.7136, 4.787, 3.729, 2.448]])
zoom_list_out = CSVListField('Zoom list out', default=[80, 336, 592, 848, 1104, 1360, 1616, 1872, 2128, 2384, 2520])
URL_set_image_size = StringField("URL_set_image_size",
default="/cgi-bin/encoder&VIDEO_RESOLUTION=N{width}x{height}",
validators=[MustContain('{width}', '{height}'),
validators.optional()])
URL_set_zoom = StringField("URL_set_zoom",
default="/cgi-bin/encoder&ZOOM=DIRECT,{zoom}",
validators=[MustContain('{zoom}'),
validators.optional()])
URL_set_focus = StringField("URL_set_focus",
default="/cgi-bin/encoder&FOCUS=DIRECT,{focus}",
validators=[MustContain('{focus}'),
validators.optional()])
URL_set_focus_mode = StringField("URL_set_focus_mode",
default="/cgi-bin/encoder&FOCUS={mode}",
validators=[MustContain("{mode}"), validators.optional()])
URL_get_image = StringField("URL_get_image", default="/cgi-bin/encoder&SNAPSHOT",
validators=[validators.optional()])
URL_get_image_size = StringField("URL_get_image_size",
default="/cgi-bin/encoder&VIDEO_RESOLUTION",
validators=[validators.optional()])
URL_get_zoom = StringField("URL_get_zoom",
default="/cgi-bin/encoder&{zoom_position}",
validators=[MustContain("{zoom_position}"),
validators.optional()])
URL_get_focus = StringField("URL_get_focus",
default="/cgi-bin/encoder&{focus_position}",
validators=[MustContain("{focus_position}"),
validators.optional()])
RET_set_image_size = StringField("RET_set_image_size", default='OK: VIDEO_RESOLUTION=''N{}x{}''')
RET_set_zoom = StringField("RET_set_zoom", default='OK: OK: ZOOM=''DIRECT,{}''')
RET_set_focus = StringField("RET_set_focus", default='OK: FOCUS=''DIRECT,{}''')
RET_get_image_size = StringField("RET_get_image_size", default='VIDEO_RESOLUTION=''N{}x{}''')
RET_get_zoom = StringField("RET_get_zoom", default='ZOOM_POSITION=''{}''')
RET_get_focus = StringField("RET_get_focus", default='FOCUS_POSITION=''{}''')
submit = SubmitField()
from pprint import pprint as print
@app.route("/reset-session")
def reset():
session.clear()
return str(session)
@app.route("/export")
def export_view():
files = glob("*.yml")
files.extend(glob("*.yaml"))
pcfg, ccfg, ptzcfg = sort_validate_configs(files)
template_data = {
"pcfg": pcfg,
"ccfg": ccfg,
"ptzcfg": ptzcfg
}
return render_template("export.html", **template_data)
@app.route("/capture", methods=["GET", "POST"])
def capture_view():
user_form = UserInputForm(request.form)
try:
lookup = [(idx, x) for idx, x in enumerate(session["camera_config"]["image_size_list"])]
reverselookup = dict([(str(v), k) for k, v in lookup])
user_form.image_size.choices = lookup
except:
pass
if user_form.validate() and user_form.submit.data:
print(user_form.data)
for k in [x for x in vars(user_form) if not x.startswith("_") and not x == "meta"]:
if k == "image_size":
session["image_width"], session["image_height"] = user_form[k].data
session[k] = user_form[k].data
for k, v in session.items():
try:
user_form[k].data = v
except:
pass
try:
user_form.image_size.data = reverselookup[str(session.get("image_size"))]
except:
pass
user_editable = ["1st_corner", "2nd_corner", "overlap", "scan_order", "image_height", "image_width"]
template_data = {"form": user_form}
return render_template("capture.html", **template_data)
def init_pano_overview():
from PIL import Image, ImageDraw
# get scalings for individual images
scaled_height = int(session.get("overview_scale", 0.2) * session['image_height'])
scaled_width = int(session.get("overview_scale", 0.2) * session["image_width"])
# create overview image
overview = Image.new("RGB", session.get("overview_shape", (1280, 720)), color="black")
draw = ImageDraw.Draw(overview)
# add lines shows rows and columns
for i in range(session["pano_cols"]):
pos = scaled_width * i
coords = [pos, 0, pos, overview.size[1]]
draw.line(coords, fill=255)
for j in range(session["pano_rows"]):
pos = scaled_height * j
coords = [0, pos, overview.size[0], pos]
draw.line(coords, fill=255)
del draw
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "overview.jpg")
try:
overview.save(path, format="JPEG")
except:
return [{"message": "failed to properly overview file to {}".format(path),
"classsuffix": "warning"}]
return []
@app.route("/api/calculate-grid", methods=["POST"])
def calculate_pano_grid():
"""
calculates the panorama fov and grid.
:return:
"""
messages = []
try:
pan0, tilt0 = session['1st_corner']
pan1, tilt1 = session['2nd_corner']
except:
# flash("First Corner or Second Corner not set. Aborting","error")
return jsonify(messages=[{"message": "First Corner or Second Corner not set. Aborting",
"classsuffix": "danger"}])
if not (session.get("image_width", None) and session.get("image_height", None)):
return jsonify(messages=[{"message": "No image width/height, set it first.",
"classsuffix": "danger"}])
HFoV = abs(float(pan0) - float(pan1))
VFoV = abs(float(tilt0) - float(tilt1))
if VFoV <= HFoV <= 2 * VFoV:
session['HFoV'] = HFoV
session['VFoV'] = VFoV
else:
return jsonify(messages=[{"message": "invalid Fov: h{} v{}".format(HFoV, VFoV),
"classsuffix": "danger"}])
lr = float(pan0) <= float(pan1)
tb = float(tilt0) >= float(tilt1)
pan_min = float(pan0) if lr else float(pan1)
pan_max = float(pan0) if not lr else float(pan1)
max_tilt = float(tilt0) if tb else float(tilt1)
min_tilt = float(tilt0) if not tb else float(tilt1)
print(max_tilt)
print(min_tilt)
session['top_left_corner'] = [pan_min, max_tilt]
session['bottom_right_corner'] = [pan_max, min_tilt]
session['pano_rows'] = int(round((max_tilt - min_tilt) / session['VFoV'] / (1.0 - session.get('overlap', 0))))
session['pano_cols'] = int(round((pan_max - pan_min) / session['HFoV'] / (1.0 - session.get('overlap', 0))))
session['pano_total'] = session['pano_rows'] * session['pano_cols']
# Gigapan Sticher only works with 2000 images max
if session['pano_total'] > 2000:
messages.append({"message": 'Total number of images {} is more than {}'.format(session['pano_total'], 2000),
"classsuffix": "warning"})
# todo: set panogridsize info values.
if session['pano_rows'] >= 0 and session['pano_cols'] >= 0:
scale = 2
# todo: set image size info values here
image_width, image_height = (session['image_width'], session['image_height'])
while scale > 0:
scaled_height = int(scale * image_height)
scaled_width = int(scale * image_width)
if scaled_height * session['pano_rows'] <= 1080 and \
scaled_width * session['pano_cols'] <= 1920:
break
scale = scale - 0.001
session["overview_scale"] = scale
# session['PanoOverViewScale'] = scale
# session['PanoOverViewHeight'] = scaled_height * session['pano_rows']
# session['PanoOverViewWidth'] = scaled_width * session['pano_cols']
# todo; return some updated infor about the state of the panorama variables in the session.
overview_msgs = init_pano_overview()
messages.extend(overview_msgs)
# updatePanoOverView()
else:
return jsonify(messages=[
{"message": "Invalid number of panorama rows or columns (you need at least 1 of each. Please try again.",
"classsuffix": "warning"}])
# todo: return some meaningful json values and represent them in the ui.
messages.append({"message": 'successfully calculated the grid'.format(session['pano_total'], 2000),
"classsuffix": "danger"})
return jsonify(messages=messages)
@app.route("/", methods=['POST', 'GET'])
@app.route("/config", methods=['POST', 'GET'])
def config():
"""
Loads a yaml config file from posted file or get filename
:return:
"""
pano_config_dict = {
"camera_config_file": str,
"ptz_config_file": str,