-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmodels.py
More file actions
1085 lines (961 loc) · 47.8 KB
/
models.py
File metadata and controls
1085 lines (961 loc) · 47.8 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
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
# SPDX-FileCopyrightText: (C) 2021 - 2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import json
import os
import socket
import traceback
import uuid
import zipfile
from functools import partial
import requests
import numpy as np
import paho.mqtt.client as mqtt
from PIL import Image
from django.core.files.base import ContentFile
from django.core.validators import FileExtensionValidator, MinValueValidator, MaxValueValidator
from django.db import models, transaction
from django.conf import settings
from django.contrib.sessions.models import Session
from django.contrib.auth.models import User
from django.utils.text import get_valid_filename
from django.core.files import File
from scene_common.camera import Camera as ScenescapeCamera, CameraPose as ScenescapeCameraPose
from scene_common.geometry import Region as ScenescapeRegion, Tripwire as ScenescapeTripwire
from scene_common.glb_top_view import generateOrthoView, getMeshSize
from scene_common.mesh_util import extractMeshFromGLB, extractMeshFromPointCloud
from scene_common.mqtt import PubSub
from scene_common.options import *
from scene_common.scene_model import SceneModel as ScenescapeScene
from scene_common.scenescape import SceneLoader
from scene_common.timestamp import get_epoch_time
from manager.validators import validate_map_file, validate_glb, validate_map_corners_lla
from manager.fields import ListField
from scene_common import log
# FIXME - when entire app has transitioned to using APIs
# move this definition to views.py
def sendUpdateCommand(scene_id=None, camera_data=None):
broker = os.environ.get("BROKER")
auth = os.environ.get("BROKERAUTH")
rootcert = os.environ.get("BROKERROOTCERT")
autocalibration = os.environ.get("AUTOCALIBRATION")
if rootcert is None:
rootcert = "/run/secrets/certs/scenescape-ca.pem"
cert = os.environ.get("BROKERCERT")
if broker is not None:
client = PubSub(auth, cert, rootcert, broker)
try:
client.connect()
except socket.gaierror as e:
log.error("Unable to connect", e)
else:
if scene_id:
client.publish(PubSub.formatTopic(PubSub.CMD_SCENE_UPDATE, scene_id = scene_id), "update")
url = f"https://{autocalibration}/v1/scenes/{scene_id}/registration"
headers = {
"Content-Type": "application/json"
}
try:
response = requests.patch(url, headers=headers, verify=rootcert, timeout=10)
log.info("Status code: %s", response.status_code)
try:
log.info("Response: %s", response.json())
except ValueError:
log.info("Non-JSON response: %s", response.text)
except requests.exceptions.RequestException as e:
log.warning("Failed to send update command to autocalibration service: %s", e)
if camera_data:
client.publish(PubSub.formatTopic(PubSub.CMD_KUBECLIENT), json.dumps(camera_data), qos=2)
msg = client.publish(PubSub.formatTopic(PubSub.CMD_DATABASE), "update", qos=1)
if not msg.is_published() and msg.rc == mqtt.MQTT_ERR_SUCCESS:
client.loopStart()
msg.wait_for_publish()
client.loopStop()
return
def sanitizeZipPath(instance, filename):
"""! Sanitize the filename, remove any existing file, and return a safe path under MEDIA_ROOT."""
safe_filename = get_valid_filename(os.path.basename(filename))
full_path = os.path.join(settings.MEDIA_ROOT, safe_filename)
os.makedirs(settings.MEDIA_ROOT, exist_ok=True)
if os.path.exists(full_path):
os.remove(full_path)
return safe_filename
class FailedLogin(models.Model):
ip = models.GenericIPAddressField(null=True)
delay = models.FloatField(default=0.0)
class Meta:
db_table = "db_failedlogin_entry"
verbose_name = "FailedLogin Entry"
verbose_name_plural = "FailedLogin Entries"
class UserSession(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
session = models.OneToOneField(Session, on_delete=models.CASCADE)
class SceneImport(models.Model):
zipFile = models.FileField(null=True, upload_to=sanitizeZipPath, blank=False, editable=True)
class Scene(models.Model):
DEFAULT_MESH_ROTATION = 90.0
id = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
name = models.CharField(max_length=200, unique=True)
map_type = models.CharField("Map Type", max_length=20, choices=MAP_TYPE_CHOICES, default='map_upload', null=True)
thumbnail = models.ImageField(default=None, null=True, editable=False)
map = models.FileField("Scene map as .glb or .ply or image or .zip or video", default=None, null=True, blank=True,
validators=[FileExtensionValidator(["glb","png","jpeg","jpg","zip","ply","mp4",
"mov", "mkv", "webm", "avi"]),
validate_map_file])
scale = models.FloatField("Pixels per meter", default=None, null=True, blank=True,
validators=[MinValueValidator(np.nextafter(0, 1))])
use_tracker = models.BooleanField("Use tracker", choices=BOOLEAN_CHOICES, default=True, blank=True)
rotation_x = models.FloatField("X Rotation (degrees)", default=0.0, null=True, blank=False)
rotation_y = models.FloatField("Y Rotation (degrees)", default=0.0, null=True, blank=False)
rotation_z = models.FloatField("Z Rotation (degrees)", default=0.0, null=True, blank=False)
translation_x = models.FloatField("X Translation (meters)", default=0.0,
null=True, blank=False)
translation_y = models.FloatField("Y Translation (meters)", default=0.0,
null=True, blank=False)
translation_z = models.FloatField("Z Translation (meters)", default=0.0,
null=True, blank=False)
scale_x = models.FloatField("X Scale", default=1.0, null=True, blank=False)
scale_y = models.FloatField("Y Scale", default=1.0, null=True, blank=False)
scale_z = models.FloatField("Z Scale", default=1.0, null=True, blank=False)
map_processed = models.DateTimeField("Last Processed at", null=True, editable=False)
output_lla = models.BooleanField("Output geospatial coordinates", choices=BOOLEAN_CHOICES, default=False, null=True)
map_corners_lla = models.JSONField("Geospatial coordinates of the four map corners in JSON format",
default=None, null=True, blank=True, validators=[validate_map_corners_lla],
help_text=(
"Provide the array of four map corners geospatial coordinates (lat, long, alt).\n"
"Required only if 'Output geospatial coordinates' is set to `Yes`.\n"
"Expected order: starting from the bottom-left corner counterclockwise.\nExpected JSON format: "
"'[ [lat1, lon1, alt1], [lat2, lon2, alt2], [lat3, lon3, alt3], [lat4, lon4, alt4] ]'"))
# Geospatial map settings
geospatial_provider = models.CharField("Geospatial Map Provider", max_length=20,
choices=GEOSPATIAL_PROVIDERS,
default='google', null=True, blank=True,
help_text="The map provider used for geospatial maps (google or mapbox)")
map_zoom = models.FloatField("Map Zoom Level", default=15.0, null=True, blank=True,
validators=[MinValueValidator(0.0)],
help_text="Zoom level for the geospatial map view")
map_center_lat = models.FloatField("Map Center Latitude", default=None, null=True, blank=True,
help_text="Center latitude for the geospatial map view")
map_center_lng = models.FloatField("Map Center Longitude", default=None, null=True, blank=True,
help_text="Center longitude for the geospatial map view")
map_bearing = models.FloatField("Map Bearing/Rotation (degrees)", default=0.0, null=True, blank=True,
help_text="Rotation angle for the geospatial map view in degrees")
trs_matrix = models.JSONField(
"Transformation matrix (Translation, Rotation, Scale) coordinates to LLA (Latitude, Longitude, Altitude)",
default=None, null=True, blank=True, editable=False,
help_text="4x4 transformation matrix (translation-rotation-scale) stored as JSON [[...], [...], [...], [...]]"
)
camera_calibration = models.CharField("Calibration Type", max_length=20, choices=CALIBRATION_CHOICES, default=MANUAL)
polycam_data = models.FileField(blank=True, null=True, validators=[FileExtensionValidator(["zip"])])
dataset_dir = models.CharField(blank=True, max_length=200, editable=False)
output_dir = models.CharField(blank=True, max_length=200, editable=False)
output = models.CharField(null=True, blank=True, max_length=500, editable=False)
retrieval_conf = models.JSONField(null=True, blank=True, editable=False)
global_descriptor_file = models.FileField(blank=True, null=True,
validators=[FileExtensionValidator(["h5"])],
editable=False)
number_of_localizations = models.IntegerField(
verbose_name="Number Of Localizations", default=50, null=True, blank=True)
global_feature = models.CharField(
verbose_name="Global Feature Matching Algorithm", max_length=200,
default="netvlad", blank=True)
def _getDefaultSiftDict():
return {"sift": dict()}
local_feature = models.JSONField(default=_getDefaultSiftDict, null=True, blank=True)
def _getDefaultNnRatioDict():
return {"NN-ratio": dict()}
matcher = models.JSONField(default=_getDefaultNnRatioDict, null=True, blank=True)
minimum_number_of_matches = models.IntegerField(
verbose_name="Minimum Number Of Matches", default=20, null=True, blank=True)
polycam_hash = models.CharField(null=True, blank=True, max_length=100,
editable=False)
apriltag_size = models.FloatField("AprilTag Size (meters)", max_length=10, default=0.162, null=True, blank=True)
regulated_rate = models.FloatField("Regulate Rate (Hz)", default=30, blank=True, validators=[MinValueValidator(0.001)])
external_update_rate = models.FloatField("Max External Update Rate (Hz)", default=30, blank=True, validators=[MinValueValidator(0.001)])
inlier_threshold = models.FloatField("Feature Match Confidence Threshold", default=0.5, blank=True, validators=[MinValueValidator(0.0)])
def __str__(self):
return self.name
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._original_map = self.map
self._original_scale = self.scale
self._original_rotation_x = self.rotation_x
self._original_rotation_y = self.rotation_y
self._original_rotation_z = self.rotation_z
self._original_translation_x = self.translation_x
self._original_translation_y = self.translation_y
self._original_translation_z = self.translation_z
self._original_calibration_mode = self.camera_calibration
self._original_polycam_data = self.polycam_data
self._original_matcher = self.matcher
self._original_num_of_localization = self.number_of_localizations
self._original_global_feature = self.global_feature
self._original_local_feature = self.local_feature
self._original_min_num_of_matches = self.minimum_number_of_matches
self._original_apriltag_size = self.apriltag_size
self._original_inlier_threshold = self.inlier_threshold
# Flag to indicate if mesh is from generateMesh flow (already aligned by mapping service)
self._from_generate_mesh = False
return
def changedCalibrationParams(self):
return self._original_calibration_mode != self.camera_calibration or \
self._original_matcher != self.matcher or \
self._original_num_of_localization != self.number_of_localizations or \
self._original_global_feature != self.global_feature or \
self._original_local_feature != self.local_feature or \
self._original_min_num_of_matches != self.minimum_number_of_matches or \
self._original_scale != self.scale or \
self._original_apriltag_size != self.apriltag_size or \
self._original_inlier_threshold != self.inlier_threshold
def regenerateThumbnail(self):
return self.map != self._original_map or \
self._original_rotation_x != self.rotation_x or \
self._original_rotation_y != self.rotation_y or \
self._original_rotation_z != self.rotation_z or \
self._original_translation_x != self.translation_x or \
self._original_translation_y != self.translation_y or \
self._original_translation_z != self.translation_z
def autoAlignSceneMap(self):
"""! Rotate the glb from y-up to z-up and translate to first
quadrant. This is only executed for direct GLB file uploads,
not for meshes generated by the generateMesh flow (which are
already aligned by the mapping service).
"""
# Skip alignment if mesh is from generateMesh flow (already aligned by mapping service)
if getattr(self, '_from_generate_mesh', False):
return
if self.map and os.path.splitext(self.map.path)[1].lower() == ".glb":
self.rotation_x = self.DEFAULT_MESH_ROTATION
self.rotation_y = 0.0
self.rotation_z = 0.0
mesh, _ = extractMeshFromGLB(self.map.path, rotation=np.array([self.rotation_x, self.rotation_y, self.rotation_z]))
width, height, depth = getMeshSize(mesh)
self.translation_x = width/2
self.translation_y = height/2
self.translation_z = depth/2
return
def resetRotation(self):
self.rotation_x = 0.0
self.rotation_y = 0.0
self.rotation_z = 0.0
return
def resetTranslation(self):
self.translation_x = 0.0
self.translation_y = 0.0
self.translation_z = 0.0
return
def saveThumbnail(self):
img_data, pixels_per_meter = generateOrthoView(self, self.map.path)
self.scale = pixels_per_meter
img = Image.fromarray(np.uint8(img_data))
with ContentFile(b'') as imgfile:
img.save(imgfile, format='PNG')
self.thumbnail.save(self.name + '_2d.png', imgfile, save=False)
return
def save(self, *args, **kwargs):
updated_scene = self.id
self.dataset_dir = f"{os.getcwd()}/datasets/{self.name}"
self.output_dir = f"{os.getcwd()}/datasets/{self.name}/output_dir"
try:
glb_from_zip = None
# use glb from zip uploaded in map and copy zip to polycam data
if (self._original_map != self.map) and \
os.path.splitext(self.map.name)[1].lower() == ".zip":
glb_from_zip = self.map
self.polycam_data = self.map
self.camera_calibration = MARKERLESS
# use glb from zip uploaded in polycam data
if (self._original_polycam_data != self.polycam_data):
glb_from_zip = self.polycam_data
if self.changedCalibrationParams():
self.map_processed = None
super().save(*args, **kwargs)
if glb_from_zip:
try:
with zipfile.ZipFile(glb_from_zip.path, 'r') as zf:
base_file_name = zf.namelist()[0].split("/")[0]
glb_content = zf.read(os.path.join(base_file_name, "raw.glb"))
self.map.save(f"{self.name}.glb", ContentFile(glb_content), save=False)
except KeyError as e:
log.info(f"Using old map file {self.map.path} as glb not found in zip file {glb_from_zip.name}.")
self.autoAlignSceneMap()
if self.regenerateThumbnail() or glb_from_zip:
if not self.map:
self.thumbnail = None
self.map_processed = None
else:
ext = os.path.splitext(self.map.path)[1].lower()
if ext == ".ply":
glb_file = extractMeshFromPointCloud(self.map.path)
with open(glb_file, 'rb') as f:
self.map.save(os.path.basename(glb_file), File(f), save=False)
self.saveThumbnail()
elif ext == ".glb":
# Note: autoAlignSceneMap() only performs alignment for uploaded GLB files.
# For generated meshes (_from_generate_mesh == True), autoAlignSceneMap() returns early.
# This asymmetry is intentional; see method implementation for details.
# Only auto-align if a new GLB file was uploaded
if self._original_map != self.map:
self.autoAlignSceneMap()
self.saveThumbnail()
else:
self.thumbnail = None
self.resetRotation()
self.resetTranslation()
super().save(*args, **kwargs)
except FileNotFoundError as e:
log.error(f"Failed to save scene , {str(e)}")
transaction.on_commit(partial(sendUpdateCommand, scene_id = updated_scene))
return
def delete(self, *args, **kwargs):
super(Scene, self).delete(*args, **kwargs)
transaction.on_commit(sendUpdateCommand)
if self.map:
storage, path = self.map.storage, self.map.path
storage.delete(path)
return
def roiJSON(self):
jdata = []
for region in self.regions.all():
rdict = {'title': region.name, 'points': [], 'uuid':str(region.uuid),
'volumetric': region.volumetric, 'height': region.height, 'buffer_size': region.buffer_size}
thresholds, range_max = region.get_sectors()
rdict['sectors'] = {'thresholds':thresholds, 'range_max':range_max}
# provide points in the right order, so ROI polygon is formed properly
for point in region.points.all().order_by('sequence'):
# FIXME - UI should be handling scaling
rdict['points'].append([point.x, point.y])
jdata.append(rdict)
return json.dumps(jdata)
def tripwireJSON(self):
jdata = []
for tripwire in self.tripwires.all():
rdict = {'title': tripwire.name, 'points': [], 'uuid':str(tripwire.uuid)}
for point in tripwire.points.all():
# FIXME - UI should be handling scaling
rdict['points'].append([point.x, point.y])
jdata.append(rdict)
return json.dumps(jdata)
@property
def scenescapeScene(self):
mScene = SceneLoader.sceneWithName(self.name)
if not mScene:
mScene = ScenescapeScene(self.name, self.map.path if self.map else None, self.scale)
mScene.use_tracker = self.use_tracker
mScene.output_lla = self.output_lla
mScene.map_corners_lla = self.map_corners_lla
mScene.mesh_translation = [self.translation_x, self.translation_y, self.translation_z]
mScene.mesh_rotation = [self.rotation_x, self.rotation_y, self.rotation_z]
try:
self.scenescapeSceneUpdateSensors(mScene)
self.scenescapeSceneUpdateRegions(mScene)
SceneLoader.addScene(mScene)
except:
traceback.print_exc()
return mScene
def scenescapeSceneUpdateSensors(self, mScene, force=False):
# print("Updating sensors")
for sensor in self.sensor_set.all():
if sensor.type == "camera" and (force or sensor.sensor_id not in mScene.cameras):
cam = sensor.cam
if cam.transforms is None:
continue
sInfo = cam.transformation
if cam.intrinsics_fx != None and cam.intrinsics_fy != None \
and cam.intrinsics_cx != None and cam.intrinsics_cy != None:
sInfo['intrinsics'] = [cam.intrinsics_fx, cam.intrinsics_fy,
cam.intrinsics_cx, cam.intrinsics_cy]
elif cam.intrinsics_fx != None:
sInfo['intrinsics'] = [cam.intrinsics_fx]
if cam.intrinsics_fy != None:
sInfo['intrinsics'].append(cam.intrinsics_fy)
if cam.distortion_k1 != None and cam.distortion_k2 != None \
and cam.distortion_p1 != None and cam.distortion_p2 != None \
and cam.distortion_k3 != None:
sInfo['distortion'] = [cam.distortion_k1, cam.distortion_k2,
cam.distortion_p1, cam.distortion_p2, cam.distortion_k3]
mScene.cameras[sensor.sensor_id] = ScenescapeCamera(
sensor.sensor_id, sInfo, resolution=(cam.width, cam.height))
return
def createSceneScapeRegion(self, existing, region):
info = {'area': "poly"}
if hasattr(region, 'area'):
info['area'] = region.area
if hasattr(region, 'map_x') and region.map_x is not None:
info['center'] = (region.map_x, region.map_y)
if hasattr(region, 'radius') and region.radius is not None:
info['radius'] = region.radius
uiPoints = region.points.all()
if len(uiPoints):
info['points'] = [(pt.x, pt.y) for pt in uiPoints]
elif info['area'] == "poly":
return
if hasattr(region, 'sensor_id'):
region_id = region.sensor_id
else:
region_id = region.name
if region_id in existing:
existing[region_id].updatePoints(info)
else:
if hasattr(region, 'uuid'):
uuid = region.uuid
else:
uuid = region_id
existing[region_id] = ScenescapeRegion(uuid, region_id, info)
return
def scenescapeSceneUpdateRegions(self, mScene):
oldRegions = list(mScene.regions.keys())
for region in self.regions.all():
self.createSceneScapeRegion(mScene.regions, region)
newRegions = list(mScene.regions.keys())
delRegions = list(set(oldRegions) - set(newRegions))
for k in delRegions:
mScene.regions.pop(k)
oldTripwires = list(mScene.tripwires.keys())
info = {}
for tripwire in self.tripwires.all():
uiPoints = tripwire.points.all()
if len(uiPoints) == 0:
continue
info['points'] = [(pt.x, pt.y) for pt in uiPoints]
if tripwire.name in mScene.tripwires:
mScene.tripwires[tripwire.name].updatePoints(info)
else:
mScene.tripwires[tripwire.name] = ScenescapeTripwire(tripwire.uuid, tripwire.name, info)
newTripwires = list(mScene.tripwires.keys())
delTripwires = list(set(oldTripwires) - set(newTripwires))
for k in delTripwires:
mScene.tripwires.pop(k)
oldSensors = list(mScene.sensors.keys())
for sensor in self.sensor_set.all():
if sensor.type != "generic":
continue
sensor = SingletonSensor.objects.get(pk=sensor.id)
self.createSceneScapeRegion(mScene.sensors, sensor)
newSensors = list(mScene.sensors.keys())
delSensors = list(set(oldSensors) - set(newSensors))
for k in delSensors:
mScene.sensors.pop(k)
return
def wssConnection(self):
log.info("Getting wss connection string.")
return "wss://localhost/mqtt"
class ChildScene(models.Model):
child = models.OneToOneField(Scene, default=None, null=True, blank=True,
on_delete=models.CASCADE, related_name="parent")
child_name = models.CharField("Child Name", default=None, max_length=200, null=True, blank=True)
remote_child_id = models.UUIDField("Remote Child ID", default=None, null=True, blank=True, unique=True)
parent = models.ForeignKey(Scene, null=False, blank=False,
on_delete=models.CASCADE, related_name="children")
child_type = models.CharField(default="local", max_length=15, blank=False)
class Meta:
constraints = [
models.CheckConstraint(
check=models.Q(child__isnull=False, child_name__isnull=True) | models.Q(child__isnull=True, child_name__isnull=False),
name="%(app_label)s_%(class)s_either_child_or_child_name"
),
models.UniqueConstraint(
name="%(app_label)s_%(class)s_local_child_unique_relationships",
fields=["child", "parent"],
),
models.UniqueConstraint(
name="%(app_label)s_%(class)s_remote_child_unique_relationships",
fields=["child_name", "parent"],
),
models.CheckConstraint(
name="%(app_label)s_%(class)s_prevent_self_follow",
check=~models.Q(child=models.F("parent")),
),
]
transform1 = models.FloatField(default=1.0, null=True, blank=True)
transform2 = models.FloatField(default=0.0, null=True, blank=True)
transform3 = models.FloatField(default=0.0, null=True, blank=True)
transform4 = models.FloatField(default=0.0, null=True, blank=True)
transform5 = models.FloatField(default=0.0, null=True, blank=True)
transform6 = models.FloatField(default=1.0, null=True, blank=True)
transform7 = models.FloatField(default=0.0, null=True, blank=True)
transform8 = models.FloatField(default=0.0, null=True, blank=True)
transform9 = models.FloatField(default=0.0, null=True, blank=True)
transform10 = models.FloatField(default=0.0, null=True, blank=True)
transform11 = models.FloatField(default=1.0, null=True, blank=True)
transform12 = models.FloatField(default=0.0, null=True, blank=True)
transform13 = models.FloatField(default=0.0, null=True, blank=True)
transform14 = models.FloatField(default=0.0, null=True, blank=True)
transform15 = models.FloatField(default=0.0, null=True, blank=True)
transform16 = models.FloatField(default=1.0, null=True, blank=True)
transform_type = models.CharField(max_length=10, choices=CHILD_SCENE_TRANSFORM_CHOICES,
default=MATRIX)
host_name = models.CharField("Hostname or IP", max_length=200, null=True, blank=True)
mqtt_username = models.CharField("MQTT Username", max_length=200, null=True, blank=True)
mqtt_password = models.CharField("MQTT Password", max_length=200, null=True, blank=True)
retrack = models.BooleanField("Retrack objects", choices=BOOLEAN_CHOICES, default=True, blank=True)
@property
def cameraPose(self):
return ScenescapeCameraPose(ScenescapeCameraPose.arrayToDictionary(
[self.transform1, self.transform2, self.transform3, self.transform4,
self.transform5, self.transform6, self.transform7, self.transform8,
self.transform9, self.transform10, self.transform11, self.transform12,
self.transform13, self.transform14, self.transform15, self.transform16],
self.transform_type), None)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
transaction.on_commit(sendUpdateCommand)
return
def delete(self, *args, **kwargs):
super().delete(*args, **kwargs)
transaction.on_commit(sendUpdateCommand)
return
class PubSubACL(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='acls')
topic = models.CharField(choices=TOPIC_CHOICES, max_length=50)
access = models.IntegerField(choices=ACCESS_CHOICES, default=0)
class Meta:
constraints = [
models.UniqueConstraint(
fields=["user", "topic"],
name="%(app_label)s_%(class)s_unique_user_topic"
)
]
def __str__(self):
return f"ACL for {self.user.username} on {self.topic} (Access choice: {self.access})"
class CalibrationMarker(models.Model):
marker_id = models.CharField(max_length=50, primary_key=True)
apriltag_id = models.CharField(max_length=10)
dims = ListField(default=list)
scene = models.ForeignKey(Scene, on_delete=models.CASCADE)
def __str__(self):
return self.marker_id
class Sensor(models.Model):
sensor_id = models.CharField(max_length=20, default=None, unique=True, verbose_name="Sensor ID")
name = models.CharField(max_length=200, unique=True)
sensor_type_choices = (('camera', 'Camera'),
('generic', 'generic'))
type = models.CharField(max_length=200, choices=sensor_type_choices)
scene = models.ForeignKey(Scene, null=True, on_delete=models.SET_NULL)
icon = models.ImageField(default=None, null=True, blank=True)
# map = models.ImageField(default=None, null=True, blank=False)
def __str__(self):
return self.name
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._original_sensor_id = self.sensor_id
self._original_name = self.name
def calibrateString(self):
return "calibrate-" + self.type
def areaJSON(self):
rdict = {'area': self.singletonsensor.area,
'radius': self.singletonsensor.radius,
'x': self.singletonsensor.map_x,
'y': self.singletonsensor.map_y,
'points': []}
for point in self.singletonsensor.points.all():
rdict['points'].append([point.x, point.y])
sectors = self.singletonsensor.get_sectors()
rdict['sectors'] = {'thresholds':sectors[0], 'range_max':sectors[1]}
return json.dumps(rdict)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
transaction.on_commit(sendUpdateCommand)
return
def delete(self, *args, **kwargs):
super().delete(*args, **kwargs)
transaction.on_commit(sendUpdateCommand)
return
def delete(self, *args, **kwargs):
# Check if an icon file also needs to be deleted
if self.icon:
storage, path = self.icon.storage, self.icon.path
super().delete(*args, **kwargs)
storage.delete(path)
else:
super().delete(*args, **kwargs)
return
class Cam(Sensor):
DEFAULT_INTRINSICS = {"fx":570.0,"fy":570.0,"cx":320.0,"cy":240.0}
command = models.CharField(default=None, max_length=512, null=True,
verbose_name="Camera (Video Source)")
camerachain = models.CharField(default=None, max_length=64, null=True, verbose_name="Camera Chain")
threshold = models.FloatField(default=None, null=True, blank=True)
aspect = models.CharField(default=None, max_length=64, null=True, blank=True)
# allow for null value for backward compatibility, defaults to 'AUTO' if null
cv_subsystem = models.CharField(default='AUTO', max_length=64, null=True, blank=False,
verbose_name="Decode Device", choices=CV_SUBSYSTEM_CHOICES)
undistort = models.BooleanField(default=False, null=False, blank=False, verbose_name="Undistort")
transforms = ListField(blank=True, default=list)
transform_type = models.CharField(max_length=26, choices=CAM_TRANSFORM_CHOICES,
default=POINT_CORRESPONDENCE)
width = models.IntegerField(default=640, null=False, blank=False)
height = models.IntegerField(default=480, null=False, blank=False)
scene_x = models.IntegerField(default=None, null=True, blank=True)
scene_y = models.IntegerField(default=None, null=True, blank=True)
scene_z = models.IntegerField(default=None, null=True, blank=True)
intrinsics_fx = models.FloatField(
default=None, null=True, blank=True, validators=[MinValueValidator(0.001)])
intrinsics_fy = models.FloatField(
default=None, null=True, blank=True, validators=[MinValueValidator(0.001)])
intrinsics_cx = models.FloatField(
default=None, null=True, blank=True, validators=[MinValueValidator(0.001)])
intrinsics_cy = models.FloatField(
default=None, null=True, blank=True, validators=[MinValueValidator(0.001)])
distortion_k1 = models.FloatField(default=None, null=True, blank=True)
distortion_k2 = models.FloatField(default=None, null=True, blank=True)
distortion_p1 = models.FloatField(default=None, null=True, blank=True)
distortion_p2 = models.FloatField(default=None, null=True, blank=True)
distortion_k3 = models.FloatField(default=None, null=True, blank=True)
sensor = models.CharField(max_length=512, null=True, blank=True)
sensorchain = models.CharField(max_length=64, null=True, blank=True)
sensorattrib = models.CharField(max_length=64, null=True, blank=True)
window = models.BooleanField(default=False)
usetimestamps = models.BooleanField(default=False)
virtual = models.CharField(max_length=512, null=True, blank=True)
debug = models.BooleanField(default=False)
override_saved_intrinstics = models.BooleanField(default=False)
frames = models.IntegerField(null=True, blank=True, validators=[MinValueValidator(1)])
stats = models.BooleanField(default=False)
waitforstable = models.BooleanField(default=False)
preprocess = models.BooleanField(default=False)
realtime = models.BooleanField(default=False)
faketime = models.BooleanField(default=False)
modelconfig = models.CharField(max_length=512, null=True, blank=True, verbose_name="Model Config", default='model_config.json')
rootcert = models.CharField(max_length=64, null=True, blank=True)
cert = models.CharField(max_length=64, null=True, blank=True)
cvcores = models.IntegerField(null=True, blank=True)
ovcores = models.IntegerField(null=True, blank=True)
unwarp = models.BooleanField(default=False)
ovmshost = models.CharField(max_length=64, null=True, blank=True)
framerate = models.IntegerField(null=True, blank=True, validators=[MinValueValidator(1)])
maxcache = models.IntegerField(null=True, blank=True, validators=[MinValueValidator(1)])
filter = models.CharField(max_length=64, choices=CAM_FILTER_CHOICES, default=NONE)
disable_rotation = models.BooleanField(default=False)
maxdistance = models.FloatField(null=True, blank=True, validators=[MinValueValidator(0.001)])
use_camera_pipeline = models.BooleanField("Use Camera Pipeline", default=False, null=True, blank=True,
help_text="Enable to directly apply the Camera Pipeline string in the camera VA pipeline instead of generating it automatically from camera settings.")
camera_pipeline = models.TextField(max_length=5000, null=True, blank=True,
help_text="The camera pipeline string in gst-launch-1.0 syntax which will be applied in camera VA pipeline once 'Use Camera Pipeline' is enabled and 'Save Camera' button is clicked. Please review and/or adjust it before applying.")
detection_labels = models.TextField(max_length=2000, null=True, blank=True,
verbose_name="Detection Labels",
help_text="Detection labels to use, one per line")
@property
def transformation(self):
return ScenescapeCameraPose.arrayToDictionary(self.transforms, self.transform_type)
def logData(self, sensor_type, jdata):
timestamp = get_epoch_time(jdata['timestamp'])
for obj in jdata['objects']:
# Logging only for person ?!
if sensor_type == 'person':
log = DataLog(timestamp)
log.pk = None
log.save()
slog = CamLog(log=log, sensor=self,
pid=int(obj['id']),
x=obj['bounding_box']['x'], y=obj['bounding_box']['y'],
width=obj['bounding_box']['width'],
height=obj['bounding_box']['height'])
slog.save()
return
def cameraData(self, action):
if self.scene is None:
scene_name = ""
else:
scene_name = self.scene.name
if self._original_sensor_id is None:
self._original_sensor_id = ""
if self._original_name is None:
self._original_name = ""
camera_data = {
'sensor_id': self.sensor_id,
'name': self.name,
'scene': scene_name,
'command': self.command,
'camerachain': self.camerachain,
'threshold': self.threshold,
'aspect': self.aspect,
'cv_subsystem': self.cv_subsystem,
'intrinsics_cx': self.intrinsics_cx,
'intrinsics_cy': self.intrinsics_cy,
'intrinsics_fx': self.intrinsics_fx,
'intrinsics_fy': self.intrinsics_fy,
'distortion_k1': self.distortion_k1,
'distortion_k2': self.distortion_k2,
'distortion_p1': self.distortion_p1,
'distortion_p2': self.distortion_p2,
'distortion_k3': self.distortion_k3,
'previous_sensor_id': self._original_sensor_id,
'previous_name': self._original_name,
'action': action,
'width': self.width, # resolution
'height': self.height, # resolution
'sensor': self.sensor,
'sensorchain': self.sensorchain,
'sensorattrib': self.sensorattrib,
'window': self.window,
'usetimestamps': self.usetimestamps,
'virtual': self.virtual,
'debug': self.debug,
'override_saved_intrinstics': self.override_saved_intrinstics,
'frames': self.frames,
'stats': self.stats,
'waitforstable': self.waitforstable,
'preprocess': self.preprocess,
'realtime': self.realtime,
'faketime': self.faketime,
'modelconfig': self.modelconfig,
'rootcert': self.rootcert,
'cert': self.cert,
'cvcores': self.cvcores,
'ovcores': self.ovcores,
'unwarp': self.unwarp,
'ovmshost': self.ovmshost,
'framerate': self.framerate,
'maxcache': self.maxcache,
'filter': self.filter,
'disable_rotation': self.disable_rotation,
'maxdistance': self.maxdistance,
'use_camera_pipeline': self.use_camera_pipeline,
'camera_pipeline': self.camera_pipeline,
'undistort': self.undistort,
'detection_labels': self.detection_labels,
}
return camera_data
def save(self, *args, **kwargs):
if self.intrinsics_cx is None:
self.intrinsics_cx = self.DEFAULT_INTRINSICS['cx']
if self.intrinsics_cy is None:
self.intrinsics_cy = self.DEFAULT_INTRINSICS['cy']
if self.intrinsics_fx is None:
self.intrinsics_fx = self.DEFAULT_INTRINSICS['fx']
if self.intrinsics_fy is None:
self.intrinsics_fy = self.DEFAULT_INTRINSICS['fy']
if self.cv_subsystem is None:
self.cv_subsystem = 'AUTO'
super().save(*args, **kwargs)
transaction.on_commit(partial(sendUpdateCommand,
camera_data = self.cameraData('save')))
return
def delete(self, *args, **kwargs):
super().delete(*args, **kwargs)
transaction.on_commit(partial(sendUpdateCommand,
camera_data = self.cameraData('delete')))
return
class SingletonSensor(Sensor):
map_x = models.FloatField(default=None, null=True, blank=True)
map_y = models.FloatField(default=None, null=True, blank=True)
area = models.CharField(max_length=16, choices=AREA_CHOICES, default='scene')
radius = models.FloatField(default=None, null=True, blank=True)
singleton_type = models.CharField("Type of Sensor", max_length=20, choices=SINGLETON_CHOICES,
default='environmental')
def notifydbupdate(self):
transaction.on_commit(sendUpdateCommand)
return
def get_sectors(self):
if not hasattr(self, 'singleton_scalar_threshold'):
return [{"color": "green", "color_min": "0"}, {"color": "yellow", "color_min": "2"}, {"color": "red", "color_min": "5"}], 10
return self.singleton_scalar_threshold.sectors, self.singleton_scalar_threshold.range_max
def delete(self, *args, **kwargs):
super().delete(*args, **kwargs)
transaction.on_commit(sendUpdateCommand)
return
class DataLog(models.Model):
timestamp = models.FloatField(db_index=True)
class MobileObject(models.Model):
timestamp = models.FloatField(db_index=True)
scene = models.ForeignKey(Scene, null=True, on_delete=models.SET_NULL)
previous = models.OneToOneField('self', on_delete=models.CASCADE,
null=True, blank=True, default=None)
pid = models.IntegerField(default=None)
x = models.FloatField(default=None, null=True, blank=True)
y = models.FloatField(default=None, null=True, blank=True)
def velocity(self):
if not self.previous:
return None
xd = self.x - self.previous.x
yd = self.y - self.previous.y
td = self.log.timestamp - self.previous.log.timestamp
if td == 0.0:
return (0, 0)
return (xd / td, yd / td)
def expected(self, ts):
v = self.velocity()
if not v:
v = (0, 0)
return (self.x + v[0] * (ts - self.log.timestamp),
self.y + v[1] * (ts - self.log.timestamp))
class Vehicle(MobileObject):
pass
class CamLog(models.Model):
log = models.OneToOneField(DataLog, on_delete=models.CASCADE,
primary_key=True, related_name="camLog")
sensor = models.ForeignKey(Sensor, on_delete=models.CASCADE, db_index=True)
pid = models.IntegerField(default=None, null=True, blank=True)
x = models.FloatField(default=None, null=True, blank=True)
y = models.FloatField(default=None, null=True, blank=True)
width = models.FloatField(default=None, null=True, blank=True)
height = models.FloatField(default=None, null=True, blank=True)
class SceneLog(models.Model):
log = models.OneToOneField(DataLog, on_delete=models.CASCADE, primary_key=True)
scene = models.ForeignKey(Scene, on_delete=models.CASCADE)
class BoundingBox(models.Model):
name = models.CharField(max_length=200)
def boundingBox(self):
tx = None
ty = None
bx = None
by = None
for point in self.points.all():
if not tx or point.x < tx:
tx = point.x
if not ty or point.y < ty:
ty = point.y
if not bx or point.x > bx:
bx = point.x
if not by or point.y > by:
by = point.y
if not tx:
return None
return ((tx, ty), (bx, by))
def notifydbupdate(self):
transaction.on_commit(sendUpdateCommand)
return
def delete(self, *args, **kwargs):
super().delete(*args, **kwargs)
self.notifydbupdate()
return
class BoundingBoxPoints(models.Model):
sequence = models.IntegerField(default=None, null=True, blank=True)
x = models.FloatField(default=None, null=True, blank=True)
y = models.FloatField(default=None, null=True, blank=True)
class Region(BoundingBox):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
scene = models.ForeignKey(Scene, on_delete=models.CASCADE, related_name="regions")
buffer_size = models.FloatField(default=0.0, null=False, blank=False, validators=[MinValueValidator(0)])
# Currently, there is no ROI support for objects under the ground plane.
height = models.FloatField(default=1.0, null=False, blank=False, validators=[MinValueValidator(0.001)])
volumetric = models.BooleanField(choices=BOOLEAN_CHOICES, default=False, null=True)
def get_sectors(self):
if not hasattr(self, 'roi_occupancy_threshold'):
return [{"color": "green", "color_min": "0"}, {"color": "yellow", "color_min": "2"}, {"color": "red", "color_min": "5"}], 10
return self.roi_occupancy_threshold.sectors, self.roi_occupancy_threshold.range_max
class RegionPoint(BoundingBoxPoints):
region = models.ForeignKey(Region, on_delete=models.CASCADE, related_name="points")
class Tripwire(BoundingBox):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
scene = models.ForeignKey(Scene, on_delete=models.CASCADE, related_name="tripwires")
height = models.FloatField(default=1.0, null=False, blank=False)
class TripwirePoint(BoundingBoxPoints):
tripwire = models.ForeignKey(Tripwire, on_delete=models.CASCADE, related_name="points")
class SingletonAreaPoint(BoundingBoxPoints):
singleton = models.ForeignKey(
SingletonSensor, on_delete=models.CASCADE, related_name="points")
class Event(models.Model):
region = models.ForeignKey(Region, on_delete=models.CASCADE, related_name="events")
timestamp = models.FloatField(db_index=True)
# Default functions for Asset3D array fields
def default_geometric_center():
return [0.0, 0.0, 0.0]
def default_center_of_mass():
return [0.0, 0.0, 0.0]
def default_friction_coefficients():
return [0.5, 0.4]
class Asset3D(models.Model):
name = models.CharField("Class Name", max_length=200, unique=True)
x_size = models.FloatField("Object size in x-axis", default=1.0, validators=[MinValueValidator(0.0)])
y_size = models.FloatField("Object size in y-axis", default=1.0, validators=[MinValueValidator(0.0)])
z_size = models.FloatField("Object size in z-axis", default=1.0, validators=[MinValueValidator(0.0)])
x_buffer_size = models.FloatField("Object buffer size in x-axis", default=0.0)
y_buffer_size = models.FloatField("Object buffer size in y-axis", default=0.0)
z_buffer_size = models.FloatField("Object buffer size in z-axis", default=0.0)
mark_color = models.CharField("Mark Color", max_length=20, default="#888888", blank=True)
model_3d = models.FileField(blank=True, null=True,
validators=[FileExtensionValidator(["glb"]), validate_glb])
rotation_x = models.FloatField("X Rotation (degrees)", default=0.0, null=True, blank=True)
rotation_y = models.FloatField("Y Rotation (degrees)", default=0.0, null=True, blank=True)
rotation_z = models.FloatField("Z Rotation (degrees)", default=0.0, null=True, blank=True)
translation_x = models.FloatField("X Translation (meters)", default=0.0,
null=True, blank=True)
translation_y = models.FloatField("Y Translation (meters)", default=0.0,
null=True, blank=True)
translation_z = models.FloatField("Z Translation (meters)", default=0.0,
null=True, blank=True)
scale = models.FloatField("Scale", default=1.0, null=True, blank=True)
rotation_from_velocity = models.BooleanField(choices=BOOLEAN_CHOICES, default=False, null=True)
tracking_radius = models.FloatField("Tracking radius (meters)", default=2.0)
shift_type = models.IntegerField(choices=SHIFT_TYPE, default=1, null=True)
project_to_map = models.BooleanField(choices=BOOLEAN_CHOICES, default=False, null=True)
# Physics properties
geometric_center = ListField(
default=default_geometric_center,
null=True,
blank=True,