File size: 54,743 Bytes
f32744f
 
 
 
 
 
 
 
 
 
 
bc99e62
 
 
 
052acac
f32744f
 
 
 
 
 
 
 
 
 
3850225
f32744f
 
 
 
 
 
 
72bcfb4
f32744f
 
 
 
 
 
 
 
 
 
7137ab0
f32744f
 
017b32e
16347be
017b32e
 
bc99e62
16347be
bc99e62
 
5738da0
 
 
f32744f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7137ab0
f32744f
 
7137ab0
f32744f
 
 
5fd20fc
 
 
 
 
 
 
 
ccfaa45
5fd20fc
 
 
f32744f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7137ab0
f32744f
 
 
6942bb2
 
5fd20fc
 
 
 
f32744f
 
 
5fd20fc
f32744f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3f6fc1
f32744f
7137ab0
 
f32744f
 
 
 
 
f858b25
5fd20fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f858b25
 
f32744f
 
 
 
 
 
 
 
 
 
 
f858b25
f32744f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a3f8ca
f32744f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f32744f
7137ab0
 
f32744f
 
 
7137ab0
f32744f
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f32744f
 
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f32744f
 
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
f32744f
 
7137ab0
 
 
 
 
 
 
 
 
f32744f
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f32744f
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f32744f
 
 
7137ab0
 
 
 
 
 
 
 
dca9b7a
7137ab0
 
f32744f
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f32744f
 
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d20dea
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5738da0
 
 
 
 
 
 
 
7137ab0
 
 
 
 
 
 
 
 
 
 
 
 
 
f32744f
8a3f8ca
f32744f
6f52602
017b32e
 
 
 
 
f32744f
 
 
 
 
017b32e
6f52602
f32744f
 
 
 
 
 
 
 
 
 
 
 
 
017b32e
6f52602
017b32e
 
 
6f52602
8a3f8ca
 
 
 
 
6f52602
8a3f8ca
 
 
6f52602
8a3f8ca
 
 
 
017b32e
86c2d29
6fa8a4a
 
f32744f
bc99e62
052acac
7137ab0
 
16347be
0d20dea
16347be
f32744f
 
3850225
 
 
 
 
 
 
39e7a09
3850225
f32744f
 
 
 
 
 
 
 
 
 
 
6fa8a4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178b9c4
 
 
 
6fa8a4a
178b9c4
 
6fa8a4a
178b9c4
 
 
 
 
 
 
 
 
 
 
 
 
 
6fa8a4a
178b9c4
 
 
 
 
 
 
 
 
 
 
6fa8a4a
178b9c4
 
 
6fa8a4a
178b9c4
 
 
 
 
 
 
 
 
 
6fa8a4a
178b9c4
 
6fa8a4a
178b9c4
 
 
 
 
 
 
 
6fa8a4a
178b9c4
 
 
 
 
 
 
 
6fa8a4a
178b9c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bc40b2
178b9c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fa8a4a
f32744f
 
 
 
 
017b32e
 
f32744f
 
 
 
 
 
 
6f52602
f32744f
 
 
 
 
 
 
 
86c2d29
6f52602
f32744f
 
63367de
b87650f
86c2d29
 
54bcad2
86c2d29
54bcad2
f32744f
 
 
 
a13f1f1
54bcad2
 
 
 
 
a13f1f1
54bcad2
 
e3f6fc1
54bcad2
 
 
a13f1f1
 
54bcad2
86c2d29
 
54bcad2
86c2d29
f32744f
 
 
7137ab0
a13f1f1
 
86c2d29
 
 
 
f32744f
 
6f52602
7137ab0
f32744f
f858b25
f32744f
 
 
 
 
7137ab0
f32744f
 
 
 
 
 
 
 
86c2d29
f32744f
7137ab0
 
f32744f
 
86c2d29
017b32e
54bcad2
017b32e
 
 
 
 
 
 
 
 
 
 
 
8791959
86c2d29
 
 
017b32e
 
 
 
7137ab0
 
 
 
017b32e
 
7137ab0
 
86c2d29
f32744f
 
 
 
86c2d29
7137ab0
f32744f
 
 
 
86c2d29
 
f32744f
 
 
 
 
 
f858b25
f32744f
86c2d29
 
 
 
f32744f
 
747e504
f858b25
747e504
f32744f
 
f858b25
f32744f
 
f858b25
7137ab0
f858b25
f32744f
 
 
86c2d29
9193ad6
 
 
 
 
 
 
 
 
 
 
 
 
 
86c2d29
a13f1f1
86c2d29
f32744f
 
86c2d29
8a3f8ca
 
 
 
 
 
a13f1f1
 
86c2d29
f32744f
 
86c2d29
 
 
 
f32744f
bc99e62
f32744f
7137ab0
9981cb8
7137ab0
 
9981cb8
 
7137ab0
 
9981cb8
7137ab0
 
 
 
9981cb8
 
9971630
b932751
9981cb8
 
9971630
9981cb8
 
 
7137ab0
 
 
 
bc99e62
7137ab0
 
 
 
 
 
86c2d29
 
f32744f
 
86c2d29
 
 
 
6fa8a4a
 
 
 
 
 
178b9c4
6fa8a4a
 
 
f32744f
6fa8a4a
 
 
 
 
7137ab0
6fa8a4a
7137ab0
 
 
 
6fa8a4a
 
7137ab0
6fa8a4a
 
7137ab0
 
 
 
6fa8a4a
178b9c4
6fa8a4a
 
 
 
 
178b9c4
6fa8a4a
178b9c4
 
 
 
6fa8a4a
 
178b9c4
6fa8a4a
 
178b9c4
 
6fa8a4a
86c2d29
747e504
f32744f
f858b25
 
 
 
 
 
 
f32744f
 
 
 
 
 
017b32e
 
 
 
 
f32744f
 
 
 
017b32e
 
7137ab0
 
 
 
f32744f
 
 
178b9c4
 
f32744f
 
 
 
 
7137ab0
dca9b7a
f32744f
 
7137ab0
 
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
from __future__ import annotations
import os
import gc
import base64
import io
import time
import shutil
import numpy as np
import torch
import cv2
import ezdxf
from ezdxf.addons.text2path import make_paths_from_str
from ezdxf import path
from ezdxf.addons import text2path
from ezdxf.enums import TextEntityAlignment
from ezdxf.fonts.fonts import FontFace, get_font_face
import gradio as gr
from PIL import Image, ImageEnhance
from pathlib import Path
from typing import List, Union
from ultralytics import YOLOWorld, YOLO
from ultralytics.engine.results import Results
from ultralytics.utils.plotting import save_one_box
from transformers import AutoModelForImageSegmentation
from torchvision import transforms
from scalingtestupdated import calculate_scaling_factor
from shapely.geometry import Polygon, Point, MultiPolygon
from scipy.interpolate import splprep, splev
from scipy.ndimage import gaussian_filter1d
from u2net import U2NETP

# ---------------------
# Create a cache folder for models
# ---------------------
CACHE_DIR = os.path.join(os.path.dirname(__file__), ".cache")
os.makedirs(CACHE_DIR, exist_ok=True)

# ---------------------
# Custom Exceptions
# ---------------------
class DrawerNotDetectedError(Exception):
    """Raised when the drawer cannot be detected in the image"""
    pass

