Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

XSegEditor.py 63 KB

You have to be logged in to leave a comment. Sign In
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
  1. import json
  2. import multiprocessing
  3. import os
  4. import pickle
  5. import sys
  6. import tempfile
  7. import time
  8. import traceback
  9. from enum import IntEnum
  10. from types import SimpleNamespace as sn
  11. import cv2
  12. import numpy as np
  13. import numpy.linalg as npla
  14. from PyQt5.QtCore import *
  15. from PyQt5.QtGui import *
  16. from PyQt5.QtWidgets import *
  17. from core import imagelib, pathex
  18. from core.cv2ex import *
  19. from core.imagelib import SegIEPoly, SegIEPolys, SegIEPolyType, sd
  20. from core.qtex import *
  21. from DFLIMG import *
  22. from localization import StringsDB, system_language
  23. from samplelib import PackedFaceset
  24. from .QCursorDB import QCursorDB
  25. from .QIconDB import QIconDB
  26. from .QStringDB import QStringDB
  27. from .QImageDB import QImageDB
  28. class OpMode(IntEnum):
  29. NONE = 0
  30. DRAW_PTS = 1
  31. EDIT_PTS = 2
  32. VIEW_BAKED = 3
  33. VIEW_XSEG_MASK = 4
  34. class PTEditMode(IntEnum):
  35. MOVE = 0
  36. ADD_DEL = 1
  37. class DragType(IntEnum):
  38. NONE = 0
  39. IMAGE_LOOK = 1
  40. POLY_PT = 2
  41. class ViewLock(IntEnum):
  42. NONE = 0
  43. CENTER = 1
  44. class QUIConfig():
  45. @staticmethod
  46. def initialize(icon_size = 48, icon_spacer_size=16, preview_bar_icon_size=64):
  47. QUIConfig.icon_q_size = QSize(icon_size, icon_size)
  48. QUIConfig.icon_spacer_q_size = QSize(icon_spacer_size, icon_spacer_size)
  49. QUIConfig.preview_bar_icon_q_size = QSize(preview_bar_icon_size, preview_bar_icon_size)
  50. class ImagePreviewSequenceBar(QFrame):
  51. def __init__(self, preview_images_count, icon_size):
  52. super().__init__()
  53. self.preview_images_count = preview_images_count = max(1, preview_images_count + (preview_images_count % 2 -1) )
  54. self.icon_size = icon_size
  55. black_q_img = QImage(np.zeros( (icon_size,icon_size,3) ).data, icon_size, icon_size, 3*icon_size, QImage.Format_RGB888)
  56. self.black_q_pixmap = QPixmap.fromImage(black_q_img)
  57. self.image_containers = [ QLabel() for i in range(preview_images_count)]
  58. main_frame_l_cont_hl = QGridLayout()
  59. main_frame_l_cont_hl.setContentsMargins(0,0,0,0)
  60. #main_frame_l_cont_hl.setSpacing(0)
  61. for i in range(len(self.image_containers)):
  62. q_label = self.image_containers[i]
  63. q_label.setScaledContents(True)
  64. if i == preview_images_count//2:
  65. q_label.setMinimumSize(icon_size+16, icon_size+16 )
  66. q_label.setMaximumSize(icon_size+16, icon_size+16 )
  67. else:
  68. q_label.setMinimumSize(icon_size, icon_size )
  69. q_label.setMaximumSize(icon_size, icon_size )
  70. opacity_effect = QGraphicsOpacityEffect()
  71. opacity_effect.setOpacity(0.5)
  72. q_label.setGraphicsEffect(opacity_effect)
  73. q_label.setSizePolicy (QSizePolicy.Fixed, QSizePolicy.Fixed)
  74. main_frame_l_cont_hl.addWidget (q_label, 0, i)
  75. self.setLayout(main_frame_l_cont_hl)
  76. self.prev_img_conts = self.image_containers[(preview_images_count//2) -1::-1]
  77. self.next_img_conts = self.image_containers[preview_images_count//2:]
  78. self.update_images()
  79. def get_preview_images_count(self):
  80. return self.preview_images_count
  81. def update_images(self, prev_imgs=None, next_imgs=None):
  82. # Fix arrays
  83. if prev_imgs is None:
  84. prev_imgs = []
  85. prev_img_conts_len = len(self.prev_img_conts)
  86. prev_q_imgs_len = len(prev_imgs)
  87. if prev_q_imgs_len < prev_img_conts_len:
  88. for i in range ( prev_img_conts_len - prev_q_imgs_len ):
  89. prev_imgs.append(None)
  90. elif prev_q_imgs_len > prev_img_conts_len:
  91. prev_imgs = prev_imgs[:prev_img_conts_len]
  92. if next_imgs is None:
  93. next_imgs = []
  94. next_img_conts_len = len(self.next_img_conts)
  95. next_q_imgs_len = len(next_imgs)
  96. if next_q_imgs_len < next_img_conts_len:
  97. for i in range ( next_img_conts_len - next_q_imgs_len ):
  98. next_imgs.append(None)
  99. elif next_q_imgs_len > next_img_conts_len:
  100. next_imgs = next_imgs[:next_img_conts_len]
  101. for i,img in enumerate(prev_imgs):
  102. self.prev_img_conts[i].setPixmap( QPixmap.fromImage( QImage_from_np(img) ) if img is not None else self.black_q_pixmap )
  103. for i,img in enumerate(next_imgs):
  104. self.next_img_conts[i].setPixmap( QPixmap.fromImage( QImage_from_np(img) ) if img is not None else self.black_q_pixmap )
  105. class ColorScheme():
  106. def __init__(self, unselected_color, selected_color, outline_color, outline_width, pt_outline_color, cross_cursor):
  107. self.poly_unselected_brush = QBrush(unselected_color)
  108. self.poly_selected_brush = QBrush(selected_color)
  109. self.poly_outline_solid_pen = QPen(outline_color, outline_width, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)
  110. self.poly_outline_dot_pen = QPen(outline_color, outline_width, Qt.DotLine, Qt.RoundCap, Qt.RoundJoin)
  111. self.pt_outline_pen = QPen(pt_outline_color)
  112. self.cross_cursor = cross_cursor
  113. class CanvasConfig():
  114. def __init__(self,
  115. pt_radius=4,
  116. pt_select_radius=8,
  117. color_schemes=None,
  118. **kwargs):
  119. self.pt_radius = pt_radius
  120. self.pt_select_radius = pt_select_radius
  121. if color_schemes is None:
  122. color_schemes = [
  123. ColorScheme( QColor(192,0,0,alpha=0), QColor(192,0,0,alpha=72), QColor(192,0,0), 2, QColor(255,255,255), QCursorDB.cross_red ),
  124. ColorScheme( QColor(0,192,0,alpha=0), QColor(0,192,0,alpha=72), QColor(0,192,0), 2, QColor(255,255,255), QCursorDB.cross_green ),
  125. ColorScheme( QColor(0,0,192,alpha=0), QColor(0,0,192,alpha=72), QColor(0,0,192), 2, QColor(255,255,255), QCursorDB.cross_blue ),
  126. ]
  127. self.color_schemes = color_schemes
  128. class QCanvasControlsLeftBar(QFrame):
  129. def __init__(self):
  130. super().__init__()
  131. #==============================================
  132. btn_poly_type_include = QToolButton()
  133. self.btn_poly_type_include_act = QActionEx( QIconDB.poly_type_include, QStringDB.btn_poly_type_include_tip, shortcut='Q', shortcut_in_tooltip=True, is_checkable=True)
  134. btn_poly_type_include.setDefaultAction(self.btn_poly_type_include_act)
  135. btn_poly_type_include.setIconSize(QUIConfig.icon_q_size)
  136. btn_poly_type_exclude = QToolButton()
  137. self.btn_poly_type_exclude_act = QActionEx( QIconDB.poly_type_exclude, QStringDB.btn_poly_type_exclude_tip, shortcut='W', shortcut_in_tooltip=True, is_checkable=True)
  138. btn_poly_type_exclude.setDefaultAction(self.btn_poly_type_exclude_act)
  139. btn_poly_type_exclude.setIconSize(QUIConfig.icon_q_size)
  140. self.btn_poly_type_act_grp = QActionGroup (self)
  141. self.btn_poly_type_act_grp.addAction(self.btn_poly_type_include_act)
  142. self.btn_poly_type_act_grp.addAction(self.btn_poly_type_exclude_act)
  143. self.btn_poly_type_act_grp.setExclusive(True)
  144. #==============================================
  145. btn_undo_pt = QToolButton()
  146. self.btn_undo_pt_act = QActionEx( QIconDB.undo_pt, QStringDB.btn_undo_pt_tip, shortcut='Ctrl+Z', shortcut_in_tooltip=True, is_auto_repeat=True)
  147. btn_undo_pt.setDefaultAction(self.btn_undo_pt_act)
  148. btn_undo_pt.setIconSize(QUIConfig.icon_q_size)
  149. btn_redo_pt = QToolButton()
  150. self.btn_redo_pt_act = QActionEx( QIconDB.redo_pt, QStringDB.btn_redo_pt_tip, shortcut='Ctrl+Shift+Z', shortcut_in_tooltip=True, is_auto_repeat=True)
  151. btn_redo_pt.setDefaultAction(self.btn_redo_pt_act)
  152. btn_redo_pt.setIconSize(QUIConfig.icon_q_size)
  153. btn_delete_poly = QToolButton()
  154. self.btn_delete_poly_act = QActionEx( QIconDB.delete_poly, QStringDB.btn_delete_poly_tip, shortcut='Delete', shortcut_in_tooltip=True)
  155. btn_delete_poly.setDefaultAction(self.btn_delete_poly_act)
  156. btn_delete_poly.setIconSize(QUIConfig.icon_q_size)
  157. #==============================================
  158. btn_pt_edit_mode = QToolButton()
  159. self.btn_pt_edit_mode_act = QActionEx( QIconDB.pt_edit_mode, QStringDB.btn_pt_edit_mode_tip, shortcut_in_tooltip=True, is_checkable=True)
  160. btn_pt_edit_mode.setDefaultAction(self.btn_pt_edit_mode_act)
  161. btn_pt_edit_mode.setIconSize(QUIConfig.icon_q_size)
  162. #==============================================
  163. controls_bar_frame2_l = QVBoxLayout()
  164. controls_bar_frame2_l.addWidget ( btn_poly_type_include )
  165. controls_bar_frame2_l.addWidget ( btn_poly_type_exclude )
  166. controls_bar_frame2 = QFrame()
  167. controls_bar_frame2.setFrameShape(QFrame.StyledPanel)
  168. controls_bar_frame2.setSizePolicy (QSizePolicy.Fixed, QSizePolicy.Fixed)
  169. controls_bar_frame2.setLayout(controls_bar_frame2_l)
  170. controls_bar_frame3_l = QVBoxLayout()
  171. controls_bar_frame3_l.addWidget ( btn_undo_pt )
  172. controls_bar_frame3_l.addWidget ( btn_redo_pt )
  173. controls_bar_frame3_l.addWidget ( btn_delete_poly )
  174. controls_bar_frame3 = QFrame()
  175. controls_bar_frame3.setFrameShape(QFrame.StyledPanel)
  176. controls_bar_frame3.setSizePolicy (QSizePolicy.Fixed, QSizePolicy.Fixed)
  177. controls_bar_frame3.setLayout(controls_bar_frame3_l)
  178. controls_bar_frame4_l = QVBoxLayout()
  179. controls_bar_frame4_l.addWidget ( btn_pt_edit_mode )
  180. controls_bar_frame4 = QFrame()
  181. controls_bar_frame4.setFrameShape(QFrame.StyledPanel)
  182. controls_bar_frame4.setSizePolicy (QSizePolicy.Fixed, QSizePolicy.Fixed)
  183. controls_bar_frame4.setLayout(controls_bar_frame4_l)
  184. controls_bar_l = QVBoxLayout()
  185. controls_bar_l.setContentsMargins(0,0,0,0)
  186. controls_bar_l.addWidget(controls_bar_frame2)
  187. controls_bar_l.addWidget(controls_bar_frame3)
  188. controls_bar_l.addWidget(controls_bar_frame4)
  189. self.setSizePolicy ( QSizePolicy.Fixed, QSizePolicy.Expanding )
  190. self.setLayout(controls_bar_l)
  191. class QCanvasControlsRightBar(QFrame):
  192. def __init__(self):
  193. super().__init__()
  194. #==============================================
  195. btn_poly_color_red = QToolButton()
  196. self.btn_poly_color_red_act = QActionEx( QIconDB.poly_color_red, QStringDB.btn_poly_color_red_tip, shortcut='1', shortcut_in_tooltip=True, is_checkable=True)
  197. btn_poly_color_red.setDefaultAction(self.btn_poly_color_red_act)
  198. btn_poly_color_red.setIconSize(QUIConfig.icon_q_size)
  199. btn_poly_color_green = QToolButton()
  200. self.btn_poly_color_green_act = QActionEx( QIconDB.poly_color_green, QStringDB.btn_poly_color_green_tip, shortcut='2', shortcut_in_tooltip=True, is_checkable=True)
  201. btn_poly_color_green.setDefaultAction(self.btn_poly_color_green_act)
  202. btn_poly_color_green.setIconSize(QUIConfig.icon_q_size)
  203. btn_poly_color_blue = QToolButton()
  204. self.btn_poly_color_blue_act = QActionEx( QIconDB.poly_color_blue, QStringDB.btn_poly_color_blue_tip, shortcut='3', shortcut_in_tooltip=True, is_checkable=True)
  205. btn_poly_color_blue.setDefaultAction(self.btn_poly_color_blue_act)
  206. btn_poly_color_blue.setIconSize(QUIConfig.icon_q_size)
  207. btn_view_baked_mask = QToolButton()
  208. self.btn_view_baked_mask_act = QActionEx( QIconDB.view_baked, QStringDB.btn_view_baked_mask_tip, shortcut='4', shortcut_in_tooltip=True, is_checkable=True)
  209. btn_view_baked_mask.setDefaultAction(self.btn_view_baked_mask_act)
  210. btn_view_baked_mask.setIconSize(QUIConfig.icon_q_size)
  211. btn_view_xseg_mask = QToolButton()
  212. self.btn_view_xseg_mask_act = QActionEx( QIconDB.view_xseg, QStringDB.btn_view_xseg_mask_tip, shortcut='5', shortcut_in_tooltip=True, is_checkable=True)
  213. btn_view_xseg_mask.setDefaultAction(self.btn_view_xseg_mask_act)
  214. btn_view_xseg_mask.setIconSize(QUIConfig.icon_q_size)
  215. btn_view_xseg_overlay_mask = QToolButton()
  216. self.btn_view_xseg_overlay_mask_act = QActionEx( QIconDB.view_xseg_overlay, QStringDB.btn_view_xseg_overlay_mask_tip, shortcut='`', shortcut_in_tooltip=True, is_checkable=True)
  217. btn_view_xseg_overlay_mask.setDefaultAction(self.btn_view_xseg_overlay_mask_act)
  218. btn_view_xseg_overlay_mask.setIconSize(QUIConfig.icon_q_size)
  219. self.btn_poly_color_act_grp = QActionGroup (self)
  220. self.btn_poly_color_act_grp.addAction(self.btn_poly_color_red_act)
  221. self.btn_poly_color_act_grp.addAction(self.btn_poly_color_green_act)
  222. self.btn_poly_color_act_grp.addAction(self.btn_poly_color_blue_act)
  223. self.btn_poly_color_act_grp.addAction(self.btn_view_baked_mask_act)
  224. self.btn_poly_color_act_grp.addAction(self.btn_view_xseg_mask_act)
  225. self.btn_poly_color_act_grp.setExclusive(True)
  226. #==============================================
  227. btn_view_lock_center = QToolButton()
  228. self.btn_view_lock_center_act = QActionEx( QIconDB.view_lock_center, QStringDB.btn_view_lock_center_tip, shortcut_in_tooltip=True, is_checkable=True)
  229. btn_view_lock_center.setDefaultAction(self.btn_view_lock_center_act)
  230. btn_view_lock_center.setIconSize(QUIConfig.icon_q_size)
  231. controls_bar_frame2_l = QVBoxLayout()
  232. controls_bar_frame2_l.addWidget ( btn_view_xseg_overlay_mask )
  233. controls_bar_frame2 = QFrame()
  234. controls_bar_frame2.setFrameShape(QFrame.StyledPanel)
  235. controls_bar_frame2.setSizePolicy (QSizePolicy.Fixed, QSizePolicy.Fixed)
  236. controls_bar_frame2.setLayout(controls_bar_frame2_l)
  237. controls_bar_frame1_l = QVBoxLayout()
  238. controls_bar_frame1_l.addWidget ( btn_poly_color_red )
  239. controls_bar_frame1_l.addWidget ( btn_poly_color_green )
  240. controls_bar_frame1_l.addWidget ( btn_poly_color_blue )
  241. controls_bar_frame1_l.addWidget ( btn_view_baked_mask )
  242. controls_bar_frame1_l.addWidget ( btn_view_xseg_mask )
  243. controls_bar_frame1 = QFrame()
  244. controls_bar_frame1.setFrameShape(QFrame.StyledPanel)
  245. controls_bar_frame1.setSizePolicy (QSizePolicy.Fixed, QSizePolicy.Fixed)
  246. controls_bar_frame1.setLayout(controls_bar_frame1_l)
  247. controls_bar_frame3_l = QVBoxLayout()
  248. controls_bar_frame3_l.addWidget ( btn_view_lock_center )
  249. controls_bar_frame3 = QFrame()
  250. controls_bar_frame3.setFrameShape(QFrame.StyledPanel)
  251. controls_bar_frame3.setSizePolicy (QSizePolicy.Fixed, QSizePolicy.Fixed)
  252. controls_bar_frame3.setLayout(controls_bar_frame3_l)
  253. controls_bar_l = QVBoxLayout()
  254. controls_bar_l.setContentsMargins(0,0,0,0)
  255. controls_bar_l.addWidget(controls_bar_frame2)
  256. controls_bar_l.addWidget(controls_bar_frame1)
  257. controls_bar_l.addWidget(controls_bar_frame3)
  258. self.setSizePolicy ( QSizePolicy.Fixed, QSizePolicy.Expanding )
  259. self.setLayout(controls_bar_l)
  260. class QCanvasOperator(QWidget):
  261. def __init__(self, cbar):
  262. super().__init__()
  263. self.cbar = cbar
  264. self.set_cbar_disabled()
  265. self.cbar.btn_poly_color_red_act.triggered.connect ( lambda : self.set_color_scheme_id(0) )
  266. self.cbar.btn_poly_color_green_act.triggered.connect ( lambda : self.set_color_scheme_id(1) )
  267. self.cbar.btn_poly_color_blue_act.triggered.connect ( lambda : self.set_color_scheme_id(2) )
  268. self.cbar.btn_view_baked_mask_act.triggered.connect ( lambda : self.set_op_mode(OpMode.VIEW_BAKED) )
  269. self.cbar.btn_view_xseg_mask_act.triggered.connect ( lambda : self.set_op_mode(OpMode.VIEW_XSEG_MASK) )
  270. self.cbar.btn_view_xseg_overlay_mask_act.toggled.connect ( lambda is_checked: self.update() )
  271. self.cbar.btn_poly_type_include_act.triggered.connect ( lambda : self.set_poly_include_type(SegIEPolyType.INCLUDE) )
  272. self.cbar.btn_poly_type_exclude_act.triggered.connect ( lambda : self.set_poly_include_type(SegIEPolyType.EXCLUDE) )
  273. self.cbar.btn_undo_pt_act.triggered.connect ( lambda : self.action_undo_pt() )
  274. self.cbar.btn_redo_pt_act.triggered.connect ( lambda : self.action_redo_pt() )
  275. self.cbar.btn_delete_poly_act.triggered.connect ( lambda : self.action_delete_poly() )
  276. self.cbar.btn_pt_edit_mode_act.toggled.connect ( lambda is_checked: self.set_pt_edit_mode( PTEditMode.ADD_DEL if is_checked else PTEditMode.MOVE ) )
  277. self.cbar.btn_view_lock_center_act.toggled.connect ( lambda is_checked: self.set_view_lock( ViewLock.CENTER if is_checked else ViewLock.NONE ) )
  278. self.mouse_in_widget = False
  279. QXMainWindow.inst.add_keyPressEvent_listener ( self.on_keyPressEvent )
  280. QXMainWindow.inst.add_keyReleaseEvent_listener ( self.on_keyReleaseEvent )
  281. self.qp = QPainter()
  282. self.initialized = False
  283. self.last_state = None
  284. def initialize(self, img, img_look_pt=None, view_scale=None, ie_polys=None, xseg_mask=None, canvas_config=None ):
  285. q_img = self.q_img = QImage_from_np(img)
  286. self.img_pixmap = QPixmap.fromImage(q_img)
  287. self.xseg_mask_pixmap = None
  288. self.xseg_overlay_mask_pixmap = None
  289. if xseg_mask is not None:
  290. h,w,c = img.shape
  291. xseg_mask = cv2.resize(xseg_mask, (w,h), interpolation=cv2.INTER_CUBIC)
  292. xseg_mask = imagelib.normalize_channels(xseg_mask, 1)
  293. xseg_img = img.astype(np.float32)/255.0
  294. xseg_overlay_mask = xseg_img*(1-xseg_mask)*0.5 + xseg_img*xseg_mask
  295. xseg_overlay_mask = np.clip(xseg_overlay_mask*255, 0, 255).astype(np.uint8)
  296. xseg_mask = np.clip(xseg_mask*255, 0, 255).astype(np.uint8)
  297. self.xseg_mask_pixmap = QPixmap.fromImage(QImage_from_np(xseg_mask))
  298. self.xseg_overlay_mask_pixmap = QPixmap.fromImage(QImage_from_np(xseg_overlay_mask))
  299. self.img_size = QSize_to_np (self.img_pixmap.size())
  300. self.img_look_pt = img_look_pt
  301. self.view_scale = view_scale
  302. if ie_polys is None:
  303. ie_polys = SegIEPolys()
  304. self.ie_polys = ie_polys
  305. if canvas_config is None:
  306. canvas_config = CanvasConfig()
  307. self.canvas_config = canvas_config
  308. # UI init
  309. self.set_cbar_disabled()
  310. self.cbar.btn_poly_color_act_grp.setDisabled(False)
  311. self.cbar.btn_view_xseg_overlay_mask_act.setDisabled(False)
  312. self.cbar.btn_poly_type_act_grp.setDisabled(False)
  313. # Initial vars
  314. self.current_cursor = None
  315. self.mouse_hull_poly = None
  316. self.mouse_wire_poly = None
  317. self.drag_type = DragType.NONE
  318. self.mouse_cli_pt = np.zeros((2,), np.float32 )
  319. # Initial state
  320. self.set_op_mode(OpMode.NONE)
  321. self.set_color_scheme_id(1)
  322. self.set_poly_include_type(SegIEPolyType.INCLUDE)
  323. self.set_pt_edit_mode(PTEditMode.MOVE)
  324. self.set_view_lock(ViewLock.NONE)
  325. # Apply last state
  326. if self.last_state is not None:
  327. self.set_color_scheme_id(self.last_state.color_scheme_id)
  328. if self.last_state.op_mode is not None:
  329. self.set_op_mode(self.last_state.op_mode)
  330. self.initialized = True
  331. self.setMouseTracking(True)
  332. self.update_cursor()
  333. self.update()
  334. def finalize(self):
  335. if self.initialized:
  336. if self.op_mode == OpMode.DRAW_PTS:
  337. self.set_op_mode(OpMode.EDIT_PTS)
  338. self.last_state = sn(op_mode = self.op_mode if self.op_mode in [OpMode.VIEW_BAKED, OpMode.VIEW_XSEG_MASK] else None,
  339. color_scheme_id = self.color_scheme_id)
  340. self.img_pixmap = None
  341. self.update_cursor(is_finalize=True)
  342. self.setMouseTracking(False)
  343. self.setFocusPolicy(Qt.NoFocus)
  344. self.set_cbar_disabled()
  345. self.initialized = False
  346. self.update()
  347. # ====================================================================================
  348. # ====================================================================================
  349. # ====================================== GETTERS =====================================
  350. # ====================================================================================
  351. # ====================================================================================
  352. def is_initialized(self):
  353. return self.initialized
  354. def get_ie_polys(self):
  355. return self.ie_polys
  356. def get_cli_center_pt(self):
  357. return np.round(QSize_to_np(self.size())/2.0)
  358. def get_img_look_pt(self):
  359. img_look_pt = self.img_look_pt
  360. if img_look_pt is None:
  361. img_look_pt = self.img_size / 2
  362. return img_look_pt
  363. def get_view_scale(self):
  364. view_scale = self.view_scale
  365. if view_scale is None:
  366. # Calc as scale to fit
  367. min_cli_size = np.min(QSize_to_np(self.size()))
  368. max_img_size = np.max(self.img_size)
  369. view_scale = min_cli_size / max_img_size
  370. return view_scale
  371. def get_current_color_scheme(self):
  372. return self.canvas_config.color_schemes[self.color_scheme_id]
  373. def get_poly_pt_id_under_pt(self, poly, cli_pt):
  374. w = np.argwhere ( npla.norm ( cli_pt - self.img_to_cli_pt( poly.get_pts() ), axis=1 ) <= self.canvas_config.pt_select_radius )
  375. return None if len(w) == 0 else w[-1][0]
  376. def get_poly_edge_id_pt_under_pt(self, poly, cli_pt):
  377. cli_pts = self.img_to_cli_pt(poly.get_pts())
  378. if len(cli_pts) >= 3:
  379. edge_dists, projs = sd.dist_to_edges(cli_pts, cli_pt, is_closed=True)
  380. edge_id = np.argmin(edge_dists)
  381. dist = edge_dists[edge_id]
  382. pt = projs[edge_id]
  383. if dist <= self.canvas_config.pt_select_radius:
  384. return edge_id, pt
  385. return None, None
  386. def get_poly_by_pt_near_wire(self, cli_pt):
  387. pt_select_radius = self.canvas_config.pt_select_radius
  388. for poly in reversed(self.ie_polys.get_polys()):
  389. pts = poly.get_pts()
  390. if len(pts) >= 3:
  391. cli_pts = self.img_to_cli_pt(pts)
  392. edge_dists, _ = sd.dist_to_edges(cli_pts, cli_pt, is_closed=True)
  393. if np.min(edge_dists) <= pt_select_radius or \
  394. any( npla.norm ( cli_pt - cli_pts, axis=1 ) <= pt_select_radius ):
  395. return poly
  396. return None
  397. def get_poly_by_pt_in_hull(self, cli_pos):
  398. img_pos = self.cli_to_img_pt(cli_pos)
  399. for poly in reversed(self.ie_polys.get_polys()):
  400. pts = poly.get_pts()
  401. if len(pts) >= 3:
  402. if cv2.pointPolygonTest( pts, tuple(img_pos), False) >= 0:
  403. return poly
  404. return None
  405. def img_to_cli_pt(self, p):
  406. return (p - self.get_img_look_pt()) * self.get_view_scale() + self.get_cli_center_pt()# QSize_to_np(self.size())/2.0
  407. def cli_to_img_pt(self, p):
  408. return (p - self.get_cli_center_pt() ) / self.get_view_scale() + self.get_img_look_pt()
  409. def img_to_cli_rect(self, rect):
  410. tl = QPoint_to_np(rect.topLeft())
  411. xy = self.img_to_cli_pt(tl)
  412. xy2 = self.img_to_cli_pt(tl + QSize_to_np(rect.size()) ) - xy
  413. return QRect ( *xy.astype(np.int), *xy2.astype(np.int) )
  414. # ====================================================================================
  415. # ====================================================================================
  416. # ====================================== SETTERS =====================================
  417. # ====================================================================================
  418. # ====================================================================================
  419. def set_op_mode(self, op_mode, op_poly=None):
  420. if not hasattr(self,'op_mode'):
  421. self.op_mode = None
  422. self.op_poly = None
  423. if self.op_mode != op_mode:
  424. # Finalize prev mode
  425. if self.op_mode == OpMode.NONE:
  426. self.cbar.btn_poly_type_act_grp.setDisabled(True)
  427. elif self.op_mode == OpMode.DRAW_PTS:
  428. self.cbar.btn_undo_pt_act.setDisabled(True)
  429. self.cbar.btn_redo_pt_act.setDisabled(True)
  430. self.cbar.btn_view_lock_center_act.setDisabled(True)
  431. # Reset view_lock when exit from DRAW_PTS
  432. self.set_view_lock(ViewLock.NONE)
  433. # Remove unfinished poly
  434. if self.op_poly.get_pts_count() < 3:
  435. self.ie_polys.remove_poly(self.op_poly)
  436. elif self.op_mode == OpMode.EDIT_PTS:
  437. self.cbar.btn_pt_edit_mode_act.setDisabled(True)
  438. self.cbar.btn_delete_poly_act.setDisabled(True)
  439. # Reset pt_edit_move when exit from EDIT_PTS
  440. self.set_pt_edit_mode(PTEditMode.MOVE)
  441. elif self.op_mode == OpMode.VIEW_BAKED:
  442. self.cbar.btn_view_baked_mask_act.setChecked(False)
  443. elif self.op_mode == OpMode.VIEW_XSEG_MASK:
  444. self.cbar.btn_view_xseg_mask_act.setChecked(False)
  445. self.op_mode = op_mode
  446. # Initialize new mode
  447. if op_mode == OpMode.NONE:
  448. self.cbar.btn_poly_type_act_grp.setDisabled(False)
  449. elif op_mode == OpMode.DRAW_PTS:
  450. self.cbar.btn_undo_pt_act.setDisabled(False)
  451. self.cbar.btn_redo_pt_act.setDisabled(False)
  452. self.cbar.btn_view_lock_center_act.setDisabled(False)
  453. elif op_mode == OpMode.EDIT_PTS:
  454. self.cbar.btn_pt_edit_mode_act.setDisabled(False)
  455. self.cbar.btn_delete_poly_act.setDisabled(False)
  456. elif op_mode == OpMode.VIEW_BAKED:
  457. self.cbar.btn_view_baked_mask_act.setChecked(True )
  458. n = QImage_to_np ( self.q_img ).astype(np.float32) / 255.0
  459. h,w,c = n.shape
  460. mask = np.zeros( (h,w,1), dtype=np.float32 )
  461. self.ie_polys.overlay_mask(mask)
  462. n = (mask*255).astype(np.uint8)
  463. self.img_baked_pixmap = QPixmap.fromImage(QImage_from_np(n))
  464. elif op_mode == OpMode.VIEW_XSEG_MASK:
  465. self.cbar.btn_view_xseg_mask_act.setChecked(True)
  466. if op_mode in [OpMode.DRAW_PTS, OpMode.EDIT_PTS]:
  467. self.mouse_op_poly_pt_id = None
  468. self.mouse_op_poly_edge_id = None
  469. self.mouse_op_poly_edge_id_pt = None
  470. self.op_poly = op_poly
  471. if op_poly is not None:
  472. self.update_mouse_info()
  473. self.update_cursor()
  474. self.update()
  475. def set_pt_edit_mode(self, pt_edit_mode):
  476. if not hasattr(self, 'pt_edit_mode') or self.pt_edit_mode != pt_edit_mode:
  477. self.pt_edit_mode = pt_edit_mode
  478. self.update_cursor()
  479. self.update()
  480. self.cbar.btn_pt_edit_mode_act.setChecked( self.pt_edit_mode == PTEditMode.ADD_DEL )
  481. def set_view_lock(self, view_lock):
  482. if not hasattr(self, 'view_lock') or self.view_lock != view_lock:
  483. if hasattr(self, 'view_lock') and self.view_lock != view_lock:
  484. if view_lock == ViewLock.CENTER:
  485. self.img_look_pt = self.mouse_img_pt
  486. QCursor.setPos ( self.mapToGlobal( QPoint_from_np(self.img_to_cli_pt(self.img_look_pt)) ))
  487. self.view_lock = view_lock
  488. self.update()
  489. self.cbar.btn_view_lock_center_act.setChecked( self.view_lock == ViewLock.CENTER )
  490. def set_cbar_disabled(self):
  491. self.cbar.btn_delete_poly_act.setDisabled(True)
  492. self.cbar.btn_undo_pt_act.setDisabled(True)
  493. self.cbar.btn_redo_pt_act.setDisabled(True)
  494. self.cbar.btn_pt_edit_mode_act.setDisabled(True)
  495. self.cbar.btn_view_lock_center_act.setDisabled(True)
  496. self.cbar.btn_poly_color_act_grp.setDisabled(True)
  497. self.cbar.btn_view_xseg_overlay_mask_act.setDisabled(True)
  498. self.cbar.btn_poly_type_act_grp.setDisabled(True)
  499. def set_color_scheme_id(self, id):
  500. if self.op_mode == OpMode.VIEW_BAKED or self.op_mode == OpMode.VIEW_XSEG_MASK:
  501. self.set_op_mode(OpMode.NONE)
  502. if not hasattr(self, 'color_scheme_id') or self.color_scheme_id != id:
  503. self.color_scheme_id = id
  504. self.update_cursor()
  505. self.update()
  506. if self.color_scheme_id == 0:
  507. self.cbar.btn_poly_color_red_act.setChecked( True )
  508. elif self.color_scheme_id == 1:
  509. self.cbar.btn_poly_color_green_act.setChecked( True )
  510. elif self.color_scheme_id == 2:
  511. self.cbar.btn_poly_color_blue_act.setChecked( True )
  512. def set_poly_include_type(self, poly_include_type):
  513. if not hasattr(self, 'poly_include_type' ) or \
  514. ( self.poly_include_type != poly_include_type and \
  515. self.op_mode in [OpMode.NONE, OpMode.EDIT_PTS] ):
  516. self.poly_include_type = poly_include_type
  517. self.update()
  518. self.cbar.btn_poly_type_include_act.setChecked(self.poly_include_type == SegIEPolyType.INCLUDE)
  519. self.cbar.btn_poly_type_exclude_act.setChecked(self.poly_include_type == SegIEPolyType.EXCLUDE)
  520. # ====================================================================================
  521. # ====================================================================================
  522. # ====================================== METHODS =====================================
  523. # ====================================================================================
  524. # ====================================================================================
  525. def update_cursor(self, is_finalize=False):
  526. if not self.initialized:
  527. return
  528. if not self.mouse_in_widget or is_finalize:
  529. if self.current_cursor is not None:
  530. QApplication.restoreOverrideCursor()
  531. self.current_cursor = None
  532. else:
  533. color_cc = self.get_current_color_scheme().cross_cursor
  534. nc = Qt.ArrowCursor
  535. if self.drag_type == DragType.IMAGE_LOOK:
  536. nc = Qt.ClosedHandCursor
  537. else:
  538. if self.op_mode == OpMode.NONE:
  539. nc = color_cc
  540. if self.mouse_wire_poly is not None:
  541. nc = Qt.PointingHandCursor
  542. elif self.op_mode == OpMode.DRAW_PTS:
  543. nc = color_cc
  544. elif self.op_mode == OpMode.EDIT_PTS:
  545. nc = Qt.ArrowCursor
  546. if self.mouse_op_poly_pt_id is not None:
  547. nc = Qt.PointingHandCursor
  548. if self.pt_edit_mode == PTEditMode.ADD_DEL:
  549. if self.mouse_op_poly_edge_id is not None and \
  550. self.mouse_op_poly_pt_id is None:
  551. nc = color_cc
  552. if self.current_cursor != nc:
  553. if self.current_cursor is None:
  554. QApplication.setOverrideCursor(nc)
  555. else:
  556. QApplication.changeOverrideCursor(nc)
  557. self.current_cursor = nc
  558. def update_mouse_info(self, mouse_cli_pt=None):
  559. """
  560. Update selected polys/edges/points by given mouse position
  561. """
  562. if mouse_cli_pt is not None:
  563. self.mouse_cli_pt = mouse_cli_pt.astype(np.float32)
  564. self.mouse_img_pt = self.cli_to_img_pt(self.mouse_cli_pt)
  565. new_mouse_hull_poly = self.get_poly_by_pt_in_hull(self.mouse_cli_pt)
  566. if self.mouse_hull_poly != new_mouse_hull_poly:
  567. self.mouse_hull_poly = new_mouse_hull_poly
  568. self.update_cursor()
  569. self.update()
  570. new_mouse_wire_poly = self.get_poly_by_pt_near_wire(self.mouse_cli_pt)
  571. if self.mouse_wire_poly != new_mouse_wire_poly:
  572. self.mouse_wire_poly = new_mouse_wire_poly
  573. self.update_cursor()
  574. self.update()
  575. if self.op_mode in [OpMode.DRAW_PTS, OpMode.EDIT_PTS]:
  576. new_mouse_op_poly_pt_id = self.get_poly_pt_id_under_pt (self.op_poly, self.mouse_cli_pt)
  577. if self.mouse_op_poly_pt_id != new_mouse_op_poly_pt_id:
  578. self.mouse_op_poly_pt_id = new_mouse_op_poly_pt_id
  579. self.update_cursor()
  580. self.update()
  581. new_mouse_op_poly_edge_id,\
  582. new_mouse_op_poly_edge_id_pt = self.get_poly_edge_id_pt_under_pt (self.op_poly, self.mouse_cli_pt)
  583. if self.mouse_op_poly_edge_id != new_mouse_op_poly_edge_id:
  584. self.mouse_op_poly_edge_id = new_mouse_op_poly_edge_id
  585. self.update_cursor()
  586. self.update()
  587. if (self.mouse_op_poly_edge_id_pt.__class__ != new_mouse_op_poly_edge_id_pt.__class__) or \
  588. (isinstance(self.mouse_op_poly_edge_id_pt, np.ndarray) and \
  589. all(self.mouse_op_poly_edge_id_pt != new_mouse_op_poly_edge_id_pt)):
  590. self.mouse_op_poly_edge_id_pt = new_mouse_op_poly_edge_id_pt
  591. self.update_cursor()
  592. self.update()
  593. def action_undo_pt(self):
  594. if self.drag_type == DragType.NONE:
  595. if self.op_mode == OpMode.DRAW_PTS:
  596. if self.op_poly.undo() == 0:
  597. self.ie_polys.remove_poly (self.op_poly)
  598. self.set_op_mode(OpMode.NONE)
  599. self.update()
  600. def action_redo_pt(self):
  601. if self.drag_type == DragType.NONE:
  602. if self.op_mode == OpMode.DRAW_PTS:
  603. self.op_poly.redo()
  604. self.update()
  605. def action_delete_poly(self):
  606. if self.op_mode == OpMode.EDIT_PTS and \
  607. self.drag_type == DragType.NONE and \
  608. self.pt_edit_mode == PTEditMode.MOVE:
  609. # Delete current poly
  610. self.ie_polys.remove_poly (self.op_poly)
  611. self.set_op_mode(OpMode.NONE)
  612. # ====================================================================================
  613. # ====================================================================================
  614. # ================================== OVERRIDE QT METHODS =============================
  615. # ====================================================================================
  616. # ====================================================================================
  617. def on_keyPressEvent(self, ev):
  618. if not self.initialized:
  619. return
  620. key = ev.key()
  621. key_mods = int(ev.modifiers())
  622. if self.op_mode == OpMode.DRAW_PTS:
  623. self.set_view_lock(ViewLock.CENTER if key_mods == Qt.ShiftModifier else ViewLock.NONE )
  624. elif self.op_mode == OpMode.EDIT_PTS:
  625. self.set_pt_edit_mode(PTEditMode.ADD_DEL if key_mods == Qt.ControlModifier else PTEditMode.MOVE )
  626. def on_keyReleaseEvent(self, ev):
  627. if not self.initialized:
  628. return
  629. key = ev.key()
  630. key_mods = int(ev.modifiers())
  631. if self.op_mode == OpMode.DRAW_PTS:
  632. self.set_view_lock(ViewLock.CENTER if key_mods == Qt.ShiftModifier else ViewLock.NONE )
  633. elif self.op_mode == OpMode.EDIT_PTS:
  634. self.set_pt_edit_mode(PTEditMode.ADD_DEL if key_mods == Qt.ControlModifier else PTEditMode.MOVE )
  635. def enterEvent(self, ev):
  636. super().enterEvent(ev)
  637. self.mouse_in_widget = True
  638. self.update_cursor()
  639. def leaveEvent(self, ev):
  640. super().leaveEvent(ev)
  641. self.mouse_in_widget = False
  642. self.update_cursor()
  643. def mousePressEvent(self, ev):
  644. super().mousePressEvent(ev)
  645. if not self.initialized:
  646. return
  647. self.update_mouse_info(QPoint_to_np(ev.pos()))
  648. btn = ev.button()
  649. if btn == Qt.LeftButton:
  650. if self.op_mode == OpMode.NONE:
  651. # Clicking in NO OPERATION mode
  652. if self.mouse_wire_poly is not None:
  653. # Click on wire on any poly -> switch to EDIT_MODE
  654. self.set_op_mode(OpMode.EDIT_PTS, op_poly=self.mouse_wire_poly)
  655. else:
  656. # Click on empty space -> create new poly with one point
  657. new_poly = self.ie_polys.add_poly(self.poly_include_type)
  658. self.ie_polys.sort()
  659. new_poly.add_pt(*self.mouse_img_pt)
  660. self.set_op_mode(OpMode.DRAW_PTS, op_poly=new_poly )
  661. elif self.op_mode == OpMode.DRAW_PTS:
  662. # Clicking in DRAW_PTS mode
  663. if len(self.op_poly.get_pts()) >= 3 and self.mouse_op_poly_pt_id == 0:
  664. # Click on first point -> close poly and switch to edit mode
  665. self.set_op_mode(OpMode.EDIT_PTS, op_poly=self.op_poly)
  666. else:
  667. # Click on empty space -> add point to current poly
  668. self.op_poly.add_pt(*self.mouse_img_pt)
  669. self.update()
  670. elif self.op_mode == OpMode.EDIT_PTS:
  671. # Clicking in EDIT_PTS mode
  672. if self.mouse_op_poly_pt_id is not None:
  673. # Click on point of op_poly
  674. if self.pt_edit_mode == PTEditMode.ADD_DEL:
  675. # in mode 'delete point'
  676. self.op_poly.remove_pt(self.mouse_op_poly_pt_id)
  677. if self.op_poly.get_pts_count() < 3:
  678. # not enough points after delete -> remove poly
  679. self.ie_polys.remove_poly (self.op_poly)
  680. self.set_op_mode(OpMode.NONE)
  681. self.update()
  682. elif self.drag_type == DragType.NONE:
  683. # otherwise -> start drag
  684. self.drag_type = DragType.POLY_PT
  685. self.drag_cli_pt = self.mouse_cli_pt
  686. self.drag_poly_pt_id = self.mouse_op_poly_pt_id
  687. self.drag_poly_pt = self.op_poly.get_pts()[ self.drag_poly_pt_id ]
  688. elif self.mouse_op_poly_edge_id is not None:
  689. # Click on edge of op_poly
  690. if self.pt_edit_mode == PTEditMode.ADD_DEL:
  691. # in mode 'insert new point'
  692. edge_img_pt = self.cli_to_img_pt(self.mouse_op_poly_edge_id_pt)
  693. self.op_poly.insert_pt (self.mouse_op_poly_edge_id+1, edge_img_pt)
  694. self.update()
  695. else:
  696. # Otherwise do nothing
  697. pass
  698. else:
  699. # other cases -> unselect poly
  700. self.set_op_mode(OpMode.NONE)
  701. elif btn == Qt.MiddleButton:
  702. if self.drag_type == DragType.NONE:
  703. # Start image drag
  704. self.drag_type = DragType.IMAGE_LOOK
  705. self.drag_cli_pt = self.mouse_cli_pt
  706. self.drag_img_look_pt = self.get_img_look_pt()
  707. self.update_cursor()
  708. def mouseReleaseEvent(self, ev):
  709. super().mouseReleaseEvent(ev)
  710. if not self.initialized:
  711. return
  712. self.update_mouse_info(QPoint_to_np(ev.pos()))
  713. btn = ev.button()
  714. if btn == Qt.LeftButton:
  715. if self.op_mode == OpMode.EDIT_PTS:
  716. if self.drag_type == DragType.POLY_PT:
  717. self.drag_type = DragType.NONE
  718. self.update()
  719. elif btn == Qt.MiddleButton:
  720. if self.drag_type == DragType.IMAGE_LOOK:
  721. self.drag_type = DragType.NONE
  722. self.update_cursor()
  723. self.update()
  724. def mouseMoveEvent(self, ev):
  725. super().mouseMoveEvent(ev)
  726. if not self.initialized:
  727. return
  728. prev_mouse_cli_pt = self.mouse_cli_pt
  729. self.update_mouse_info(QPoint_to_np(ev.pos()))
  730. if self.view_lock == ViewLock.CENTER:
  731. if npla.norm(self.mouse_cli_pt - prev_mouse_cli_pt) >= 1:
  732. self.img_look_pt = self.mouse_img_pt
  733. QCursor.setPos ( self.mapToGlobal( QPoint_from_np(self.img_to_cli_pt(self.img_look_pt)) ))
  734. self.update()
  735. if self.drag_type == DragType.IMAGE_LOOK:
  736. delta_pt = self.cli_to_img_pt(self.mouse_cli_pt) - self.cli_to_img_pt(self.drag_cli_pt)
  737. self.img_look_pt = self.drag_img_look_pt - delta_pt
  738. self.update()
  739. if self.op_mode == OpMode.DRAW_PTS:
  740. self.update()
  741. elif self.op_mode == OpMode.EDIT_PTS:
  742. if self.drag_type == DragType.POLY_PT:
  743. delta_pt = self.cli_to_img_pt(self.mouse_cli_pt) - self.cli_to_img_pt(self.drag_cli_pt)
  744. self.op_poly.set_point(self.drag_poly_pt_id, self.drag_poly_pt + delta_pt)
  745. self.update()
  746. def wheelEvent(self, ev):
  747. super().wheelEvent(ev)
  748. if not self.initialized:
  749. return
  750. mods = int(ev.modifiers())
  751. delta = ev.angleDelta()
  752. cli_pt = QPoint_to_np(ev.pos())
  753. if self.drag_type == DragType.NONE:
  754. sign = np.sign( delta.y() )
  755. prev_img_pos = self.cli_to_img_pt (cli_pt)
  756. delta_scale = sign*0.2 + sign * self.get_view_scale() / 10.0
  757. self.view_scale = np.clip(self.get_view_scale() + delta_scale, 1.0, 20.0)
  758. new_img_pos = self.cli_to_img_pt (cli_pt)
  759. if sign > 0:
  760. self.img_look_pt = self.get_img_look_pt() + (prev_img_pos-new_img_pos)#*1.5
  761. else:
  762. QCursor.setPos ( self.mapToGlobal(QPoint_from_np(self.img_to_cli_pt(prev_img_pos))) )
  763. self.update()
  764. def paintEvent(self, event):
  765. super().paintEvent(event)
  766. if not self.initialized:
  767. return
  768. qp = self.qp
  769. qp.begin(self)
  770. qp.setRenderHint(QPainter.Antialiasing)
  771. qp.setRenderHint(QPainter.HighQualityAntialiasing)
  772. qp.setRenderHint(QPainter.SmoothPixmapTransform)
  773. src_rect = QRect(0, 0, *self.img_size)
  774. dst_rect = self.img_to_cli_rect( src_rect )
  775. if self.op_mode == OpMode.VIEW_BAKED:
  776. qp.drawPixmap(dst_rect, self.img_baked_pixmap, src_rect)
  777. elif self.op_mode == OpMode.VIEW_XSEG_MASK:
  778. if self.xseg_mask_pixmap is not None:
  779. qp.drawPixmap(dst_rect, self.xseg_mask_pixmap, src_rect)
  780. else:
  781. if self.cbar.btn_view_xseg_overlay_mask_act.isChecked() and \
  782. self.xseg_overlay_mask_pixmap is not None:
  783. qp.drawPixmap(dst_rect, self.xseg_overlay_mask_pixmap, src_rect)
  784. elif self.img_pixmap is not None:
  785. qp.drawPixmap(dst_rect, self.img_pixmap, src_rect)
  786. polys = self.ie_polys.get_polys()
  787. polys_len = len(polys)
  788. color_scheme = self.get_current_color_scheme()
  789. pt_rad = self.canvas_config.pt_radius
  790. pt_rad_x2 = pt_rad*2
  791. pt_select_radius = self.canvas_config.pt_select_radius
  792. op_mode = self.op_mode
  793. op_poly = self.op_poly
  794. for i,poly in enumerate(polys):
  795. selected_pt_path = QPainterPath()
  796. poly_line_path = QPainterPath()
  797. pts_line_path = QPainterPath()
  798. pt_remove_cli_pt = None
  799. poly_pts = poly.get_pts()
  800. for pt_id, img_pt in enumerate(poly_pts):
  801. cli_pt = self.img_to_cli_pt(img_pt)
  802. q_cli_pt = QPoint_from_np(cli_pt)
  803. if pt_id == 0:
  804. poly_line_path.moveTo(q_cli_pt)
  805. else:
  806. poly_line_path.lineTo(q_cli_pt)
  807. if poly == op_poly:
  808. if self.op_mode == OpMode.DRAW_PTS or \
  809. (self.op_mode == OpMode.EDIT_PTS and \
  810. (self.pt_edit_mode == PTEditMode.MOVE) or \
  811. (self.pt_edit_mode == PTEditMode.ADD_DEL and self.mouse_op_poly_pt_id == pt_id) \
  812. ):
  813. pts_line_path.moveTo( QPoint_from_np(cli_pt + np.float32([0,-pt_rad])) )
  814. pts_line_path.lineTo( QPoint_from_np(cli_pt + np.float32([0,pt_rad])) )
  815. pts_line_path.moveTo( QPoint_from_np(cli_pt + np.float32([-pt_rad,0])) )
  816. pts_line_path.lineTo( QPoint_from_np(cli_pt + np.float32([pt_rad,0])) )
  817. if (self.op_mode == OpMode.EDIT_PTS and \
  818. self.pt_edit_mode == PTEditMode.ADD_DEL and \
  819. self.mouse_op_poly_pt_id == pt_id):
  820. pt_remove_cli_pt = cli_pt
  821. if self.op_mode == OpMode.DRAW_PTS and \
  822. len(op_poly.get_pts()) >= 3 and pt_id == 0 and self.mouse_op_poly_pt_id == pt_id:
  823. # Circle around poly point
  824. selected_pt_path.addEllipse(q_cli_pt, pt_rad_x2, pt_rad_x2)
  825. if poly == op_poly:
  826. if op_mode == OpMode.DRAW_PTS:
  827. # Line from last point to mouse
  828. poly_line_path.lineTo( QPoint_from_np(self.mouse_cli_pt) )
  829. if self.mouse_op_poly_pt_id is not None:
  830. pass
  831. if self.mouse_op_poly_edge_id_pt is not None:
  832. if self.pt_edit_mode == PTEditMode.ADD_DEL and self.mouse_op_poly_pt_id is None:
  833. # Ready to insert point on edge
  834. m_cli_pt = self.mouse_op_poly_edge_id_pt
  835. pts_line_path.moveTo( QPoint_from_np(m_cli_pt + np.float32([0,-pt_rad])) )
  836. pts_line_path.lineTo( QPoint_from_np(m_cli_pt + np.float32([0,pt_rad])) )
  837. pts_line_path.moveTo( QPoint_from_np(m_cli_pt + np.float32([-pt_rad,0])) )
  838. pts_line_path.lineTo( QPoint_from_np(m_cli_pt + np.float32([pt_rad,0])) )
  839. if len(poly_pts) >= 2:
  840. # Closing poly line
  841. poly_line_path.lineTo( QPoint_from_np(self.img_to_cli_pt(poly_pts[0])) )
  842. # Draw calls
  843. qp.setPen(color_scheme.pt_outline_pen)
  844. qp.setBrush(QBrush())
  845. qp.drawPath(selected_pt_path)
  846. qp.setPen(color_scheme.poly_outline_solid_pen)
  847. qp.setBrush(QBrush())
  848. qp.drawPath(pts_line_path)
  849. if poly.get_type() == SegIEPolyType.INCLUDE:
  850. qp.setPen(color_scheme.poly_outline_solid_pen)
  851. else:
  852. qp.setPen(color_scheme.poly_outline_dot_pen)
  853. qp.setBrush(color_scheme.poly_unselected_brush)
  854. if op_mode == OpMode.NONE:
  855. if poly == self.mouse_wire_poly:
  856. qp.setBrush(color_scheme.poly_selected_brush)
  857. #else:
  858. # if poly == op_poly:
  859. # qp.setBrush(color_scheme.poly_selected_brush)
  860. qp.drawPath(poly_line_path)
  861. if pt_remove_cli_pt is not None:
  862. qp.setPen(color_scheme.poly_outline_solid_pen)
  863. qp.setBrush(QBrush())
  864. qp.drawLine( *(pt_remove_cli_pt + np.float32([-pt_rad_x2,-pt_rad_x2])), *(pt_remove_cli_pt + np.float32([pt_rad_x2,pt_rad_x2])) )
  865. qp.drawLine( *(pt_remove_cli_pt + np.float32([-pt_rad_x2,pt_rad_x2])), *(pt_remove_cli_pt + np.float32([pt_rad_x2,-pt_rad_x2])) )
  866. qp.end()
  867. class QCanvas(QFrame):
  868. def __init__(self):
  869. super().__init__()
  870. self.canvas_control_left_bar = QCanvasControlsLeftBar()
  871. self.canvas_control_right_bar = QCanvasControlsRightBar()
  872. cbar = sn( btn_poly_color_red_act = self.canvas_control_right_bar.btn_poly_color_red_act,
  873. btn_poly_color_green_act = self.canvas_control_right_bar.btn_poly_color_green_act,
  874. btn_poly_color_blue_act = self.canvas_control_right_bar.btn_poly_color_blue_act,
  875. btn_view_baked_mask_act = self.canvas_control_right_bar.btn_view_baked_mask_act,
  876. btn_view_xseg_mask_act = self.canvas_control_right_bar.btn_view_xseg_mask_act,
  877. btn_view_xseg_overlay_mask_act = self.canvas_control_right_bar.btn_view_xseg_overlay_mask_act,
  878. btn_poly_color_act_grp = self.canvas_control_right_bar.btn_poly_color_act_grp,
  879. btn_view_lock_center_act = self.canvas_control_right_bar.btn_view_lock_center_act,
  880. btn_poly_type_include_act = self.canvas_control_left_bar.btn_poly_type_include_act,
  881. btn_poly_type_exclude_act = self.canvas_control_left_bar.btn_poly_type_exclude_act,
  882. btn_poly_type_act_grp = self.canvas_control_left_bar.btn_poly_type_act_grp,
  883. btn_undo_pt_act = self.canvas_control_left_bar.btn_undo_pt_act,
  884. btn_redo_pt_act = self.canvas_control_left_bar.btn_redo_pt_act,
  885. btn_delete_poly_act = self.canvas_control_left_bar.btn_delete_poly_act,
  886. btn_pt_edit_mode_act = self.canvas_control_left_bar.btn_pt_edit_mode_act )
  887. self.op = QCanvasOperator(cbar)
  888. self.l = QHBoxLayout()
  889. self.l.setContentsMargins(0,0,0,0)
  890. self.l.addWidget(self.canvas_control_left_bar)
  891. self.l.addWidget(self.op)
  892. self.l.addWidget(self.canvas_control_right_bar)
  893. self.setLayout(self.l)
  894. class LoaderQSubprocessor(QSubprocessor):
  895. def __init__(self, image_paths, q_label, q_progressbar, on_finish_func ):
  896. self.image_paths = image_paths
  897. self.image_paths_len = len(image_paths)
  898. self.idxs = [*range(self.image_paths_len)]
  899. self.filtered_image_paths = self.image_paths.copy()
  900. self.image_paths_has_ie_polys = { image_path : False for image_path in self.image_paths }
  901. self.q_label = q_label
  902. self.q_progressbar = q_progressbar
  903. self.q_progressbar.setRange(0, self.image_paths_len)
  904. self.q_progressbar.setValue(0)
  905. self.q_progressbar.update()
  906. self.on_finish_func = on_finish_func
  907. self.done_count = 0
  908. super().__init__('LoaderQSubprocessor', LoaderQSubprocessor.Cli, 60)
  909. def get_data(self, host_dict):
  910. if len (self.idxs) > 0:
  911. idx = self.idxs.pop(0)
  912. image_path = self.image_paths[idx]
  913. self.q_label.setText(f'{QStringDB.loading_tip}... {image_path.name}')
  914. return idx, image_path
  915. return None
  916. def on_clients_finalized(self):
  917. self.on_finish_func([x for x in self.filtered_image_paths if x is not None], self.image_paths_has_ie_polys)
  918. def on_data_return (self, host_dict, data):
  919. self.idxs.insert(0, data[0])
  920. def on_result (self, host_dict, data, result):
  921. idx, has_dflimg, has_ie_polys = result
  922. if not has_dflimg:
  923. self.filtered_image_paths[idx] = None
  924. self.image_paths_has_ie_polys[self.image_paths[idx]] = has_ie_polys
  925. self.done_count += 1
  926. if self.q_progressbar is not None:
  927. self.q_progressbar.setValue(self.done_count)
  928. class Cli(QSubprocessor.Cli):
  929. def process_data(self, data):
  930. idx, filename = data
  931. dflimg = DFLIMG.load(filename)
  932. if dflimg is not None and dflimg.has_data():
  933. ie_polys = dflimg.get_seg_ie_polys()
  934. return idx, True, ie_polys.has_polys()
  935. return idx, False, False
  936. class MainWindow(QXMainWindow):
  937. def __init__(self, input_dirpath, cfg_root_path):
  938. self.loading_frame = None
  939. self.help_frame = None
  940. super().__init__()
  941. self.input_dirpath = input_dirpath
  942. self.trash_dirpath = input_dirpath.parent / (input_dirpath.name + '_trash')
  943. self.cfg_root_path = cfg_root_path
  944. self.cfg_path = cfg_root_path / 'MainWindow_cfg.dat'
  945. self.cfg_dict = pickle.loads(self.cfg_path.read_bytes()) if self.cfg_path.exists() else {}
  946. self.cached_images = {}
  947. self.cached_has_ie_polys = {}
  948. self.initialize_ui()
  949. # Loader
  950. self.loading_frame = QFrame(self.main_canvas_frame)
  951. self.loading_frame.setAutoFillBackground(True)
  952. self.loading_frame.setFrameShape(QFrame.StyledPanel)
  953. self.loader_label = QLabel()
  954. self.loader_progress_bar = QProgressBar()
  955. intro_image = QLabel()
  956. intro_image.setPixmap( QPixmap.fromImage(QImageDB.intro) )
  957. intro_image_frame_l = QVBoxLayout()
  958. intro_image_frame_l.addWidget(intro_image, alignment=Qt.AlignCenter)
  959. intro_image_frame = QFrame()
  960. intro_image_frame.setSizePolicy (QSizePolicy.Expanding, QSizePolicy.Expanding)
  961. intro_image_frame.setLayout(intro_image_frame_l)
  962. loading_frame_l = QVBoxLayout()
  963. loading_frame_l.addWidget (intro_image_frame)
  964. loading_frame_l.addWidget (self.loader_label)
  965. loading_frame_l.addWidget (self.loader_progress_bar)
  966. self.loading_frame.setLayout(loading_frame_l)
  967. self.loader_subprocessor = LoaderQSubprocessor( image_paths=pathex.get_image_paths(input_dirpath, return_Path_class=True),
  968. q_label=self.loader_label,
  969. q_progressbar=self.loader_progress_bar,
  970. on_finish_func=self.on_loader_finish )
  971. def on_loader_finish(self, image_paths, image_paths_has_ie_polys):
  972. self.image_paths_done = []
  973. self.image_paths = image_paths
  974. self.image_paths_has_ie_polys = image_paths_has_ie_polys
  975. self.set_has_ie_polys_count ( len([ 1 for x in self.image_paths_has_ie_polys if self.image_paths_has_ie_polys[x] == True]) )
  976. self.loading_frame.hide()
  977. self.loading_frame = None
  978. self.process_next_image(first_initialization=True)
  979. def closeEvent(self, ev):
  980. self.cfg_dict['geometry'] = self.saveGeometry().data()
  981. self.cfg_path.write_bytes( pickle.dumps(self.cfg_dict) )
  982. def update_cached_images (self, count=5):
  983. d = self.cached_images
  984. for image_path in self.image_paths_done[:-count]+self.image_paths[count:]:
  985. if image_path in d:
  986. del d[image_path]
  987. for image_path in self.image_paths[:count]+self.image_paths_done[-count:]:
  988. if image_path not in d:
  989. img = cv2_imread(image_path)
  990. if img is not None:
  991. d[image_path] = img
  992. def load_image(self, image_path):
  993. try:
  994. img = self.cached_images.get(image_path, None)
  995. if img is None:
  996. img = cv2_imread(image_path)
  997. self.cached_images[image_path] = img
  998. if img is None:
  999. io.log_err(f'Unable to load {image_path}')
  1000. except:
  1001. img = None
  1002. return img
  1003. def update_preview_bar(self):
  1004. count = self.image_bar.get_preview_images_count()
  1005. d = self.cached_images
  1006. prev_imgs = [ d.get(image_path, None) for image_path in self.image_paths_done[-1:-count:-1] ]
  1007. next_imgs = [ d.get(image_path, None) for image_path in self.image_paths[:count] ]
  1008. self.image_bar.update_images(prev_imgs, next_imgs)
  1009. def canvas_initialize(self, image_path, only_has_polys=False):
  1010. if only_has_polys and not self.image_paths_has_ie_polys[image_path]:
  1011. return False
  1012. dflimg = DFLIMG.load(image_path)
  1013. if not dflimg or not dflimg.has_data():
  1014. return False
  1015. ie_polys = dflimg.get_seg_ie_polys()
  1016. xseg_mask = dflimg.get_xseg_mask()
  1017. img = self.load_image(image_path)
  1018. if img is None:
  1019. return False
  1020. self.canvas.op.initialize ( img, ie_polys=ie_polys, xseg_mask=xseg_mask )
  1021. self.filename_label.setText(f"{image_path.name}")
  1022. return True
  1023. def canvas_finalize(self, image_path):
  1024. self.canvas.op.finalize()
  1025. if image_path.exists():
  1026. dflimg = DFLIMG.load(image_path)
  1027. ie_polys = dflimg.get_seg_ie_polys()
  1028. new_ie_polys = self.canvas.op.get_ie_polys()
  1029. if not new_ie_polys.identical(ie_polys):
  1030. prev_has_polys = self.image_paths_has_ie_polys[image_path]
  1031. self.image_paths_has_ie_polys[image_path] = new_ie_polys.has_polys()
  1032. new_has_polys = self.image_paths_has_ie_polys[image_path]
  1033. if not prev_has_polys and new_has_polys:
  1034. self.set_has_ie_polys_count ( self.get_has_ie_polys_count() +1)
  1035. elif prev_has_polys and not new_has_polys:
  1036. self.set_has_ie_polys_count ( self.get_has_ie_polys_count() -1)
  1037. dflimg.set_seg_ie_polys( new_ie_polys )
  1038. dflimg.save()
  1039. self.filename_label.setText(f"")
  1040. def process_prev_image(self):
  1041. key_mods = QApplication.keyboardModifiers()
  1042. step = 5 if key_mods == Qt.ShiftModifier else 1
  1043. only_has_polys = key_mods == Qt.ControlModifier
  1044. if self.canvas.op.is_initialized():
  1045. self.canvas_finalize(self.image_paths[0])
  1046. while True:
  1047. for _ in range(step):
  1048. if len(self.image_paths_done) != 0:
  1049. self.image_paths.insert (0, self.image_paths_done.pop(-1))
  1050. else:
  1051. break
  1052. if len(self.image_paths) == 0:
  1053. break
  1054. ret = self.canvas_initialize(self.image_paths[0], len(self.image_paths_done) != 0 and only_has_polys)
  1055. if ret or len(self.image_paths_done) == 0:
  1056. break
  1057. self.update_cached_images()
  1058. self.update_preview_bar()
  1059. def process_next_image(self, first_initialization=False):
  1060. key_mods = QApplication.keyboardModifiers()
  1061. step = 0 if first_initialization else 5 if key_mods == Qt.ShiftModifier else 1
  1062. only_has_polys = False if first_initialization else key_mods == Qt.ControlModifier
  1063. if self.canvas.op.is_initialized():
  1064. self.canvas_finalize(self.image_paths[0])
  1065. while True:
  1066. for _ in range(step):
  1067. if len(self.image_paths) != 0:
  1068. self.image_paths_done.append(self.image_paths.pop(0))
  1069. else:
  1070. break
  1071. if len(self.image_paths) == 0:
  1072. break
  1073. if self.canvas_initialize(self.image_paths[0], only_has_polys):
  1074. break
  1075. self.update_cached_images()
  1076. self.update_preview_bar()
  1077. def trash_current_image(self):
  1078. self.process_next_image()
  1079. img_path = self.image_paths_done.pop(-1)
  1080. img_path = Path(img_path)
  1081. self.trash_dirpath.mkdir(parents=True, exist_ok=True)
  1082. img_path.rename( self.trash_dirpath / img_path.name )
  1083. self.update_cached_images()
  1084. self.update_preview_bar()
  1085. def initialize_ui(self):
  1086. self.canvas = QCanvas()
  1087. image_bar = self.image_bar = ImagePreviewSequenceBar(preview_images_count=9, icon_size=QUIConfig.preview_bar_icon_q_size.width())
  1088. image_bar.setSizePolicy ( QSizePolicy.Fixed, QSizePolicy.Fixed )
  1089. btn_prev_image = QXIconButton(QIconDB.left, QStringDB.btn_prev_image_tip, shortcut='A', click_func=self.process_prev_image)
  1090. btn_prev_image.setIconSize(QUIConfig.preview_bar_icon_q_size)
  1091. btn_next_image = QXIconButton(QIconDB.right, QStringDB.btn_next_image_tip, shortcut='D', click_func=self.process_next_image)
  1092. btn_next_image.setIconSize(QUIConfig.preview_bar_icon_q_size)
  1093. btn_delete_image = QXIconButton(QIconDB.trashcan, QStringDB.btn_delete_image_tip, shortcut='X', click_func=self.trash_current_image)
  1094. btn_delete_image.setIconSize(QUIConfig.preview_bar_icon_q_size)
  1095. pad_image = QWidget()
  1096. pad_image.setFixedSize(QUIConfig.preview_bar_icon_q_size)
  1097. preview_image_bar_frame_l = QHBoxLayout()
  1098. preview_image_bar_frame_l.setContentsMargins(0,0,0,0)
  1099. preview_image_bar_frame_l.addWidget ( pad_image, alignment=Qt.AlignCenter)
  1100. preview_image_bar_frame_l.addWidget ( btn_prev_image, alignment=Qt.AlignCenter)
  1101. preview_image_bar_frame_l.addWidget ( image_bar)
  1102. preview_image_bar_frame_l.addWidget ( btn_next_image, alignment=Qt.AlignCenter)
  1103. #preview_image_bar_frame_l.addWidget ( btn_delete_image, alignment=Qt.AlignCenter)
  1104. preview_image_bar_frame = QFrame()
  1105. preview_image_bar_frame.setSizePolicy ( QSizePolicy.Fixed, QSizePolicy.Fixed )
  1106. preview_image_bar_frame.setLayout(preview_image_bar_frame_l)
  1107. preview_image_bar_frame2_l = QHBoxLayout()
  1108. preview_image_bar_frame2_l.setContentsMargins(0,0,0,0)
  1109. preview_image_bar_frame2_l.addWidget ( btn_delete_image, alignment=Qt.AlignCenter)
  1110. preview_image_bar_frame2 = QFrame()
  1111. preview_image_bar_frame2.setSizePolicy ( QSizePolicy.Fixed, QSizePolicy.Fixed )
  1112. preview_image_bar_frame2.setLayout(preview_image_bar_frame2_l)
  1113. preview_image_bar_l = QHBoxLayout()
  1114. preview_image_bar_l.addWidget (preview_image_bar_frame, alignment=Qt.AlignCenter)
  1115. preview_image_bar_l.addWidget (preview_image_bar_frame2)
  1116. preview_image_bar = QFrame()
  1117. preview_image_bar.setFrameShape(QFrame.StyledPanel)
  1118. preview_image_bar.setSizePolicy ( QSizePolicy.Expanding, QSizePolicy.Fixed )
  1119. preview_image_bar.setLayout(preview_image_bar_l)
  1120. label_font = QFont('Courier New')
  1121. self.filename_label = QLabel()
  1122. self.filename_label.setFont(label_font)
  1123. self.has_ie_polys_count_label = QLabel()
  1124. status_frame_l = QHBoxLayout()
  1125. status_frame_l.setContentsMargins(0,0,0,0)
  1126. status_frame_l.addWidget ( QLabel(), alignment=Qt.AlignCenter)
  1127. status_frame_l.addWidget (self.filename_label, alignment=Qt.AlignCenter)
  1128. status_frame_l.addWidget (self.has_ie_polys_count_label, alignment=Qt.AlignCenter)
  1129. status_frame = QFrame()
  1130. status_frame.setLayout(status_frame_l)
  1131. main_canvas_l = QVBoxLayout()
  1132. main_canvas_l.setContentsMargins(0,0,0,0)
  1133. main_canvas_l.addWidget (self.canvas)
  1134. main_canvas_l.addWidget (status_frame)
  1135. main_canvas_l.addWidget (preview_image_bar)
  1136. self.main_canvas_frame = QFrame()
  1137. self.main_canvas_frame.setLayout(main_canvas_l)
  1138. self.main_l = QHBoxLayout()
  1139. self.main_l.setContentsMargins(0,0,0,0)
  1140. self.main_l.addWidget (self.main_canvas_frame)
  1141. self.setLayout(self.main_l)
  1142. geometry = self.cfg_dict.get('geometry', None)
  1143. if geometry is not None:
  1144. self.restoreGeometry(geometry)
  1145. else:
  1146. self.move( QPoint(0,0))
  1147. def get_has_ie_polys_count(self):
  1148. return self.has_ie_polys_count
  1149. def set_has_ie_polys_count(self, c):
  1150. self.has_ie_polys_count = c
  1151. self.has_ie_polys_count_label.setText(f"{c} {QStringDB.labeled_tip}")
  1152. def resizeEvent(self, ev):
  1153. if self.loading_frame is not None:
  1154. self.loading_frame.resize( ev.size() )
  1155. if self.help_frame is not None:
  1156. self.help_frame.resize( ev.size() )
  1157. def start(input_dirpath):
  1158. """
  1159. returns exit_code
  1160. """
  1161. io.log_info("Running XSeg editor.")
  1162. if PackedFaceset.path_contains(input_dirpath):
  1163. io.log_info (f'\n{input_dirpath} contains packed faceset! Unpack it first.\n')
  1164. return 1
  1165. root_path = Path(__file__).parent
  1166. cfg_root_path = Path(tempfile.gettempdir())
  1167. QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
  1168. QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
  1169. app = QApplication([])
  1170. app.setApplicationName("XSegEditor")
  1171. app.setStyle('Fusion')
  1172. QFontDatabase.addApplicationFont( str(root_path / 'gfx' / 'fonts' / 'NotoSans-Medium.ttf') )
  1173. app.setFont( QFont('NotoSans'))
  1174. QUIConfig.initialize()
  1175. QStringDB.initialize()
  1176. QIconDB.initialize( root_path / 'gfx' / 'icons' )
  1177. QCursorDB.initialize( root_path / 'gfx' / 'cursors' )
  1178. QImageDB.initialize( root_path / 'gfx' / 'images' )
  1179. app.setWindowIcon(QIconDB.app_icon)
  1180. app.setPalette( QDarkPalette() )
  1181. win = MainWindow( input_dirpath=input_dirpath, cfg_root_path=cfg_root_path)
  1182. win.show()
  1183. win.raise_()
  1184. app.exec_()
  1185. return 0
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...