-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1863 lines (1700 loc) · 71.6 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Pose Annotator</title>
<meta charset="UTF-8">
<meta name="author" content="Cristian Ortega Singer">
<script src="https://code.jquery.com/jquery-latest.js"></script>
<!--import filsaver.js -->
<script async="" src="https://cdn.rawgit.com/eligrey/FileSaver.js/5ed507ef8aa53d8ecfea96d96bc7214cd2476fd2/FileSaver.min.js"> </script>
<!-- import jszip.js for zipping-->
<script type="text/javascript" src="https://stuk.github.io/jszip/dist/jszip.min.js"></script>
<script>
$(window).load(function () {
$(".loader").fadeOut("slow");
});
</script>
<style>
#img-svg-wrap{
position: relative;
display: inline-block;
}
#image{
min-width: 100%;
height: auto;
}
#svg{
position: absolute;
top: 0;
left: 0;
width: 100%;
min-width: 100%;
height: 100%;
}
line{
fill:none;
stroke-width:3;
stroke: black;
}
.black{
stroke:black;
}
.orange{
stroke:orange;
}
.point{
stroke: black;
}
#createDescr, #poseDelButton{
display:none;
}
#filescroll{
width: 200px;
height: 300px;
overflow: scroll;
}
li{
list-style-type: none;
}
ul{
padding-left: 0;
}
.dropdown-menu {
min-width: 370px !important;
}
#mainContainer{
margin-bottom: 70px;
}
#footerContainer{
position: fixed;
bottom: 0;
}
#loadingDiv{
position: absolute;
top:20%;
left:20%;
z-index: 99;
width: 400px;
height: 300px;
display: flex;
justify-content: center;
}
#spinner{
position: absolute;
top: 30%;
}
#loadingText{
position: absolute;
top:40%;
}
</style>
</head>
<body>
<div id="mainContainer" class="container-fluid">
<div id="loadingDiv" class="m-auto rounded-lg border border-primary bg-light span6 offset3 loader">
<div id="spinner" class="spinner-border mx-auto" role="status">
<span class="sr-only">Loading...</span>
</div>
<span id="loadingText">Setting up Page...</span>
</div>
<h1 class="display-5">Pose Annotator</h1>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark" id="navbar" class="row">
<!--<span class="navbar-brand">Logo</span>-->
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardropI" data-toggle="dropdown">
Import
</a>
<div class="dropdown-menu">
<h5 class="dropdown-header">Metadata:</h5>
<input class="dropdown-item" type="file" id="metadata" accept="application/json" multiple>
<h5 class="dropdown-header">Images:</h5>
<input class="dropdown-item" type="file" id="images" accept="image/*" multiple>
<a class="dropdown-item text-primary" id="importButton" href="javascript: handleFiles();"> Import Files</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardropL" data-toggle="dropdown">
Load
</a>
<div class="dropdown-menu">
<input class="dropdown-item" type="file" id="workingfile" accept="application/json">
<a class="dropdown-item text-primary" id="loadButton" href="javascript: handleWorkingFile();"> Load Project</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript: exportUnfinished();">Save</a>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript: exportFinished();">Export</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="modal" data-target="#settingsModal" onclick="javascript: toggleSettings();" style="cursor: pointer;">Settings</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardropH" data-toggle="dropdown">
?
</a>
<div class="dropdown-menu">
<a class="dropdown-item text-primary" id="loadButton" href="https://docs.google.com/document/d/1t2OCi7HAAHCphB0O1CmdNRlzamoxoq1w4XdWJdLQ6aI/edit?usp=sharing" target="_blank"> Open User Manual <i class='fas fa-external-link-alt' style='font-size:12px'></i></a>
<a class="dropdown-item text-primary" id="loadButton" href="mailto:[email protected]"> Contact Developer <i class='fas fa-external-link-alt' style='font-size:12px'></i></a>
</div>
</li>
</ul>
</nav>
<div id="main" class="row">
<div id="sidebar" class="col-md-3 col-lg-3">
<div class="row mt-2">
<div class="col">
<h5> Checked: <span id = "checked" class="badge badge-secondary">No</span></h5>
</div>
</div>
<div id="tools" class="row">
<div class="col btn-group-vertical">
<button type="button" class="btn btn-dark my-1" id="undoButton" onclick="resetPage();">Undo All Changes</button>
<button type="button" class="btn btn-dark my-1" id="resetButton" onclick="toggleResetMode(!resetMode);">Reset Mode</button>
<button type="button" class="btn btn-dark my-1" id="createButton" onclick="toggleCreationMode(!creationMode);">Creation Mode</button>
<button type="button" class="btn btn-dark my-1" id="deleteButton" onclick="toggleDeletionMode(!deletionMode);">Deletion Mode</button>
<button type="button" class="btn btn-dark my-1" id="selectButton" onclick="toggleSelectionMode(!selectionMode);">Selection Mode</button>
<div class="btn-group">
<button type="button" class="btn btn-dark my-1" id="lastPic" onclick="previousImage();">
<i class='fas fa-arrow-left' style='font-size:26px'></i>
</button>
<button type="button" class="btn btn-dark my-1" id="nextPic" onclick="nextImage();">
<i class='fas fa-arrow-right' style='font-size:26px'></i>
</button>
</div>
</div>
<div class="col">
<div id="optionsContainer" class="row">
<div id="createDescr" class="row">
<div class="row">
<div class="col">
<button type="button" class="btn btn-dark ml-1 my-1" id="createPoseButton" onclick="initWholePoseCreation();">Add new pose</button>
</div>
</div>
<div class="col">
<span> Possible Points to add </br>(klick on joint where a part of the pose is missing):</span> </br>
</div>
<div class="row">
<div class="col ml-1">
<!-- this is the pose model you need for creating new poses and pose parts-->
<svg id="xsvg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="225 140 180 503.5" width="150px" onload="draggable(evt);">
<line x1="304.844" y1="158.085" x2="286.6189880371094" y2="146.37899780273438" class="xl black" id="xhead1" ></line>
<line x1="304.844" y1="158.085" x2="321.30999755859375" y2="146.09800720214844" class="xl" id="xhead2"></line>
<line x1="286.6189880371094" y1="146.37899780273438" x2="272.69500732421875" y2="164.41000366210938" class="xl black" id="xhead3"></line>
<line x1="321.30999755859375" y1="146.09800720214844" x2="337.718994140625" y2="160.0659942626953" class="xl black" id="xhead4"></line>
<line x1="304.844" y1="158.085" x2="309.149" y2="217.125" class="xl black" id="xspine1"></line>
<line x1="309.149" y1="217.125" x2="311.356" y2="377.002" class="xl black" id="xspine2"></line>
<line x1="309.149" y1="217.125" x2="265.398" y2="214.965" class="xl black" id="xarmL1"></line>
<line x1="265.398" y1="214.965" x2="241.82000732421875" y2="296.8479919433594" class="xl black" id="xarmL2"></line>
<line x1="241.82000732421875" y1="296.8479919433594" x2="234.33299255371094" y2="372.14501953125" class="xl black" id="xarmL3"></line>
<line x1="309.149" y1="217.125" x2="357.325" y2="217.193" class="xl black" id="xarmR1"></line>
<line x1="357.325" y1="217.193" x2="379.21" y2="302.628" class="xl black" id="xarmR2"></line>
<line x1="379.21" y1="302.628" x2="382.9330139160156" y2="369.89801025390625" class="xl black" id="xarmR3"></line>
<line x1="311.356" y1="377.002" x2="278.532" y2="377.052" class="xl black" id="xlegL1"></line>
<line x1="278.532" y1="377.052" x2="280.661" y2="501.84" class="xl black" id="xlegL2"></line>
<line x1="280.661" y1="501.84" x2="285.015" y2="620.016" class="xl black" id="xlegL3"></line>
<line x1="311.356" y1="377.002" x2="344.174" y2="374.887" class="xl black" id="xlegR1"></line>
<line x1="344.174" y1="374.887" x2="344.244" y2="497.407" class="xl black" id="xlegR2"></line>
<line x1="344.244" y1="497.407" x2="342.0530090332031" y2="614.6240234375" class="xl black" id="xlegR3"></line>
<line x1="285.015" y1="620.016" x2="267.1669921875" y2="630.7310180664062" class="xl black" id="xfootL1"></line>
<line x1="267.1669921875" y1="630.7310180664062" x2="244.47198486328125" y2="619.89501953125" class="xl black" id="xfootL2"></line>
<line x1="285.015" y1="620.016" x2="303.4849853515625" y2="636.5659790039062" class="xl black" id="xfootL3"></line>
<line x1="342.0530090332031" y1="614.6240234375" x2="368.5419921875" y2="627.5029907226562" class="xl black" id="xfootR1"></line>
<line x1="368.5419921875" y1="627.5029907226562" x2="393.6449890136719" y2="618.614013671875" class="xl black" id="xfootR2"></line>
<line x1="342.0530090332031" y1="614.6240234375" x2="324.5820007324219" y2="628.4340209960938" class="xl black" id="xfootR3"></line>
<circle cx="304.844" cy="158.085" r="5" class="xp black" id="x0"></circle>
<circle cx="309.149" cy="217.125" r="5" class="xp black" id="x1"></circle>
<circle cx="265.398" cy="214.965" r="5" class="xp black" id="x2"></circle>
<circle cx="241.82000732421875" cy="296.8479919433594" r="5" class="xp black" id="x3"></circle>
<circle cx="234.33299255371094" cy="372.14501953125" r="5" class="xp black" id="x4"></circle>
<circle cx="357.325" cy="217.193" r="5" class="xp black" id="x5"></circle>
<circle cx="379.21" cy="302.628" r="5" class="xp black" id="x6"></circle>
<circle cx="382.9330139160156" cy="369.89801025390625" r="5" class="xp black" id="x7"></circle>
<circle cx="311.356" cy="377.002" r="5" class="xp black" id="x8"></circle>
<circle cx="278.532" cy="377.052" r="5" class="xp black" id="x9"></circle>
<circle cx="280.661" cy="501.84" r="5" class="xp black" id="x10"></circle>
<circle cx="285.015" cy="620.016" r="5" class="xp black" id="x11"></circle>
<circle cx="344.174" cy="374.887" r="5" class="xp black" id="x12"></circle>
<circle cx="344.244" cy="497.407" r="5" class="xp black" id="x13"></circle>
<circle cx="342.0530090332031" cy="614.6240234375" r="5" class="xp black" id="x14"></circle>
<circle cx="286.6189880371094" cy="146.37899780273438" r="5" class="xp black" id="x15"></circle>
<circle cx="321.30999755859375" cy="146.09800720214844" r="5" class="xp black" id="x16"></circle>
<circle cx="272.69500732421875" cy="164.41000366210938" r="5" class="xp black" id="x17"></circle>
<circle cx="337.718994140625" cy="160.0659942626953" r="5" class="xp black" id="x18"></circle>
<circle cx="368.5419921875" cy="627.5029907226562" r="5" class="xp black" id="x19"></circle>
<circle cx="393.6449890136719" cy="618.614013671875" r="5" class="xp black" id="x20"></circle>
<circle cx="324.5820007324219" cy="628.4340209960938" r="5" class="xp black" id="x21"></circle>
<circle cx="267.1669921875" cy="630.7310180664062" r="5" class="xp black" id="x22"></circle>
<circle cx="244.47198486328125" cy="619.89501953125" r="5" class="xp black" id="x23"></circle>
<circle cx="303.4849853515625" cy="636.5659790039062" r="5" class="xp black" id="x24"></circle>
</svg>
</div>
</div>
</div>
<div id="poseDelDescr" class="row">
<div class="col">
<button type="button" class="btn btn-dark my-1" id="poseDelButton" onclick="deletePose();" disabled = true>Delete Pose</button>
</div>
</div>
</div>
</div>
</div>
<div id="files" class="row">
<div class="col">
<h5>Images:</h5>
<div id="filescroll" style="overflow-x: hidden;">
<ul id="fileview"></ul>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-lg-6">
<button class="btn-block btn-dark py-1 mt-4" data-toggle="collapse" data-target="#shortcuts">Show/Hide Shortcuts</button>
</div>
</div>
</div>
<div id="workspace" class="col">
<div class="row mt-3">
<div id="img-svg-wrap">
<img x= 0 y= 0 id="image" href="" alt="Import or Load Files to Start..">
<svg id="svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" onload="draggable(evt)">
</svg>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="row">
<div class="col-md-3 col-lg-3"></div>
<div id="shortcuts" class="col container mx-auto mt-2 collapse">
<table class="table table-dark table-striped">
<thead>
<tr>
<th>Mode</th>
<th>Key</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<tr>
<td>Normal</td>
<td>arrow down</td>
<td>black/orange</td>
</tr>
<tr>
<td>Reset</td>
<td>r</td>
<td>purple</td>
</tr>
<tr>
<td>Delete</td>
<td>d</td>
<td>red</td>
</tr>
<tr>
<td>Create</td>
<td>c</td>
<td>green</td>
</tr>
<tr>
<td>Select</td>
<td>s</td>
<td>blue</td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-3 col-lg-3"></div>
</div>
</div>
</div>
<!-- Settings Modal -->
<div class="modal fade" id="settingsModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Settings</h4>
<button type="button" class="close" data-dismiss="modal" onclick="toggleSettings()">×</button>
</div>
<div class="modal-body">
<form action="#">
<h4>Filepaths</h4>
<div class="form-group">
<label for="metapath">Metadata filepath</label>
<input type="text" class="form-control" id="metapath" placeholder="metadata/">
</div>
<div class="form-group">
<label for="imagepath">Images filepath</label>
<input type="text" class="form-control" id="imagepath" placeholder="img/">
</div>
<button type="button" class="btn btn-default text-primary" onclick="changePaths();">Save</button>
<!--<h4>Hotkeys and Colors</h4>
<div class="form-group">
<label for="resetKey">Reset Mode</label>
<input type="text" class="form-control" id="resetKey" placeholder="r" onkeydown="function f(){getKeyCode(event); setKeyCode('resetKey');}">
<input type="color" id="resetColor" name="favcolor" value="#800080">
</div>
<div class="form-group">
<label for="deleteKey">Delete Mode</label>
<input type="text" class="form-control" id="deleteKey" placeholder="d">
<input type="color" id="deleteColor" name="favcolor" value="#ff0000">
</div>
<div class="form-group">
<label for="createKey">Creation Mode</label>
<input type="text" class="form-control" id="createKey" placeholder="c">
<input type="color" id="createColor" name="favcolor" value="#008000">
</div>
<div class="form-group">
<label for="selectKey">SelectionMode</label>
<input type="text" class="form-control" id="selectKey" placeholder="s">
<input type="color" id="SelectColor" name="favcolor" value="#0000ff">
</div>
<div class="form-group">
<label for="colorKey">Color change</label>
<input type="text" class="form-control" id="colorKey" placeholder="arrow down">
</div>-->
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="toggleSettings()">Close</button>
</div>
</div>
</div>
</div>
</div>
<div id="footerContainer" class="container-fluid">
<div class="row">
<div class="col mb-0 p-0">
<footer class="page-footer font-small bg-dark">
<div class="footer-copyright text-center py-3">
<a href="https://github.com/Crisphi/poseannotator" target="_blank"><i class='fab fa-github' style='font-size:24px'></i></a>
<span class="text-secondary">© 2020 Copyright:</span>
<a href="https://www.izdigital.fau.de/" target="_blank"> Cristian Ortega Singer IzDigital FAU Erlangen-Nürnberg</a>
</div>
</footer>
</div>
</div>
</div>
<script type="text/javascript">
"use strict";
//initializing global variables
var img_filepath = "img/";
var meta_filepath = "metadata/";
var single_meta_file = false;
var workingfile = false;
var meta_file_list = {};
var annotations;
var annotationsTemp;
var image_file_list = {};
var current_image_id;
var current_image_source;
var meta_id;
var unsavedChanges = {};
var fileview;
var working_file_list = {};
var working_data = {};
var selectedPoseElements = [];
var color = "black";
var resetMode = false;
var creationMode = false;
var wholePoseCreationMode = false;
var deletionMode = false;
var selectionMode = false;
var creating = false;
var creatingWhole = false;
var settings = false;
var bodyparts = {};
var bodyschema = {
"head1" : [0, 15],
"head2" : [0, 16],
"head3" : [15, 17],
"head4" : [16, 18],
"spine1": [0, 1],
"spine2": [1, 8],
"armL1" : [1, 2],
"armL2" : [2, 3],
"armL3" : [3, 4],
"armR1" : [1, 5],
"armR2" : [5, 6],
"armR3" : [6, 7],
"legL1" : [8, 9],
"legL2" : [9, 10],
"legL3" : [10, 11],
"legR1" : [8, 12],
"legR2" : [12, 13],
"legR3" : [13, 14],
"footL1": [11, 22],
"footL2": [22, 23],
"footL3": [11, 24],
"footR1": [14, 19],
"footR2": [19, 20],
"footR3": [14, 21]
};
var highlighted;
var highlightedModel = [];
var greenHighlighted;
var creationId;
var creationId2;
var creationCoordX;
var creationCoordY;
var creationPoseId;
var selectedElement;
var offset;
var transform;
var bbox;
var minX;
var minY;
var maxX;
var maxY;
var boundaryX1 = 0;
var boundaryY1 = 0;
var boundaryX2 = document.getElementById("svg").width;
var boundaryY2 = document.getElementById("svg").height;
var svgNS = "http://www.w3.org/2000/svg";
// Check for the various File API support.
if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
alert("The File APIs are not fully supported in this browser.");
}
//various event listeners for tracking key and mouse navigation
document.addEventListener("keydown", function(event) {
if((settings == false) &&(annotations)){
if(event.keyCode == 37){
console.log("Left was pressed");
previousImage();
}
else if(event.keyCode == 39) {
console.log("Right was pressed");
nextImage();
}
else if((event.keyCode == 40) &&(!deletionMode) &&(!creationMode)&&(!resetMode)&&(!selectionMode)){
console.log("Down was pressed");
changeAnnoColor();
}
else if(event.keyCode == 68){
console.log("d was pressed");
toggleDeletionMode(!deletionMode);
}else if(event.keyCode == 67){
console.log("c was pressed");
toggleCreationMode(!creationMode);
}else if(event.keyCode == 82){
console.log("r was pressed");
toggleResetMode(!resetMode);
}else if(event.keyCode == 83){
console.log("s was pressed");
toggleSelectionMode(!selectionMode);
}
}
});
//function for making the annotation points draggable
function draggable(evt){
var svg = evt.target;
//set event listeners for mouse movement
svg.addEventListener("mousedown", function(event){
if(creating){
createPosePart(event);
creating = false;
}else if(resetMode){
undoChanges(event);
}else if(creationMode){
selectAddingPoint(event);
addAnnotation(event);
}else if(deletionMode){
deletePoint(event);
}else if(selectionMode){
selectPose(event);
}else{
startDrag(event);
}
});
svg.addEventListener("mousedown", startDrag);
svg.addEventListener("mousemove", drag);
svg.addEventListener("mouseup", endDrag);
svg.addEventListener("mouseleave", endDrag);
//function for adding new points to an uncomplete bodyschema
function addAnnotation(evt){
//get svg and posemodel
let descr = document.getElementById("createDescr");
let svg = document.getElementById("svg");
let children = svg.childNodes;
if(evt.target.classList.contains("point")){
//reset possible highlights
if(highlighted){
highlighted.removeAttribute("style");
deleteDescr();
}
selectedElement = evt.target;
highlighted = selectedElement;
//highlight selected point and save his id & coordinates for point and line creation
selectedElement.setAttributeNS(null, "style", "fill: red;")
let pointId = selectedElement.getAttribute("id");
let ids = pointId.split(" ");
creationId2 = ids[1];
creationPoseId = ids[0];
creationCoordX = selectedElement.getAttributeNS(null, "cx");
creationCoordY = selectedElement.getAttributeNS(null, "cy");
//get points that are connected with selected point via bodyschema
for(let i in bodyschema){
if(bodyschema[i][0] == creationId2){
for(let j = 0; j < children.length; j++){
if(children[j].classList.contains("point")){
let pointId2 = children[j].getAttribute("id");
let ids2 = pointId2.split(" ");
if((ids2[1] == bodyschema[i][1]) && (ids2[0] == ids[0])){
let xTemp = parseInt(children[j].getAttribute("cx"));
let yTemp = parseInt(children[j].getAttribute("cy"));
//if the point doesn't exist, save it in array and highlight equivalent in pose model
if((xTemp == "0") && (yTemp == "0")){
let tempidp = "x" + ids2[1].toString();
let tempxp = document.getElementById(tempidp);
tempxp.setAttributeNS(null, "style", "fill:red;");
let tempid = "x" + i.toString();
let tempx = document.getElementById(tempid);
tempx.setAttributeNS(null, "style", "stroke: red;");
highlightedModel.push(tempxp, tempx);
}
}
}
}
}
}
}
}
//funcion that handles interaction with the pose model in creation mode
function selectAddingPoint(evt){
//check if event occured in pose model svg
if((evt.target.classList.contains("xp")) && (highlightedModel.includes(evt.target))){
//handles the reset of highlighting
if((greenHighlighted) && highlightedModel.includes(greenHighlighted)){
greenHighlighted.setAttributeNS(null, "style", "fill: red;");
}else if(greenHighlighted){
greenHighlighted.setAttributeNS(null, "style", "fill: black;");
greenHighlighted = null;
}
selectedElement = evt.target;
//highlights selected Point in pose model
selectedElement.setAttributeNS(null, "style", "fill: green;");
greenHighlighted = selectedElement;
//get and save id of selected point to create equivalent in the real image
let idRaw = selectedElement.getAttributeNS(null, "id");
let id = idRaw.split("x")[1];
console.log(id);
creationId = id;
creating = true;
}
}
//handles actual creation of new pose parts in the current image
function createPosePart(evt){
if(evt.target.getAttributeNS(null, "id") == "svg"){
console.log(evt.target);
//get coordinates for point to create
let coord = getMousePos(evt);
let x2 = coord.x;
let y2 = coord.y;
let idp = creationPoseId + " " + creationId;
console.log(idp);
//overwrite coordinates of missing point to make it visible
console.log(x2, y2);
let cPoint = document.getElementById(idp);
cPoint.setAttributeNS(null, "cx", x2);
cPoint.setAttributeNS(null, "cy", y2);
cPoint.setAttributeNS(null, "style", "visibility: visible;");
//create line
let idl;
let tempEl;
for(let i in bodyschema){
if((bodyschema[i][0] == creationId)&&(bodyschema[i][1] == creationId2)){
idl = creationPoseId + " " + i;
console.log(idl);
createLine(x2, y2, creationCoordX, creationCoordY, idl);
}else if((bodyschema[i][1] == creationId)&&(bodyschema[i][0] == creationId2)){
idl = creationPoseId + " " + i;
console.log(idl);
createLine(creationCoordX, creationCoordY, x2, y2, idl);
}
tempEl = i;
}
if(creatingWhole){
for(let i = 0; i < highlightedModel.length; i++){
highlightedModel[i].setAttributeNS(null, "style", "fill: black;");
}
highlightedModel = [];
creatingWhole = false;
}
}
}
//set coordinates of selected point back to default
function undoChanges(evt){
if(evt.target.classList.contains("point")){
selectedElement = evt.target;
if(selectedElement.hasAttributeNS(null,"transform")){
selectedElement.removeAttributeNS(null, "transform");
let lineIds = getLineIds();
let x = selectedElement.getAttributeNS(null, "cx");
let y = selectedElement.getAttributeNS(null, "cy");
for(let k in lineIds){
//console.log(k);
let line = document.getElementById(k.toString());
if(line){
if(lineIds[k] == 0){
line.setAttributeNS(null,"x1", x);
line.setAttributeNS(null,"y1", y);
//console.log("changed x1 and y1");
}else if(lineIds[k] == 1){
line.setAttributeNS(null,"x2", x);
line.setAttributeNS(null,"y2", y);
//console.log("changed x2 and y2");
}else{
console.log("An error occured. Look in source code function undoChanges() to see what happened.");
}
}
}
}
}
}
//Delete parts of the pose
function deletePoint(evt){
console.log("Point Deletion");
if(evt.target.classList.contains("point")){
selectedElement = evt.target;
//removes transformations if they exist
if(selectedElement.hasAttributeNS(null,"transform")){
selectedElement.removeAttributeNS(null, "transform");
//sets coordinates of point to 0
selectedElement.setAttributeNS(null, "cx", 0);
selectedElement.setAttributeNS(null, "cy", 0);
selectedElement.setAttributeNS(null, "style", "visibility: hidden;");
//get all lines that connect to point
let lineIds = getLineIds();
//delete all connected lines
for(let k in lineIds){
//console.log(k);
let line = document.getElementById(k.toString());
if(line){
line.remove();
}
}
}
}
}
//select whole pose and not just one point
function selectPose(evt){
console.log("Select Pose Start");
selectedPoseElements = [];
if(evt.target.classList.contains("point")){
selectedElement = evt.target;
//get pose id of clicked point
let pointId = selectedElement.getAttribute("id");
let ids = pointId.split(" ");
let pointschemaId = ids[1];
let poseId = ids[0];
let svg = document.getElementById("svg");
//get all svg elements with the same pose id and save them in selectedPoseElements array
for(let i = 0; i < svg.children.length; i++){
let tempElementId = svg.children[i].getAttribute("id");
let idsTemp = tempElementId.split(" ");
if(poseId == idsTemp[0]){
selectedPoseElements.push(svg.children[i]);
svg.children[i].setAttributeNS(null, "style", "stroke: blue;");
}else{
svg.children[i].removeAttributeNS(null, "style");
}
}
//enable delete button if user selected a pose (clicked on a point on svg)
if(selectedPoseElements){
console.log(selectedPoseElements);
let btn = document.getElementById("poseDelButton");
btn.disabled = false;
}else{
console.log("An Error occured. Please look at function selectPose(evt) in code.");
}
}
}
//track mouseposition on screen if mouse interacts with svg
function getMousePos(evt){
let ctm = svg.getScreenCTM();
if(evt.touches){
evt = evt.touches[0];
}
return{
x: (evt.clientX - ctm.e) / ctm.a,
y: (evt.clientY - ctm.f) / ctm.d
};
}
// handles mousedown event, starts dragging process
function startDrag(evt){
let svgtemp = document.getElementById("svg");
//set boundaries to Imagesize so that it is impossible to drag points out of view
boundaryX2 = svgtemp.width.baseVal.value;
boundaryY2 = svgtemp.height.baseVal.value;
//console.log("Boundaries:");
//console.log(boundaryX2);
//console.log(boundaryY2);
//correct mouse offset in relation to svg through a transformation
if(evt.target.classList.contains("point")){
selectedElement = evt.target;
offset = getMousePos(evt);
//console.log(offset);
let transforms = selectedElement.transform.baseVal;
if(transforms.length === 0 || transforms.getItem(0).type !== SVGTransform.SVG_TRANSFORM_TRANSLATE){
let translate = svg.createSVGTransform();
translate.setTranslate(0,0);
selectedElement.transform.baseVal.insertItemBefore(translate,0);
}
transform = transforms.getItem(0);
offset.x -= transform.matrix.e;
offset.y -= transform.matrix.f;
//console.log(offset.x);
//console.log(offset.y);
//set boundaries according to the collision box of svg element
bbox = selectedElement.getBBox();
minX = boundaryX1 - bbox.x;
maxX = boundaryX2 -bbox.x - bbox.width;
minY = boundaryY1 - bbox.y;
maxY = boundaryY2 - bbox.y - bbox.height;
}
}
//handles mousemove event; the dragging process itself
function drag(evt){
if(selectedElement){
evt.preventDefault();
let coord = getMousePos(evt);
let dx = coord.x - offset.x;
let dy = coord.y - offset.y;
//make shure svg element stays within boundaries
if(dx < minX){
dx = minX;
}else if(dx >maxX){
dx = maxX;
}
if(dy < minY){
dy = minY;
}else if(dy > maxY){
dy = maxY;
}
//set new position for svg point
transform.setTranslate(dx, dy);
let lineIds = getLineIds();
for(let k in lineIds){
//console.log(k);
let line = document.getElementById(k.toString());
if(line){
if(lineIds[k] == 0){
line.setAttributeNS(null,"x1", coord.x);
line.setAttributeNS(null,"y1", coord.y);
//console.log("changed x1 and y1");
}else if(lineIds[k] == 1){
line.setAttributeNS(null,"x2", coord.x);
line.setAttributeNS(null,"y2", coord.y);
//console.log("changed x2 and y2");
}else{
console.log("An error occured. Look in source code function pointchange() to see what happened.");
}
}
}
}
}
//set new position for svg lines if selected point is one of the coordinates
function getLineIds(){
//get point id and bodyschema ids for identifing correct lines
let pointId = selectedElement.getAttribute("id");
let ids = pointId.split(" ");
let pointschemaId = ids[1];
let poseId = ids[0];
//console.log("Pointschema Id: " + pointschemaId);
let lineIds = {};
//get all bodyparts that have the right identifier
for(let i in bodyschema){
for(let j = 0; j < bodyschema[i].length; j++){
if(bodyschema[i][j] == pointschemaId){
lineIds[poseId + " " + i] = j;
}
}
}
return lineIds;
}
//handles mouseup and mouseleave events; stops dragging process
function endDrag (evt){
selectedElement = false;
}
}
//prepare metadata for new Pose in image
function initWholePoseCreation(){
cacheAnnotations();
let facePoints = [];
for(let i= 0; i < 210; i++){
facePoints.push(0);
}
let handLeftPoints = [];
for(let i= 0; i < 63; i++){
handLeftPoints.push(0);
}
let handRightPoints = [];
for(let i= 0; i < 63; i++){
handRightPoints.push(0);
}
let posePoints = [];
for(let i= 0; i < 75; i++){
posePoints.push(0);
}
let dataInit = {
face_keypoints_2d: facePoints,
face_keypoints_3d: [],
hand_left_keypoints_2d: handLeftPoints,
hand_left_keypoints_3d: [],
hand_right_keypoints_2d: handRightPoints,
hand_right_keypoints_3d: [],
pose_keypoints_2d: posePoints,
pose_keypoints_3d: []
}
unsavedChanges[current_image_id]["people"].push(dataInit);
console.log(unsavedChanges[current_image_id]["people"]);
creationPoseId = unsavedChanges[current_image_id]["people"].length - 1;
let xsvg = document.getElementById("xsvg");
for(let i = 0; i < xsvg.childNodes.length; i++){
if((xsvg.childNodes[i].classList)&&(xsvg.childNodes[i].classList.contains("xp"))){
xsvg.childNodes[i].setAttributeNS(null, "style", "fill: red;");
highlightedModel.push(xsvg.childNodes[i]);
}
}
creatingWhole = true;
loadAnnotations();
}
//Not finished will be included in future versions: load json file if the annotations are saved in only one json file
function loadMetaFileSingle(){
}
//load current json file if the annotations are saved in multiple json files
function loadMetaFiles(){
single_meta_file = false;
//console.log(meta_file_list);
//get name of json file through the name of the image file
let id = current_image_source.split(".")[0];
//create a new regex pattern
let patt = new RegExp(id + "_");
//console.log(id);
//search in loaded meta files for the correct file via file name and save file in meta_id
for(let i = 0; i < meta_file_list.length; i++){
let d = meta_file_list[i];
if(patt.test(d.name)){
//console.log(d);
meta_id = d;
}
}
//if file found launch a file reader to get content of file as a string array
if(meta_id){
let reader = new FileReader();
reader.onload = getAnnotations;
//console.log(meta_id);
reader.readAsText(meta_id);
}
function getAnnotations(t, callback = function(){loadAnnotations();}){
//console.log(callback);
let lines = t.target.result;
let a = JSON.parse(lines);
annotations = $.extend(annotations,a);
//console.log("Annotations:");
//console.log(annotations);
callback();
}
}
//initiate the file reading process of json and image files
function handleFiles(){
workingfile = false;
//get json and image files from the file selector
image_file_list = document.getElementById("images").files;
meta_file_list = document.getElementById("metadata").files;
displayFileList();
//catching errors
if(!image_file_list[0]){
alert("You have to select image files before you can continue!");
}else if(!meta_file_list[0]){
alert("You have to select JSON files before you can continue!");
}else if(image_file_list[0] && meta_file_list[0]){
console.log(image_file_list);
console.log(meta_file_list);
if(annotations){
if(confirm("Annotations already included! Unsaved changes will be lost! Do you want to replace annotation File(s)?")){
current_image_id = 0;
loadImage();
annotations = {};