class ReferenceBoxNotDetectedError(Exception):
    """Raised when the Reference coin cannot be detected in the image"""
    pass

class BoundaryOverlapError(Exception):
    """The specified boundary dimensions are too small and overlap with the inner contours.Please provide larger value for boundary length and width."""
    pass

class TextOverlapError(Exception):
    """Raised when the text overlaps with the inner contours (with a margin of 0.75).Please provide larger value for boundary length and width."""
    pass

class FingerCutOverlapError(Exception):
    """There was an overlap with fingercuts... Please try again to generate dxf."""
    pass
# ---------------------
# Global Model Initialization with caching and print statements
# ---------------------
print("Loading YOLOWorld model...")
start_time = time.time()
yolo_model_path = os.path.join(CACHE_DIR, "yolov8x-worldv2.pt")
if not os.path.exists(yolo_model_path):
    print("Caching YOLOWorld model to", yolo_model_path)
    shutil.copy("yolov8x-worldv2.pt", yolo_model_path)
drawer_detector_global = YOLOWorld(yolo_model_path)
drawer_detector_global.set_classes(["box"])
print("YOLOWorld model loaded in {:.2f} seconds".format(time.time() - start_time))

print("Loading YOLO reference model...")
start_time = time.time()
reference_model_path = os.path.join(CACHE_DIR, "coin_det.pt")
if not os.path.exists(reference_model_path):
    print("Caching YOLO reference model to", reference_model_path)
    shutil.copy("coin_det.pt", reference_model_path)
reference_detector_global = YOLO(reference_model_path)
print("YOLO reference model loaded in {:.2f} seconds".format(time.time() - start_time))

print("Loading U²-Net model for reference background removal (U2NETP)...")
start_time = time.time()
u2net_model_path = os.path.join(CACHE_DIR, "u2netp.pth")
if not os.path.exists(u2net_model_path):
    print("Caching U²-Net model to", u2net_model_path)
    shutil.copy("u2netp.pth", u2net_model_path)
u2net_global = U2NETP(3, 1)
u2net_global.load_state_dict(torch.load(u2net_model_path, map_location="cpu"))
device = "cpu"
u2net_global.to(device)
u2net_global.eval()
print("U²-Net model loaded in {:.2f} seconds".format(time.time() - start_time))

print("Loading BiRefNet model...")
start_time = time.time()
birefnet_global = AutoModelForImageSegmentation.from_pretrained(
    "zhengpeng7/BiRefNet", trust_remote_code=True, cache_dir=CACHE_DIR
)
torch.set_float32_matmul_precision("high")
birefnet_global.to(device)
birefnet_global.eval()
print("BiRefNet model loaded in {:.2f} seconds".format(time.time() - start_time))

# Define transform for BiRefNet
transform_image_global = transforms.Compose([
    transforms.Resize((1024, 1024)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])

# ---------------------
# Model Reload Function (if needed)
# ---------------------
def unload_and_reload_models():
    global drawer_detector_global, reference_detector_global, birefnet_global, u2net_global
    print("Reloading models...")
    start_time = time.time()
    del drawer_detector_global, reference_detector_global, birefnet_global, u2net_global
    gc.collect()
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
    gc.collect()
    new_drawer_detector = YOLOWorld(os.path.join(CACHE_DIR, "yolov8x-worldv2.pt"))
    new_drawer_detector.set_classes(["box"])
    new_reference_detector = YOLO(os.path.join(CACHE_DIR, "coin_det.pt"))
    new_birefnet = AutoModelForImageSegmentation.from_pretrained(
        "zhengpeng7/BiRefNet", trust_remote_code=True, cache_dir=CACHE_DIR
    )
    new_birefnet.to(device)
    new_birefnet.eval()
    new_u2net = U2NETP(3, 1)
    new_u2net.load_state_dict(torch.load(os.path.join(CACHE_DIR, "u2netp.pth"), map_location="cpu"))
    new_u2net.to(device)
    new_u2net.eval()
    drawer_detector_global = new_drawer_detector
    reference_detector_global = new_reference_detector
    birefnet_global = new_birefnet
    u2net_global = new_u2net
    print("Models reloaded in {:.2f} seconds".format(time.time() - start_time))

# ---------------------
# Helper Function: resize_img (defined once)
# ---------------------
def resize_img(img: np.ndarray, resize_dim):
    return np.array(Image.fromarray(img).resize(resize_dim))

# ---------------------
# Other Helper Functions for Detection & Processing
# ---------------------
def yolo_detect(image: Union[str, Path, int, Image.Image, list, tuple, np.ndarray, torch.Tensor]) -> np.ndarray:
    t = time.time()
    results: List[Results] = drawer_detector_global.predict(image)
    if not results or len(results) == 0 or len(results[0].boxes) == 0:
        raise DrawerNotDetectedError("Drawer not detected in the image.")
    print("Drawer detection completed in {:.2f} seconds".format(time.time() - t))
    return save_one_box(results[0].cpu().boxes.xyxy, im=results[0].orig_img, save=False)

def detect_reference_square(img: np.ndarray):
    t = time.time()
    res = reference_detector_global.predict(img, conf=0.15)
    if not res or len(res) == 0 or len(res[0].boxes) == 0:
        raise ReferenceBoxNotDetectedError("Reference Coin not detected in the image.")
    print("Reference coin detection completed in {:.2f} seconds".format(time.time() - t))
    return (
        save_one_box(res[0].cpu().boxes.xyxy, res[0].orig_img, save=False),
        res[0].cpu().boxes.xyxy[0]
    )

# Use U2NETP for reference background removal.
def remove_bg_u2netp(image: np.ndarray) -> np.ndarray:
    t = time.time()
    image_pil = Image.fromarray(image)
    transform_u2netp = transforms.Compose([
        transforms.Resize((320, 320)),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ])
    input_tensor = transform_u2netp(image_pil).unsqueeze(0).to("cpu")
    with torch.no_grad():
        outputs = u2net_global(input_tensor)
    pred = outputs[0]
    pred = (pred - pred.min()) / (pred.max() - pred.min() + 1e-8)
    pred_np = pred.squeeze().cpu().numpy()
    pred_np = cv2.resize(pred_np, (image_pil.width, image_pil.height))
    pred_np = (pred_np * 255).astype(np.uint8)
    print("U2NETP background removal completed in {:.2f} seconds".format(time.time() - t))
    return pred_np

# Use BiRefNet for main object background removal.
def remove_bg(image: np.ndarray) -> np.ndarray:
    t = time.time()
    image_pil = Image.fromarray(image)
    input_images = transform_image_global(image_pil).unsqueeze(0).to("cpu")
    with torch.no_grad():
        preds = birefnet_global(input_images)[-1].sigmoid().cpu()
    pred = preds[0].squeeze()
    pred_pil = transforms.ToPILImage()(pred)
    scale_ratio = 1024 / max(image_pil.size)
    scaled_size = (int(image_pil.size[0] * scale_ratio), int(image_pil.size[1] * scale_ratio))
    result = np.array(pred_pil.resize(scaled_size))
    print("BiRefNet background removal completed in {:.2f} seconds".format(time.time() - t))
    return result

def make_square(img: np.ndarray):
    height, width = img.shape[:2]
    max_dim = max(height, width)
    pad_height = (max_dim - height) // 2
    pad_width = (max_dim - width) // 2
    pad_height_extra = max_dim - height - 2 * pad_height
    pad_width_extra = max_dim - width - 2 * pad_width
    if len(img.shape) == 3:
        padded = np.pad(img, ((pad_height, pad_height + pad_height_extra),
                              (pad_width, pad_width + pad_width_extra),
                              (0, 0)), mode="edge")
    else:
        padded = np.pad(img, ((pad_height, pad_height + pad_height_extra),
                              (pad_width, pad_width + pad_width_extra)), mode="edge")
    return padded

def shrink_bbox(image: np.ndarray, shrink_factor: float):
    height, width = image.shape[:2]
    center_x, center_y = width // 2, height // 2
    new_width = int(width * shrink_factor)
    new_height = int(height * shrink_factor)
    x1 = max(center_x - new_width // 2, 0)
    y1 = max(center_y - new_height // 2, 0)
    x2 = min(center_x + new_width // 2, width)
    y2 = min(center_y + new_height // 2, height)
    return image[y1:y2, x1:x2]

def exclude_scaling_box(image: np.ndarray, bbox: np.ndarray, orig_size: tuple, processed_size: tuple, expansion_factor: float = 1.2) -> np.ndarray:
    x_min, y_min, x_max, y_max = map(int, bbox)
    scale_x = processed_size[1] / orig_size[1]
    scale_y = processed_size[0] / orig_size[0]
    x_min = int(x_min * scale_x)
    x_max = int(x_max * scale_x)
    y_min = int(y_min * scale_y)
    y_max = int(y_max * scale_y)
    box_width = x_max - x_min
    box_height = y_max - y_min
    expanded_x_min = max(0, int(x_min - (expansion_factor - 1) * box_width / 2))
    expanded_x_max = min(image.shape[1], int(x_max + (expansion_factor - 1) * box_width / 2))
    expanded_y_min = max(0, int(y_min - (expansion_factor - 1) * box_height / 2))
    expanded_y_max = min(image.shape[0], int(y_max + (expansion_factor - 1) * box_height / 2))
    image[expanded_y_min:expanded_y_max, expanded_x_min:expanded_x_max] = 0
    return image

import logging
import time
import signal
import numpy as np
import cv2
from scipy.interpolate import splprep, splev
from scipy.ndimage import gaussian_filter1d
from shapely.geometry import Point, Polygon
import random
import ezdxf
import functools

# Set up logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Custom TimeoutError class
class TimeoutReachedError(Exception):
    pass

# Timeout context manager
class TimeoutContext:
    def __init__(self, seconds):
        self.seconds = seconds
        self.original_handler = None
        
    def timeout_handler(self, signum, frame):
        raise TimeoutReachedError(f"Function timed out after {self.seconds} seconds")
        
    def __enter__(self):
        if hasattr(signal, 'SIGALRM'):  # Unix-like systems
            self.original_handler = signal.getsignal(signal.SIGALRM)
            signal.signal(signal.SIGALRM, self.timeout_handler)
            signal.alarm(self.seconds)
        self.start_time = time.time()
        return self
        
    def __exit__(self, exc_type, exc_val, exc_tb):
        if hasattr(signal, 'SIGALRM'):  # Unix-like systems
            signal.alarm(0)
            signal.signal(signal.SIGALRM, self.original_handler)
        if exc_type is TimeoutReachedError:
            logger.warning(f"Timeout reached: {exc_val}")
            return True  # Suppress the exception
        return False

def resample_contour(contour):
    logger.info(f"Starting resample_contour with contour of shape {contour.shape}")
    
    num_points = 1000
    smoothing_factor = 5
    spline_degree = 3
    
    if len(contour) < spline_degree + 1:
        error_msg = f"Contour must have at least {spline_degree + 1} points, but has {len(contour)} points."
        logger.error(error_msg)
        raise ValueError(error_msg)
        
    try:
        contour = contour[:, 0, :]
        logger.debug(f"Reshaped contour to shape {contour.shape}")
        
        tck, _ = splprep([contour[:, 0], contour[:, 1]], s=smoothing_factor)
        logger.debug("Generated spline parameters")
        
        u = np.linspace(0, 1, num_points)
        resampled_points = splev(u, tck)
        logger.debug(f"Resampled to {num_points} points")
        
        smoothed_x = gaussian_filter1d(resampled_points[0], sigma=1)
        smoothed_y = gaussian_filter1d(resampled_points[1], sigma=1)
        
        result = np.array([smoothed_x, smoothed_y]).T
        logger.info(f"Completed resample_contour with result shape {result.shape}")
        return result
    except Exception as e:
        logger.error(f"Error in resample_contour: {e}")
        raise

def extract_outlines(binary_image: np.ndarray) -> (np.ndarray, list):
    logger.info(f"Starting extract_outlines with image shape {binary_image.shape}")
    
    try:
        contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        logger.debug(f"Found {len(contours)} contours")
        
        outline_image = np.zeros_like(binary_image)
        cv2.drawContours(outline_image, contours, -1, (255), thickness=2)
        
        result_image = cv2.bitwise_not(outline_image)
        logger.info(f"Completed extract_outlines with {len(contours)} contours")
        return result_image, contours
    except Exception as e:
        logger.error(f"Error in extract_outlines: {e}")
        raise

def union_tool_and_circle(tool_polygon: Polygon, center_inch, circle_diameter=1.0):
    logger.info(f"Starting union_tool_and_circle with center at {center_inch}")
    
    try:
        radius = circle_diameter / 2.0
        circle_poly = Point(center_inch).buffer(radius, resolution=64)
        logger.debug(f"Created circle with radius {radius} at {center_inch}")
        
        union_poly = tool_polygon.union(circle_poly)
        logger.info(f"Completed union_tool_and_circle, result area: {union_poly.area}")
        return union_poly
    except Exception as e:
        logger.error(f"Error in union_tool_and_circle: {e}")
        raise

def build_tool_polygon(points_inch):
    logger.info(f"Starting build_tool_polygon with {len(points_inch)} points")
    
    try:
        polygon = Polygon(points_inch)
        logger.info(f"Completed build_tool_polygon, polygon area: {polygon.area}")
        return polygon
    except Exception as e:
        logger.error(f"Error in build_tool_polygon: {e}")
        raise

def polygon_to_exterior_coords(poly):
    logger.info(f"Starting polygon_to_exterior_coords with polygon type {poly.geom_type}")
    
    try:
        # Handle GeometryCollection case specifically
        if poly.geom_type == "GeometryCollection":
            logger.warning("Converting GeometryCollection to Polygon")
            # Find the largest geometry in the collection that has an exterior
            largest_area = 0
            largest_geom = None
            for geom in poly.geoms:
                if hasattr(geom, 'area') and geom.area > largest_area:
                    if hasattr(geom, 'exterior') or geom.geom_type == "MultiPolygon":
                        largest_area = geom.area
                        largest_geom = geom
            
            if largest_geom is None:
                logger.warning("No valid geometry found in GeometryCollection")
                return []
                
            poly = largest_geom
        
        if poly.geom_type == "MultiPolygon":
            logger.debug("Converting MultiPolygon to single Polygon")
            biggest = max(poly.geoms, key=lambda g: g.area)
            poly = biggest
            
        if not hasattr(poly, 'exterior') or poly.exterior is None:
            logger.warning("Polygon has no exterior")
            return []
            
        coords = list(poly.exterior.coords)
        logger.info(f"Completed polygon_to_exterior_coords with {len(coords)} coordinates")
        return coords
    except Exception as e:
        logger.error(f"Error in polygon_to_exterior_coords: {e}")
        # Return empty list as fallback
        return []
    
def place_finger_cut_adjusted(
    tool_polygon: Polygon,
    points_inch: list,
    existing_centers: list,
    all_polygons: list,
    circle_diameter: float = 1.0,
    min_gap: float = 0.5,
    max_attempts: int = 100
) -> (Polygon, tuple):
    logger.info(f"Starting place_finger_cut_adjusted with {len(points_inch)} points")
    
    # Define fallback function for timeout case
    def fallback_solution():
        logger.warning("Using fallback approach for finger cut placement")
        candidate_center = points_inch[len(points_inch) // 2]
        radius = circle_diameter / 2.0
        candidate_circle = Point(candidate_center).buffer(radius, resolution=64)
        
        try:
            union_poly = tool_polygon.union(candidate_circle)
        except Exception as e:
            logger.warning(f"Fallback union failed, using buffer trick: {e}")
            union_poly = tool_polygon.buffer(0).union(candidate_circle.buffer(0))
            
        existing_centers.append(candidate_center)
        logger.info(f"Used fallback finger cut at center {candidate_center}")
        return union_poly, candidate_center

    needed_center_distance = circle_diameter + min_gap
    radius = circle_diameter / 2.0
    
    # Limit points to prevent timeout - use a subset for efficient processing
    if len(points_inch) > 100:
        logger.info(f"Limiting points from {len(points_inch)} to 100 for efficiency")
        step = len(points_inch) // 100
        points_inch = points_inch[::step]
    
    # Randomize candidate points order
    indices = list(range(len(points_inch)))
    random.shuffle(indices)
    logger.debug(f"Shuffled {len(indices)} point indices")

    # Use a non-blocking timeout approach with explicit time checks
    start_time = time.time()
    timeout_seconds = 5
    attempts = 0
    
    try:
        while attempts < max_attempts:
            # Check if we're approaching the timeout
            current_time = time.time()
            if current_time - start_time > timeout_seconds - 0.1:  # Leave 0.1s margin
                logger.warning(f"Approaching timeout after {attempts} attempts")
                return fallback_solution()
                
            # Process a batch of points to improve efficiency
            for i in indices:
                # Check timeout frequently
                if time.time() - start_time > timeout_seconds - 0.05:
                    logger.warning("Timeout during point processing")
                    return fallback_solution()
                    
                cx, cy = points_inch[i]
                # Reduce the number of adjustments to speed up processing
                for dx, dy in [(0,0), (-0.2,0), (0.2,0), (0,0.2), (0,-0.2)]:
                    candidate_center = (cx + dx, cy + dy)
                    
                    # Quick check for existing centers distance
                    if any(np.hypot(candidate_center[0] - ex, candidate_center[1] - ey) < needed_center_distance
                           for ex, ey in existing_centers):
                        continue

                    # Create candidate circle
                    candidate_circle = Point(candidate_center).buffer(radius, resolution=32)  # Reduced resolution
                    
                    # Quick geometric checks
                    if tool_polygon.contains(candidate_circle) or not candidate_circle.intersects(tool_polygon):
                        continue
                    
                    # Check intersection area - use simplified geometry for speed
                    try:
                        inter_area = candidate_circle.intersection(tool_polygon).area
                        if inter_area <= 0 or inter_area >= candidate_circle.area:
                            continue
                    except Exception:
                        continue

                    # Quick distance check to other polygons
                    too_close = False
                    for other_poly in all_polygons:
                        if other_poly.equals(tool_polygon):
                            continue
                        if other_poly.distance(candidate_circle) < min_gap:
                            too_close = True
                            break
                    if too_close:
                        continue
                    
                    # Attempt the union
                    try:
                        union_poly = tool_polygon.union(candidate_circle)
                        # Check if we got a multi-polygon when we don't want one
                        if union_poly.geom_type == "MultiPolygon" and len(union_poly.geoms) > 1:
                            continue
                        # Check if the union actually changed anything
                        if union_poly.equals(tool_polygon):
                            continue
                    except Exception:
                        continue
                    
                    # We found a valid candidate
                    existing_centers.append(candidate_center)
                    logger.info(f"Completed place_finger_cut_adjusted successfully at center {candidate_center}")
                    return union_poly, candidate_center
            
            attempts += 1
            # If we've made several attempts and are running out of time, use fallback
            if attempts >= max_attempts // 2 and (time.time() - start_time) > timeout_seconds * 0.8:
                logger.warning(f"Approaching timeout after {attempts} attempts")
                return fallback_solution()
                
            logger.debug(f"Completed attempt {attempts}/{max_attempts}")
            
        # If we reached max attempts without finding a solution
        logger.warning(f"No suitable finger cut found after {max_attempts} attempts, using fallback")
        return fallback_solution()
            
    except Exception as e:
        logger.error(f"Error in place_finger_cut_adjusted: {e}")
        return fallback_solution()

def save_dxf_spline(offset_value,inflated_contours, scaling_factor, height, finger_clearance=False):
    logger.info(f"Starting save_dxf_spline with {len(inflated_contours)} contours")
    
    degree = 3
    closed = True
    
    try:
        doc = ezdxf.new(units=0)
        doc.units = ezdxf.units.IN
        doc.header["$INSUNITS"] = ezdxf.units.IN
        msp = doc.modelspace()
        
        finger_cut_centers = []
        final_polygons_inch = []
        
        for idx, contour in enumerate(inflated_contours):
            logger.debug(f"Processing contour {idx+1}/{len(inflated_contours)}")
            
            try:
                resampled_contour = resample_contour(contour)
                points_inch = [(x * scaling_factor, (height - y) * scaling_factor) for x, y in resampled_contour]
                
                if len(points_inch) < 3:
                    logger.warning(f"Skipping contour {idx}: insufficient points ({len(points_inch)})")
                    continue
                    
                if np.linalg.norm(np.array(points_inch[0]) - np.array(points_inch[-1])) > 1e-2:
                    logger.debug("Closing contour by adding first point to end")
                    points_inch.append(points_inch[0])
                    
                tool_polygon = build_tool_polygon(points_inch)
                
                if finger_clearance:
                    logger.debug("Applying finger clearance")
                    try:
                        # Use a hard 5-second timeout for the entire finger cut operation
                        start_time = time.time()
                        union_poly, center = place_finger_cut_adjusted(
                            tool_polygon, 
                            points_inch, 
                            finger_cut_centers, 
                            final_polygons_inch, 
                            circle_diameter=1.0, 
                            min_gap=(0.5+offset_value), 
                            max_attempts=100
                        )
                        
                        # Check if we exceeded the timeout anyway
                        if time.time() - start_time > 5:
                            logger.warning(f"Finger cut took too long for contour {idx} ({time.time() - start_time:.2f}s)")
                        
                        if union_poly is not None:
                            tool_polygon = union_poly
                            logger.debug(f"Applied finger cut at {center}")
                    except Exception as e:
                        logger.warning(f"Finger cut failed for contour {idx}: {e}, using original polygon")
                        
                exterior_coords = polygon_to_exterior_coords(tool_polygon)
                
                if len(exterior_coords) < 3:
                    logger.warning(f"Skipping contour {idx}: insufficient exterior points ({len(exterior_coords)})")
                    continue
                for existing_poly in final_polygons_inch:
                    if tool_polygon.intersects(existing_poly):
                        # Check if the intersection is more than just touching points
                        intersection = tool_polygon.intersection(existing_poly)
                        # If the intersection has ANY area (not just points touching)
                        if intersection.area > 0:  # Zero tolerance for overlap
                            logger.error(f"Polygon {idx} overlaps with an existing polygon")
                            raise FingerCutOverlapError("There was an overlap with fingercuts... Please try again to generate dxf.")
                    
                msp.add_spline(exterior_coords, degree=degree, dxfattribs={"layer": "TOOLS"})
                final_polygons_inch.append(tool_polygon)
                logger.debug(f"Added spline for contour {idx}")
                
            except ValueError as e:
                logger.warning(f"Skipping contour {idx}: {e}")
                
        logger.info(f"Completed save_dxf_spline with {len(final_polygons_inch)} successful polygons")
        return doc, final_polygons_inch
        
    except Exception as e:
        logger.error(f"Error in save_dxf_spline: {e}")
        raise

def add_rectangular_boundary(doc, polygons_inch, boundary_length, boundary_width, offset_unit, annotation_text="", image_height_in=None, image_width_in=None):
    msp = doc.modelspace()
    # Convert from mm if necessary
    if offset_unit.lower() == "mm":
        if boundary_length < 50:
            boundary_length = boundary_length * 25.4
        if boundary_width < 50:
            boundary_width = boundary_width * 25.4
        boundary_length_in = boundary_length / 25.4
        boundary_width_in = boundary_width / 25.4
    else:
        boundary_length_in = boundary_length
        boundary_width_in = boundary_width

    # Compute bounding box of inner contours
    min_x = float("inf")
    min_y = float("inf")
    max_x = -float("inf")
    max_y = -float("inf")
    for poly in polygons_inch:
        b = poly.bounds
        min_x = min(min_x, b[0])
        min_y = min(min_y, b[1])
        max_x = max(max_x, b[2])
        max_y = max(max_y, b[3])
    if min_x == float("inf"):
        print("No tool polygons found, skipping boundary.")
        return None

    # Compute inner bounding box dimensions
    inner_width = max_x - min_x
    inner_length = max_y - min_y

    # Set clearance margins
    clearance_side = 0.25  # left/right clearance
    clearance_tb = 0.25    # top/bottom clearance
    if annotation_text.strip():
        clearance_tb = 0.75

    # Calculate center of inner contours
    center_x = (min_x + max_x) / 2
    center_y = (min_y + max_y) / 2

    # Draw rectangle centered at (center_x, center_y)
    left = center_x - boundary_width_in / 2
    right = center_x + boundary_width_in / 2
    bottom = center_y - boundary_length_in / 2
    top = center_y + boundary_length_in / 2

    rect_coords = [(left, bottom), (right, bottom), (right, top), (left, top), (left, bottom)]
    from shapely.geometry import Polygon 
    boundary_polygon = Polygon(rect_coords)
    msp.add_lwpolyline(rect_coords, close=True, dxfattribs={"layer": "BOUNDARY"})
    
    text_top = boundary_polygon.bounds[1] + 1
    too_small = boundary_width_in < inner_width + 2 * clearance_side or boundary_length_in < inner_length + 2 * clearance_tb
    if too_small:
        raise BoundaryOverlapError("Error: The specified boundary dimensions are too small and overlap with the inner contours. Please provide larger value for boundary length and width.")
    if annotation_text.strip() and text_top > min_y - 1:
        raise TextOverlapError("Error: The text is too close to the inner contours. Please provide larger value for boundary length and width.")
    return boundary_polygon

def draw_polygons_inch(polygons_inch, image_rgb, scaling_factor, image_height, color=(0,0,255), thickness=2):
    for poly in polygons_inch:
        if poly.geom_type == "MultiPolygon":
            for subpoly in poly.geoms:
                draw_single_polygon(subpoly, image_rgb, scaling_factor, image_height, color, thickness)
        else:
            draw_single_polygon(poly, image_rgb, scaling_factor, image_height, color, thickness)

def draw_single_polygon(poly, image_rgb, scaling_factor, image_height, color=(0,0,255), thickness=2):
    ext = list(poly.exterior.coords)
    if len(ext) < 3:
        return
    pts_px = []
    for (x_in, y_in) in ext:
        px = int(x_in / scaling_factor)
        py = int(image_height - (y_in / scaling_factor))
        pts_px.append([px, py])
    pts_px = np.array(pts_px, dtype=np.int32)
    cv2.polylines(image_rgb, [pts_px], isClosed=True, color=color, thickness=thickness, lineType=cv2.LINE_AA)

# def draw_and_pad(polygons_inch, scaling_factor,boundary_polygon, padding=50, 
#                  color=(0,0,255), thickness=2):
#     """
#     - polygons_inch: list of Shapely Polygons in inch units (already including boundary).
#     - scaling_factor: inches per pixel.
#     - padding: padding in pixels.
#     """
#     all_x = []
#     all_y = []
#     pixel_polys = []
    
#     # 1) Convert to pixel coords and collect bounds
#     for poly in polygons_inch:
#         coords = list(poly.exterior.coords)
#         pts = []
#         for x_in, y_in in coords:
#             px = int(round(x_in / scaling_factor))
#             py = int(round(y_in / scaling_factor))
#             pts.append([px, py])
#             all_x.append(px)
#             all_y.append(py)
#         pixel_polys.append(np.array(pts, dtype=np.int32))
    
#     # 2) Compute canvas size
    
#     min_x, max_x = min(all_x), max(all_x)
#     min_y, max_y = min(all_y), max(all_y)
#     width  = max_x - min_x + 1
#     height = max_y - min_y + 1
    
#     # 3) Create blank white canvas
#     canvas = 255 * np.ones((height, width, 3), dtype=np.uint8)
    
#     # 4) Draw each polygon, flipping y within the local box
#     for pts in pixel_polys:
#         # Offset so min corner is (0,0)
#         pts_off = pts - np.array([[min_x, min_y]])
#         # Flip y: new_y = height-1 - old_y
#         pts_off[:,1] = (height - 1) - pts_off[:,1]
#         cv2.polylines(canvas, [pts_off], isClosed=True, 
#                       color=color, thickness=thickness, lineType=cv2.LINE_AA)
    
#     # 5) Pad the canvas

#         padded = cv2.copyMakeBorder(
#             canvas,
#             top=padding, bottom=padding,
#             left=padding, right=padding,
#             borderType=cv2.BORDER_CONSTANT,
#             value=[255,255,255]
#         )
#     return padded

# import numpy as np
# import cv2

# def draw_and_pad(polygons_inch, scaling_factor, boundary_polygon, padding=50,
#                  color=(0,0,255), thickness=2):
#     """
#     - polygons_inch: list of Shapely Polygons in inch units.
#     - scaling_factor: inches per pixel.
#     - boundary_polygon: the Shapely boundary polygon, or None.
#     - padding: base padding in pixels.
#     """
#     all_x, all_y = [], []
#     pixel_polys = []
    
#     # 1) Convert to pixel coords and collect bounds
#     for poly in polygons_inch:
#         coords = list(poly.exterior.coords)
#         pts = []
#         for x_in, y_in in coords:
#             px = int(round(x_in / scaling_factor))
#             py = int(round(y_in / scaling_factor))
#             pts.append([px, py])
#             all_x.append(px)
#             all_y.append(py)
#         pixel_polys.append(np.array(pts, dtype=np.int32))
    
#     # 2) Compute canvas size
#     min_x, max_x = min(all_x), max(all_x)
#     min_y, max_y = min(all_y), max(all_y)
#     width  = max_x - min_x + 1
#     height = max_y - min_y + 1
    
#     # 3) Create blank white canvas
#     canvas = 255 * np.ones((height, width, 3), dtype=np.uint8)
    
#     # 4) Draw each polygon, flipping y within the local box
#     for pts in pixel_polys:
#         pts_off = pts - np.array([[min_x, min_y]])
#         pts_off[:,1] = (height - 1) - pts_off[:,1]
#         cv2.polylines(canvas, [pts_off], isClosed=True, 
#                       color=color, thickness=thickness, lineType=cv2.LINE_AA)
    
#     # 5) Decide padding amounts
#     if boundary_polygon is not None:
#         top = bottom = left = right = padding
#     else:
#         # Double the padding if there's no boundary, to avoid clipping
#         top = bottom = left = right = padding * 2

#     # 6) Pad the canvas
#     padded = cv2.copyMakeBorder(
#         canvas,
#         top=top, bottom=bottom,
#         left=left, right=right,
#         borderType=cv2.BORDER_CONSTANT,
#         value=[255,255,255]
#     )
#     return padded

import numpy as np
import cv2

# def draw_and_pad(polygons_inch, scaling_factor, boundary_polygon, padding=50, 
#                  color=(0, 0, 255), thickness=2):
#     """
#     Draws Shapely Polygons (in inch units) on a white canvas.
    
#     When boundary_polygon is None, the computed bounds are expanded by the padding value 
#     so that the drawn contours are not clipped at the edges after adding the final padding.
    
#     Arguments:
#         polygons_inch: list of Shapely Polygons in inch units (already including boundary).
#         scaling_factor: inches per pixel.
#         boundary_polygon: the Shapely boundary polygon, or None.
#         padding: padding in pixels.
#         color: color of the drawn polylines (in BGR format).
#         thickness: line thickness.

#     Returns:
#         padded: an image (numpy array) of the drawn polygons with an external white border.
#     """
#     all_x = []
#     all_y = []
#     pixel_polys = []
    
#     # 1) Convert each polygon to pixel coordinates and compute overall bounds.
#     for poly in polygons_inch:
#         coords = list(poly.exterior.coords)
#         pts = []
#         for x_in, y_in in coords:
#             px = int(round(x_in / scaling_factor))
#             py = int(round(y_in / scaling_factor))
#             pts.append([px, py])
#             all_x.append(px)
#             all_y.append(py)
#         pixel_polys.append(np.array(pts, dtype=np.int32))
    
#     # 2) Compute the basic canvas size from the polygon bounds.
#     min_x, max_x = min(all_x), max(all_x)
#     min_y, max_y = min(all_y), max(all_y)
    
#     # If no boundary polygon is provided, expand the bounds to add margin
#     # so that later when we pad externally, the contours do not get clipped.
#     if boundary_polygon is None:
#         min_x -= padding
#         max_x += padding
#         min_y -= padding
#         max_y += padding

#     width  = max_x - min_x + 1
#     height = max_y - min_y + 1
    
#     # 3) Create a blank white canvas.
#     canvas = 255 * np.ones((height, width, 3), dtype=np.uint8)
    
#     # 4) Draw each polygon, flipping the y-coordinates to match image coordinates.
#     for pts in pixel_polys:
#         # Offset so the minimum corner becomes (0,0) on canvas.
#         pts_off = pts - np.array([[min_x, min_y]])
#         # Flip y: image coordinates have (0,0) at the top-left.
#         pts_off[:, 1] = (height - 1) - pts_off[:, 1]
#         cv2.polylines(canvas, [pts_off], isClosed=True, 
#                       color=color, thickness=thickness, lineType=cv2.LINE_AA)
    
#     # 5) Finally, add external padding on all sides.
#     padded = cv2.copyMakeBorder(
#         canvas,
#         top=padding, bottom=padding,
#         left=padding, right=padding,
#         borderType=cv2.BORDER_CONSTANT,
#         value=[255, 255, 255]
#     )
    
#     return padded

import numpy as np
import cv2
from shapely.geometry import Polygon

import numpy as np
import cv2
from shapely.geometry import Polygon

def draw_and_pad(polygons_inch,
                               scaling_factor,      # inches per pixel
                               boundary_polygon=None,
                               max_res=1024,
                               simplify_tol_px=1.0,
                               padding_px=50,
                               color=(0,0,255),
                               thickness=2):
    # 1) Simplify & collect raw coords in inches
    all_x, all_y = [], []
    simple_polys = []
    for poly in polygons_inch:
        tol_in = simplify_tol_px * scaling_factor / max_res
        simp = poly.simplify(tolerance=tol_in, preserve_topology=True)
        coords = np.array(simp.exterior.coords)  # (N,2) in inches
        all_x.extend(coords[:,0])
        all_y.extend(coords[:,1])
        simple_polys.append(coords)

    # 2) Compute full‑res pixel extents
    min_x_in, max_x_in = min(all_x), max(all_x)
    min_y_in, max_y_in = min(all_y), max(all_y)
    w_in = (max_x_in - min_x_in) if boundary_polygon is None else (max_x_in - min_x_in)
    h_in = (max_y_in - min_y_in) if boundary_polygon is None else (max_y_in - min_y_in)
    full_w_px = np.ceil(w_in / scaling_factor)
    full_h_px = np.ceil(h_in / scaling_factor)

    # 3) Compute preview scale ≤1 so dims ≤ max_res
    scale = min(max_res / full_w_px, max_res / full_h_px, 1.0)

    # 4) Compute preview dims & allocate _fully‐padded_ canvas
    W = int(np.ceil(full_w_px * scale))
    H = int(np.ceil(full_h_px * scale))
    PW, PH = W + 2*padding_px, H + 2*padding_px
    canvas = 255 * np.ones((PH, PW, 3), dtype=np.uint8)

    # Precompute offsets (in preview px) of the “world origin”
    off_x = int(np.floor(min_x_in / scaling_factor * scale))
    off_y = int(np.floor(min_y_in / scaling_factor * scale))

    # 5) Draw each polygon, now fully inside the padded canvas
    for coords in simple_polys:
        # inch→preview‐px transform
        pts = ((coords / scaling_factor) * scale).round().astype(int)
        # shift by both the minimum and the padding:
        pts[:,0] = pts[:,0] - off_x + padding_px
        pts[:,1] = pts[:,1] - off_y + padding_px
        # flip Y into image coords
        pts[:,1] = PH - 1 - pts[:,1]
        cv2.polylines(canvas,
                      [pts],
                      isClosed=True,
                      color=color,
                      thickness=thickness,
                      lineType=cv2.LINE_AA)

    return canvas, scale, off_y, padding_px, PH



# ---------------------
# Main Predict Function with Finger Cut Clearance, Boundary Box, Annotation and Sharpness Enhancement
# ---------------------
def predict(
    image: Union[str, bytes, np.ndarray],
    offset_value: float,
    offset_unit: str,         # "mm" or "inches"
    finger_clearance: str,    # "Yes" or "No"
    add_boundary: str,        # "Yes" or "No"
    boundary_length: float,
    boundary_width: float,
    annotation_text: str
):
    overall_start = time.time()
    # Convert image to NumPy array if needed
    if isinstance(image, str):
        if os.path.exists(image):
            image = np.array(Image.open(image).convert("RGB"))
        else:
            try:
                image = np.array(Image.open(io.BytesIO(base64.b64decode(image))).convert("RGB"))
            except Exception:
                raise ValueError("Invalid base64 image data")

    # Apply brightness and sharpness enhancement
    if isinstance(image, np.ndarray):
        pil_image = Image.fromarray(image)
        enhanced_image = ImageEnhance.Sharpness(pil_image).enhance(1.5)
        image = np.array(enhanced_image)

    # ---------------------
    # 1) Detect the drawer with YOLOWorld (or use original image if not detected)
    # ---------------------
    drawer_detected = True
    try:
        t = time.time()
        drawer_img = yolo_detect(image)
        print("Drawer detection completed in {:.2f} seconds".format(time.time() - t))
    except DrawerNotDetectedError as e:
        print(f"Drawer not detected: {e}, using original image.")
        drawer_detected = False
        drawer_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

    # Process the image (either cropped drawer or original)
    t = time.time()
    if drawer_detected:
        # For detected drawers: shrink and square
        shrunked_img = make_square(shrink_bbox(drawer_img, 0.95))
    else:
        # For non-drawer images: keep original dimensions
        shrunked_img = drawer_img  # Already in BGR format from above
    del drawer_img
    gc.collect()
    print("Image processing completed in {:.2f} seconds".format(time.time() - t))

    # ---------------------
    # 2) Detect the reference box with YOLO (now works on either cropped or original image)
    # ---------------------
    try:
        t = time.time()
        reference_obj_img, scaling_box_coords = detect_reference_square(shrunked_img)
        print("Reference coin detection completed in {:.2f} seconds".format(time.time() - t))
    except ReferenceBoxNotDetectedError as e:
        return None, None, None, None, f"Error: {str(e)}"

    # ---------------------
    # 3) Remove background of the reference box to compute scaling factor
    # ---------------------
    t = time.time()
    reference_obj_img = make_square(reference_obj_img)
    reference_square_mask = remove_bg_u2netp(reference_obj_img)
    reference_square_mask= resize_img(reference_square_mask,(reference_obj_img.shape[1],reference_obj_img.shape[0]))
    print("Reference image processing completed in {:.2f} seconds".format(time.time() - t))

    t = time.time()
    try:
        cv2.imwrite("mask.jpg", cv2.cvtColor(reference_obj_img, cv2.COLOR_RGB2GRAY))
        scaling_factor = calculate_scaling_factor(
            target_image=reference_square_mask,
            reference_obj_size_mm=0.955,
            feature_detector="ORB",
        )
    except ZeroDivisionError:
        scaling_factor = None
        print("Error calculating scaling factor: Division by zero")
    except Exception as e:
        scaling_factor = None
        print(f"Error calculating scaling factor: {e}")

    if scaling_factor is None or scaling_factor == 0:
        scaling_factor = 0.7
        print("Using default scaling factor of 0.7 due to calculation error")
    gc.collect()
    print("Scaling factor determined: {}".format(scaling_factor))

    # ---------------------
    # 4) Optional boundary dimension checks (now without size limits)
    # ---------------------
    if add_boundary.lower() == "yes":
        if offset_unit.lower() == "mm":
            if boundary_length < 50:
                boundary_length = boundary_length * 25.4
            if boundary_width < 50:
                boundary_width = boundary_width * 25.4
            boundary_length_in = boundary_length / 25.4
            boundary_width_in = boundary_width / 25.4
        else:
            boundary_length_in = boundary_length
            boundary_width_in = boundary_width
            
    # ---------------------
    # 5) Remove background from the shrunked drawer image (main objects)
    # ---------------------
    if offset_unit.lower() == "mm":
        if offset_value < 1:
            offset_value = offset_value * 25.4
        offset_inches = offset_value / 25.4
        if offset_value==0:
            offset_value = offset_value * 25.4
            offset_inches = offset_value / 25.4
            offset_inches+=0.005
    else:
        offset_inches = offset_value
        if offset_inches==0:
            offset_inches+=0.005

    t = time.time()
    orig_size = shrunked_img.shape[:2]
    objects_mask = remove_bg(shrunked_img)
    processed_size = objects_mask.shape[:2]

    objects_mask = exclude_scaling_box(objects_mask, scaling_box_coords, orig_size, processed_size, expansion_factor=1.2)
    objects_mask = resize_img(objects_mask, (shrunked_img.shape[1], shrunked_img.shape[0]))
    del scaling_box_coords
    gc.collect()
    print("Object masking completed in {:.2f} seconds".format(time.time() - t))

    # Dilate mask by offset_pixels
    t = time.time()
    offset_pixels = (offset_inches / scaling_factor) * 2 + 1 if scaling_factor != 0 else 1
    dilated_mask = cv2.dilate(objects_mask, np.ones((int(offset_pixels), int(offset_pixels)), np.uint8))
    del objects_mask
    gc.collect()
    print("Mask dilation completed in {:.2f} seconds".format(time.time() - t))

    Image.fromarray(dilated_mask).save("./outputs/scaled_mask_new.jpg")

    # ---------------------
    # 6) Extract outlines from the mask and convert them to DXF splines
    # ---------------------
    t = time.time()
    outlines, contours = extract_outlines(dilated_mask)
    print("Outline extraction completed in {:.2f} seconds".format(time.time() - t))

    output_img = shrunked_img.copy()
    del shrunked_img
    gc.collect()

    t = time.time()
    use_finger_clearance = True if finger_clearance.lower() == "yes" else False
    doc, final_polygons_inch = save_dxf_spline(
        offset_inches,contours, scaling_factor, processed_size[0], finger_clearance=use_finger_clearance
    )
    del contours
    gc.collect()
    print("DXF generation completed in {:.2f} seconds".format(time.time() - t))

    # ---------------------
    # Compute bounding box of inner tool contours BEFORE adding optional boundary
    # ---------------------
    inner_min_x = float("inf")
    inner_min_y = float("inf")
    inner_max_x = -float("inf")
    inner_max_y = -float("inf")
    for poly in final_polygons_inch:
        b = poly.bounds
        inner_min_x = min(inner_min_x, b[0])
        inner_min_y = min(inner_min_y, b[1])
        inner_max_x = max(inner_max_x, b[2])
        inner_max_y = max(inner_max_y, b[3])

    # ---------------------
    # 7) Add optional rectangular boundary
    # ---------------------
    boundary_polygon = None
    if add_boundary.lower() == "yes":
        boundary_polygon = add_rectangular_boundary(
            doc,
            final_polygons_inch,
            boundary_length,
            boundary_width,
            offset_unit,
            annotation_text,
            image_height_in=output_img.shape[0] * scaling_factor,
            image_width_in=output_img.shape[1] * scaling_factor
        )
        if boundary_polygon is not None:
            final_polygons_inch.append(boundary_polygon)

    # ---------------------
    # 8) Add annotation text (if provided) in the DXF
    # ---------------------
    msp = doc.modelspace()
    
    if annotation_text.strip():
        if boundary_polygon is not None:
            text_height_dxf = 0.75
            text_y_dxf = boundary_polygon.bounds[1] + 0.25
            font = get_font_face("Arial")
            
            # Create text paths first
            paths = text2path.make_paths_from_str(
                annotation_text.strip().upper(),
                font=font,
                size=text_height_dxf,
                align=TextEntityAlignment.LEFT
            )
            
            # Calculate actual text width from the path's bounds
            text_bbox = path.bbox(paths)
            #text_width = text_bbox[2] - text_bbox[0]  # xmax - xmin
            #text_width = text_bbox.width
            # Calculate center point of inner tool contours
            center_x = (inner_min_x + inner_max_x) / 2.0
            text_width = text_bbox.extmax.x - text_bbox.extmin.x
            # Calculate starting x position for truly centered text
            text_x = center_x - (text_width / 2.0)
            
            # Create a translation matrix
            translation = ezdxf.math.Matrix44.translate(text_x, text_y_dxf, 0)
            # Apply the translation to each path
            translated_paths = [p.transform(translation) for p in paths]
        
            # Render the paths as splines and polylines
            path.render_splines_and_polylines(
                msp, 
                translated_paths, 
                dxfattribs={"layer": "ANNOTATION", "color": 7}
            )

    # Save the DXF
    dxf_filepath = os.path.join("./outputs", "out.dxf")
    doc.saveas(dxf_filepath)

    # ---------------------
    # 9) For the preview images, draw the polygons and place text similarly
    # ---------------------
    #draw_polygons_inch(final_polygons_inch, output_img, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
    for poly in final_polygons_inch:
    # Skip the boundary polygon
        if boundary_polygon is not None and poly == boundary_polygon:
            continue
        draw_single_polygon(poly, output_img, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
    new_outlines,preview_scale, off_y, padding_px, PH= draw_and_pad(final_polygons_inch, scaling_factor,boundary_polygon)

    #draw_polygons_inch(final_polygons_inch, new_outlines, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
    import math
    if annotation_text.strip():
        # Common variables
        font = cv2.FONT_HERSHEY_SIMPLEX
        text = annotation_text.strip().upper()
        canvas_height, canvas_width = new_outlines.shape[:2]
        
        if boundary_polygon is not None:
            # Keep original code for output_img
            text_x_img = int(((inner_min_x + inner_max_x) / 2.0) / scaling_factor)
            text_y_in = boundary_polygon.bounds[1] + 0.25
            text_y_img = int(processed_size[0] - (text_y_in / scaling_factor))
            org = (text_x_img - int(len(annotation_text.strip()) * 6), text_y_img)
            
            # Process for output_img with mask (keeping your original code)
            temp_img = np.zeros_like(output_img)
            cv2.putText(temp_img, text, org, font, 2, (0, 0, 255), 4, cv2.LINE_AA)
            cv2.putText(temp_img, text, org, font, 2, (255, 255, 255), 2, cv2.LINE_AA)
            outline_mask = cv2.cvtColor(temp_img, cv2.COLOR_BGR2GRAY)
            _, outline_mask = cv2.threshold(outline_mask, 1, 255, cv2.THRESH_BINARY)
            output_img[outline_mask > 0] = temp_img[outline_mask > 0]
            
            # For new_outlines - simple, centered text
            def optimal_font_dims(img, font_scale = 1e-3, thickness_scale = 2e-3):
                h, w, _ = img.shape
                font_scale = min(w, h) * font_scale
                thickness = math.ceil(min(w, h) * thickness_scale)
                return font_scale, thickness
            font_scale,thickness = optimal_font_dims(new_outlines)
            (text_width, text_height), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, font_scale, thickness)
            text_x = (canvas_width - text_width) // 2
            raw_y       = (text_y_in / scaling_factor) * preview_scale
            y1          = raw_y - off_y + padding_px
            text_y_px   = int(round(PH - 1 - y1))
            text_y_px_adjusted = text_y_px - baseline
            # bottom_margin_px = int(0.25 / scaling_factor)
            # font_scale,_ = optimal_font_dims(new_outlines)
            #text_y_outlines = int(canvas_height - (text_y_in + (0.75) / scaling_factor))
            
            # First outline, then inner text
            cv2.putText(new_outlines, text, (text_x, text_y_px_adjusted), font, font_scale, (0, 0, 255), thickness+2, cv2.LINE_AA)
            cv2.putText(new_outlines, text, (text_x, text_y_px_adjusted), font, font_scale, (255, 255, 255), thickness-1, cv2.LINE_AA)
        

    outlines_color = cv2.cvtColor(new_outlines, cv2.COLOR_BGR2RGB)
    print("Total prediction time: {:.2f} seconds".format(time.time() - overall_start))
    return (
        cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB),
        outlines_color,
        dxf_filepath,
        dilated_mask,
        str(scaling_factor)
    )

# ---------------------
# Gradio Interface
# ---------------------
if __name__ == "__main__":
    os.makedirs("./outputs", exist_ok=True)
    def gradio_predict(img, offset, offset_unit, finger_clearance, add_boundary, boundary_length, boundary_width, annotation_text):
        try:
            return predict(img, offset, offset_unit, finger_clearance, add_boundary, boundary_length, boundary_width, annotation_text)
        except Exception as e:
            return None, None, None, None, f"Error: {str(e)}"
    iface = gr.Interface(
        fn=gradio_predict,
        inputs=[
            gr.Image(label="Input Image"),
            gr.Number(label="Offset value for Mask", value=0.075),
            gr.Dropdown(label="Offset Unit", choices=["mm", "inches"], value="inches"),
            gr.Dropdown(label="Add Finger Clearance?", choices=["Yes", "No"], value="Yes"),
            gr.Dropdown(label="Add Rectangular Boundary?", choices=["Yes", "No"], value="Yes"),
            gr.Number(label="Boundary Length", value=50, precision=2),
            gr.Number(label="Boundary Width", value=50, precision=2),
            gr.Textbox(label="Annotation (max 20 chars)", max_length=20, placeholder="Type up to 20 characters")
        ],
        outputs=[
            gr.Image(format="png",label="Output Image"),
            gr.Image(format="png",label="Outlines of Objects"),
            gr.File(label="DXF file"),
            gr.Image(label="Mask"),
            gr.Textbox(label="Scaling Factor (inches/pixel)")
        ],
        examples=[
            ["./Test20.jpg", 0.075, "inches", "Yes", "No", 300.0, 200.0, "MyTool"],
            ["./Test21.jpg", 0.075, "inches", "Yes", "Yes", 300.0, 200.0, "Tool2"]
        ]
    )
    iface.launch(share=True)