File size: 54,011 Bytes
72d1759
 
 
 
 
 
 
 
 
 
 
710feff
72d1759
2caa9f6
 
0c6b4cf
 
72d1759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd51374
72d1759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09fada4
72d1759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09fada4
72d1759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09fada4
72d1759
 
 
09fada4
0c6b4cf
 
 
 
09fada4
0c6b4cf
72d1759
 
 
 
 
 
 
 
 
 
 
 
 
7022d7f
72d1759
 
7022d7f
72d1759
 
 
 
 
 
 
 
 
09fada4
0c6b4cf
09fada4
 
0c6b4cf
 
 
09fada4
0c6b4cf
 
09fada4
0c6b4cf
 
d7401be
0c6b4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09fada4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
 
09fada4
 
 
0c6b4cf
 
 
 
 
09fada4
0c6b4cf
fd51374
 
0c6b4cf
 
 
 
 
 
 
fd51374
0c6b4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
0c6b4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09fada4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
 
 
 
 
 
 
 
 
 
09fada4
0c6b4cf
 
09fada4
0633d98
0c6b4cf
09fada4
0c6b4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd51374
0c6b4cf
fd51374
 
 
 
0c6b4cf
 
 
 
 
 
 
 
 
 
 
 
fd51374
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
 
 
fd51374
 
0c6b4cf
fd51374
0c6b4cf
 
 
 
 
 
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efeae0f
fd51374
 
 
 
 
 
0c6b4cf
fd51374
 
0c6b4cf
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
fd51374
 
0c6b4cf
fd51374
 
 
 
 
 
 
 
 
 
0c6b4cf
 
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fab5aaa
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efeae0f
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
fd51374
 
 
 
 
 
 
 
 
 
0c6b4cf
 
fd51374
 
 
 
0c6b4cf
fd51374
0c6b4cf
 
 
 
 
 
 
fd51374
0c6b4cf
 
 
 
 
fd51374
 
 
0c6b4cf
 
 
 
 
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
 
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
fd51374
 
 
 
 
0c6b4cf
fd51374
 
 
 
 
 
 
0c6b4cf
 
fd51374
 
 
0c6b4cf
fd51374
 
 
0c6b4cf
 
 
 
 
fd51374
 
0c6b4cf
 
fd51374
0c6b4cf
 
 
 
 
 
 
 
 
 
 
 
 
fd51374
 
 
 
 
 
 
0c6b4cf
fd51374
0c6b4cf
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6b4cf
 
 
 
 
 
 
 
fd51374
 
 
 
 
 
 
0633d98
fd51374
 
 
 
 
d7401be
 
fd51374
 
 
d7401be
 
 
 
fd51374
 
d7401be
 
fd51374
d7401be
 
0c6b4cf
fd51374
 
 
0c6b4cf
fd51374
 
 
 
 
0c6b4cf
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
fd51374
d7401be
 
fd51374
 
 
d7401be
 
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
 
fd51374
 
d7401be
 
fd51374
d7401be
 
0c6b4cf
fd51374
d7401be
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
 
 
fd51374
d7401be
 
 
 
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
fd51374
 
 
 
 
d7401be
 
 
fd51374
 
d7401be
 
fd51374
d7401be
 
0c6b4cf
fd51374
 
 
 
 
 
 
 
 
 
 
 
d7401be
fd51374
 
0c6b4cf
 
fd51374
 
 
0c6b4cf
 
 
 
fd51374
 
0c6b4cf
 
fd51374
0c6b4cf
 
 
fd51374
 
 
 
0c6b4cf
fd51374
 
0c6b4cf
d7401be
fd51374
 
 
d7401be
0c6b4cf
 
 
fd51374
 
0c6b4cf
 
fd51374
0c6b4cf
 
 
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
fd51374
 
 
 
 
 
 
 
 
 
 
 
 
 
d7401be
 
fd51374
 
 
d7401be
 
 
 
fd51374
 
d7401be
 
fd51374
d7401be
 
0c6b4cf
d7401be
0c6b4cf
 
 
d7401be
0c6b4cf
 
 
 
 
d7401be
 
 
 
 
0c6b4cf
 
 
 
 
 
 
 
09fada4
0c6b4cf
fd51374
72d1759
0c6b4cf
 
 
5f29e78
2f34187
 
 
5f29e78
2f34187
09fada4
 
 
 
 
 
2f34187
fd51374
0c6b4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72d1759
 
 
 
0c6b4cf
72d1759
 
 
 
 
 
 
 
0c6b4cf
 
09fada4
 
 
0c6b4cf
 
 
 
 
 
 
 
 
 
 
 
fd51374
 
 
 
 
 
 
09fada4
fd51374
 
 
 
 
 
 
 
 
72d1759
 
0c6b4cf
 
 
 
 
 
 
fd51374
 
 
 
0c6b4cf
fd51374
 
 
 
0c6b4cf
fd51374
 
 
 
0c6b4cf
fd51374
 
0c6b4cf
fd51374
0c6b4cf
 
 
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
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
import base64
import os
from io import BytesIO

import cv2
import gradio as gr
import numpy as np
import pyrebase
import requests
from openai import OpenAI
from PIL import Image, ImageDraw, ImageFont
from ultralytics import YOLO

from prompts import remove_unwanted_prompt

model = YOLO("yolo11n.pt")


def get_middle_thumbnail(input_image: Image, grid_size=(10, 10), padding=3):
    """
    Extract the middle thumbnail from a sprite sheet, handling different aspect ratios
    and removing padding.

    Args:
        input_image: PIL Image
        grid_size: Tuple of (columns, rows)
        padding: Number of padding pixels on each side (default 3)

    Returns:
        PIL.Image: The middle thumbnail image with padding removed
    """
    sprite_sheet = input_image

    # Calculate thumbnail dimensions based on actual sprite sheet size
    sprite_width, sprite_height = sprite_sheet.size
    thumb_width_with_padding = sprite_width // grid_size[0]
    thumb_height_with_padding = sprite_height // grid_size[1]

    # Remove padding to get actual image dimensions
    thumb_width = thumb_width_with_padding - (2 * padding)  # 726 - 6 = 720
    thumb_height = thumb_height_with_padding - (2 * padding)  # varies based on input

    # Calculate the middle position
    total_thumbs = grid_size[0] * grid_size[1]
    middle_index = total_thumbs // 2

    # Calculate row and column of middle thumbnail
    middle_row = middle_index // grid_size[0]
    middle_col = middle_index % grid_size[0]

    # Calculate pixel coordinates for cropping, including padding offset
    left = (middle_col * thumb_width_with_padding) + padding
    top = (middle_row * thumb_height_with_padding) + padding
    right = left + thumb_width  # Don't add padding here
    bottom = top + thumb_height  # Don't add padding here

    # Crop and return the middle thumbnail
    middle_thumb = sprite_sheet.crop((left, top, right, bottom))
    return middle_thumb


def encode_image_to_base64(image: Image.Image, format: str = "JPEG") -> str:
    """
    Convert a PIL image to a base64 string.

    Args:
        image: PIL Image object
        format: Image format to use for encoding (default: PNG)

    Returns:
        Base64 encoded string of the image
    """
    buffered = BytesIO()
    image.save(buffered, format=format)
    return base64.b64encode(buffered.getvalue()).decode("utf-8")


def add_top_numbers(
    input_image,
    num_divisions=20,
    margin=90,
    font_size=70,
    dot_spacing=20,
):
    """
    Add numbered divisions across the top and bottom of any image with dotted vertical lines.

    Args:
        input_image (Image): PIL Image
        num_divisions (int): Number of divisions to create
        margin (int): Size of margin in pixels for numbers
        font_size (int): Font size for numbers
        dot_spacing (int): Spacing between dots in pixels
    """
    # Load the image
    original_image = input_image

    # Create new image with extra space for numbers on top and bottom
    new_width = original_image.width
    new_height = original_image.height + (
        2 * margin
    )  # Add margin to both top and bottom
    new_image = Image.new("RGB", (new_width, new_height), "white")

    # Paste original image in the middle
    new_image.paste(original_image, (0, margin))

    # Initialize drawing context
    draw = ImageDraw.Draw(new_image)

    try:
        font = ImageFont.truetype("arial.ttf", font_size)
    except OSError:
        print("Using default font")
        font = ImageFont.load_default(size=font_size)

    # Calculate division width
    division_width = original_image.width / num_divisions

    # Draw division numbers and dotted lines
    for i in range(num_divisions):
        x = (i * division_width) + (division_width / 2)

        # Draw number at top
        draw.text((x, margin // 2), str(i + 1), fill="black", font=font, anchor="mm")

        # Draw number at bottom
        draw.text(
            (x, new_height - (margin // 2)),
            str(i + 1),
            fill="black",
            font=font,
            anchor="mm",
        )

        # Draw dotted line from top margin to bottom margin
        y_start = margin
        y_end = new_height - margin

        # Draw dots with specified spacing
        current_y = y_start
        while current_y < y_end:
            draw.circle(
                [x - 1, current_y - 1, x + 1, current_y + 1],
                fill="black",
                width=5,
                radius=3,
            )
            current_y += dot_spacing

    return new_image


def analyze_image(numbered_input_image: Image, prompt, input_image, ct):
    """
    Perform inference on an image using GPT-4V.

    Args:
        numbered_input_image (Image): PIL Image
        prompt (str): The prompt/question about the image
        input_image (Image): input image without numbers

    Returns:
        str: The model's response
    """
    client = OpenAI()
    base64_image = encode_image_to_base64(numbered_input_image, format="JPEG")

    messages = [
        {
            "role": "user",
            "content": [
                {"type": "text", "text": prompt},
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
                },
            ],
        }
    ]

    response = client.chat.completions.create(
        model="gpt-4o", messages=messages, max_tokens=300
    )

    messages.extend(
        [
            {"role": "assistant", "content": response.choices[0].message.content},
            {
                "role": "user",
                "content": "please return the response in the json with keys left_row, right_row, and num_of_speakers",
            },
        ],
    )

    response = (
        client.chat.completions.create(model="gpt-4o", messages=messages)
        .choices[0]
        .message.content
    )

    left_index = response.find("{")
    right_index = response.rfind("}")

    try:
        if left_index != -1 and right_index != -1:
            print(response[left_index : right_index + 1])
            response_json = eval(response[left_index : right_index + 1])
    except Exception as e:
        print(e)
        return 0, 20

    return (
        response_json["left_row"],
        response_json["right_row"],
        response_json["num_of_speakers"],
    )


def get_sprite_firebase(cid, rsid, uid):
    config = {
        "apiKey": f"{os.getenv('FIREBASE_API_KEY')}",
        "authDomain": f"{os.getenv('FIREBASE_AUTH_DOMAIN')}",
        "databaseURL": f"{os.getenv('FIREBASE_DATABASE_URL')}",
        "projectId": f"{os.getenv('FIREBASE_PROJECT_ID')}",
        "storageBucket": f"{os.getenv('FIREBASE_STORAGE_BUCKET')}",
        "messagingSenderId": f"{os.getenv('FIREBASE_MESSAGING_SENDER_ID')}",
        "appId": f"{os.getenv('FIREBASE_APP_ID')}",
        "measurementId": f"{os.getenv('FIREBASE_MEASUREMENT_ID')}",
    }

    firebase = pyrebase.initialize_app(config)
    db = firebase.database()
    account_id = os.getenv("ROLL_ACCOUNT")

    COLLAB_EDIT_LINK = "collab_sprite_link_handler"

    path = f"{account_id}/{COLLAB_EDIT_LINK}/{uid}/{cid}/{rsid}"

    data = db.child(path).get()
    return data.val()


def find_persons_center(image, num_of_speakers=1):
    """
    Find the center point of the largest num_of_speakers persons in the image.
    If multiple persons are detected, merge the bounding boxes of only the largest ones.

    Args:
        image: CV2/numpy array image
        num_of_speakers: Number of speakers to consider (default: 1)

    Returns:
        int: x-coordinate of the center point of all considered persons
    """
    # Detect persons (class 0 in COCO dataset)
    results = model(image, classes=[0], conf=0.6)

    if not results or len(results[0].boxes) == 0:
        # If no persons detected, return center of image
        return image.shape[1] // 2

    # Get all person boxes
    boxes = results[0].boxes.xyxy.cpu().numpy()

    # Print the number of persons detected (for debugging)
    print(f"Detected {len(boxes)} persons in the image")

    if len(boxes) == 1:
        # If only one person, return center of their bounding box
        x1, _, x2, _ = boxes[0]
        center_x = int((x1 + x2) // 2)
        print(f"Single person detected at center x: {center_x}")
        return center_x
    else:
        # Multiple persons - consider only the largest num_of_speakers boxes

        # Calculate area for each box
        box_areas = [(box[2] - box[0]) * (box[3] - box[1]) for box in boxes]

        # Sort boxes by area (largest first) and take top num_of_speakers
        sorted_indices = sorted(
            range(len(box_areas)), key=lambda i: box_areas[i], reverse=True
        )

        # Use all available boxes if fewer detected than requested
        num_boxes_to_use = min(num_of_speakers, len(boxes))
        selected_indices = sorted_indices[:num_boxes_to_use]
        selected_boxes = [boxes[i] for i in selected_indices]

        # Create a merged bounding box from selected boxes
        left_x = min(box[0] for box in selected_boxes)
        right_x = max(box[2] for box in selected_boxes)
        merged_center_x = int((left_x + right_x) // 2)

        print(
            f"{num_boxes_to_use} largest persons merged bounding box center x: {merged_center_x}"
        )
        print(f"Merged bounds: left={left_x}, right={right_x}")

        return merged_center_x


def create_layouts(image, left_division, right_division, num_of_speakers):
    """
    Create different layout variations of the image using specific aspect ratios.
    All layout variations will be centered on detected persons.

    Args:
        image: PIL Image
        left_division: Left division index (1-20)
        right_division: Right division index (1-20)

    Returns:
        tuple: (standard_crops, threehalfs_layouts, twothirdhalfs_layouts, twoequalhalfs_layouts, visualization_data)
    """
    # Convert PIL Image to cv2 format
    if isinstance(image, Image.Image):
        image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    else:
        image_cv = image.copy()

    # Get image dimensions
    height, width = image_cv.shape[:2]

    # Calculate division width and crop boundaries
    division_width = width / 20  # Assuming 20 divisions
    left_boundary = int((left_division - 1) * division_width)
    right_boundary = int(right_division * division_width)

    # 1. Create cutout image based on divisions
    cutout_image = image_cv[:, left_boundary:right_boundary].copy()
    cutout_width = right_boundary - left_boundary
    cutout_height = cutout_image.shape[0]

    # 2. Run YOLO on cutout to get person bounding box and center
    results = model(cutout_image, classes=[0], conf=0.6)

    # Default center if no detection
    cutout_center_x = cutout_image.shape[1] // 2
    cutout_center_y = cutout_height // 2

    # Default values for bounding box
    person_top = 0.0
    person_height = float(cutout_height)

    if results and len(results[0].boxes) > 0:
        # Get person detection
        boxes = results[0].boxes.xyxy.cpu().numpy()

        if len(boxes) == 1:
            # Single person
            x1, y1, x2, y2 = boxes[0]
            cutout_center_x = int((x1 + x2) // 2)
            cutout_center_y = int((y1 + y2) // 2)
            person_top = y1
            person_height = y2 - y1
        else:
            # Multiple persons - consider only the largest num_of_speakers boxes

            # Calculate area for each box
            box_areas = [(box[2] - box[0]) * (box[3] - box[1]) for box in boxes]

            # Sort boxes by area (largest first) and take top num_of_speakers
            sorted_indices = sorted(
                range(len(box_areas)), key=lambda i: box_areas[i], reverse=True
            )

            # Use all available boxes if fewer detected than requested
            num_boxes_to_use = min(num_of_speakers, len(boxes))
            selected_indices = sorted_indices[:num_boxes_to_use]
            selected_boxes = [boxes[i] for i in selected_indices]

            # Merge bounding boxes of selected boxes
            left_x = min(box[0] for box in selected_boxes)
            right_x = max(box[2] for box in selected_boxes)
            top_y = min(box[1] for box in selected_boxes)  # Top of highest person
            bottom_y = max(box[3] for box in selected_boxes)  # Bottom of lowest person

            cutout_center_x = int((left_x + right_x) // 2)
            cutout_center_y = int((top_y + bottom_y) // 2)
            person_top = top_y
            person_height = bottom_y - top_y

    # 3. Create 16:9 and 9:16 versions with person properly framed
    aspect_16_9 = 16 / 9
    aspect_9_16 = 9 / 16

    # For 16:9 version (with 5% margin above person)
    target_height_16_9 = int(cutout_width / aspect_16_9)
    if target_height_16_9 <= cutout_height:
        # Calculate 5% of person height for top margin
        top_margin = int(person_height * 0.05)

        # Start 5% above the person's top
        y_start = int(max(0, person_top - top_margin))

        # If this would make the crop exceed the bottom, adjust y_start
        if y_start + target_height_16_9 > cutout_height:
            y_start = int(max(0, cutout_height - target_height_16_9))

        y_end = int(min(cutout_height, y_start + target_height_16_9))
        cutout_16_9 = cutout_image[y_start:y_end, :].copy()
    else:
        # Handle rare case where we need to adjust width (not expected with normal images)
        new_width = int(cutout_height * aspect_16_9)
        x_start = max(
            0, min(cutout_width - new_width, cutout_center_x - new_width // 2)
        )
        x_end = min(cutout_width, x_start + new_width)
        cutout_16_9 = cutout_image[:, x_start:x_end].copy()

    # For 9:16 version (centered on person, adjusted upward for face visibility)
    target_width_9_16 = int(cutout_height * aspect_9_16)

    # Adjust center point upward by 20% of person height to ensure face is visible
    adjusted_center_y = int(cutout_center_y - (person_height * 0.2))

    if target_width_9_16 <= cutout_width:
        # Center horizontally around person
        x_start = int(
            max(
                0,
                min(
                    cutout_width - target_width_9_16,
                    cutout_center_x - target_width_9_16 // 2,
                ),
            )
        )
        x_end = int(min(cutout_width, x_start + target_width_9_16))

        # Use adjusted center point for vertical positioning
        y_start = int(
            max(
                0,
                min(
                    cutout_height - cutout_height,
                    adjusted_center_y - cutout_height // 2,
                ),
            )
        )
        cutout_9_16 = cutout_image[y_start:, x_start:x_end].copy()
    else:
        # Handle rare case where we need to adjust height
        new_height = int(cutout_width / aspect_9_16)

        # Use adjusted center point for vertical positioning
        y_start = int(
            max(0, min(cutout_height - new_height, adjusted_center_y - new_height // 2))
        )
        y_end = int(min(cutout_height, y_start + new_height))
        cutout_9_16 = cutout_image[y_start:y_end, :].copy()

    # 4. Scale the center back to original image coordinates
    original_center_x = left_boundary + cutout_center_x
    original_center_y = cutout_center_y
    original_person_top = person_top

    # Store visualization data for drawing
    visualization_data = {
        "original_center_x": original_center_x,
        "original_center_y": original_center_y,
        "original_person_top": original_person_top,
        "original_person_height": person_height,
        "cutout_bounds": (left_boundary, right_boundary),
    }

    # 5. Create new layout variations - each segment is independently centered on the subject

    # ----- Create crops for threehalfs layout -----
    # For 16:9 (three 5.3:9 segments, each independently centered)
    aspect_5_3_9 = 5.3 / 9

    # Calculate dimensions for each segment
    segment_height_16_9 = cutout_height  # Use full height
    segment_width_16_9 = int(segment_height_16_9 * aspect_5_3_9)

    # Create three segments for 16:9 threehalfs - all centered on the person
    threehalfs_16_9_segments = []
    for i in range(3):
        # Each segment is centered on the person
        segment_x_start = int(
            max(
                0,
                min(
                    cutout_width - segment_width_16_9,
                    cutout_center_x - segment_width_16_9 // 2,
                ),
            )
        )
        segment_x_end = int(min(cutout_width, segment_x_start + segment_width_16_9))

        # Create the segment
        segment = cutout_image[:, segment_x_start:segment_x_end].copy()

        # Add a label for visualization
        label = f"Part {i+1}"
        cv2.putText(
            segment,
            label,
            (10, 30),
            cv2.FONT_HERSHEY_SIMPLEX,
            0.8,
            (255, 255, 255),
            2,
            cv2.LINE_AA,
        )

        threehalfs_16_9_segments.append(segment)

    # For 9:16 (three 9:5.3 segments, each independently centered)
    aspect_9_5_3 = 9 / 5.3

    # Calculate dimensions for each segment
    segment_width_9_16 = cutout_9_16.shape[1]  # Use full width of 9:16 crop
    segment_height_9_16 = int(segment_width_9_16 / aspect_9_5_3)

    # Get adjusted center for 9:16 segments (move up by 20% of person height)
    cutout_9_16_center_y = cutout_9_16.shape[0] // 2
    adjusted_9_16_center_y = int(cutout_9_16_center_y - (person_height * 0.2))
    cutout_9_16_height = cutout_9_16.shape[0]

    # Create three segments for 9:16 threehalfs - all centered on the person
    threehalfs_9_16_segments = []

    for i in range(3):
        # Each segment is centered on the person with adjusted center point
        segment_y_start = int(
            max(
                0,
                min(
                    cutout_9_16_height - segment_height_9_16,
                    person_top,
                ),
            )
        )
        segment_y_end = int(
            min(cutout_9_16_height, segment_y_start + segment_height_9_16)
        )

        # Create the segment
        segment = cutout_9_16[segment_y_start:segment_y_end, :].copy()

        # Add a label for visualization
        label = f"Part {i+1}"
        cv2.putText(
            segment,
            label,
            (10, 30),
            cv2.FONT_HERSHEY_SIMPLEX,
            0.8,
            (255, 255, 255),
            2,
            cv2.LINE_AA,
        )

        threehalfs_9_16_segments.append(segment)

    # ----- Create crops for twothirdhalfs layout -----
    # For 16:9 (two segments: 10.6:9 and 5.3:9 OR 5.3:9 and 10.6:9)
    aspect_10_6_9 = 10.6 / 9

    # Calculate dimensions for segments
    segment1_height_16_9 = cutout_height  # Use full height
    segment1_width_16_9 = int(segment1_height_16_9 * aspect_10_6_9)
    segment2_height_16_9 = cutout_height  # Use full height
    segment2_width_16_9 = int(segment2_height_16_9 * aspect_5_3_9)

    # Create segments for 16:9 twothirdhalfs var1 (10.6:9 then 5.3:9)
    # Both segments independently centered on the person

    # First segment (10.6:9)
    segment_x_start = int(
        max(
            0,
            min(
                cutout_width - segment1_width_16_9,
                cutout_center_x - segment1_width_16_9 // 2,
            ),
        )
    )
    segment_x_end = int(min(cutout_width, segment_x_start + segment1_width_16_9))
    segment1 = cutout_image[:, segment_x_start:segment_x_end].copy()

    # Add label
    cv2.putText(
        segment1,
        "10.6:9",
        (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )

    # Second segment (5.3:9)
    segment_x_start = int(
        max(
            0,
            min(
                cutout_width - segment2_width_16_9,
                cutout_center_x - segment2_width_16_9 // 2,
            ),
        )
    )
    segment_x_end = int(min(cutout_width, segment_x_start + segment2_width_16_9))
    segment2 = cutout_image[:, segment_x_start:segment_x_end].copy()

    # Add label
    cv2.putText(
        segment2,
        "5.3:9",
        (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )

    twothirdhalfs_16_9_var1_segments = [segment1, segment2]

    # Create segments for 16:9 twothirdhalfs var2 (5.3:9 then 10.6:9)
    # First segment (5.3:9) - reuse segment2 from var1
    # Second segment (10.6:9) - reuse segment1 from var1
    twothirdhalfs_16_9_var2_segments = [segment2.copy(), segment1.copy()]

    # For 9:16 (two segments stacked: 9:10.6 and 9:5.3 OR 9:5.3 and 9:10.6)
    aspect_9_10_6 = 9 / 10.6
    aspect_9_5_3 = 9 / 5.3

    # Calculate dimensions for segments
    segment1_width_9_16 = cutout_9_16.shape[1]  # Use full width of 9:16 crop
    segment1_height_9_16 = int(segment1_width_9_16 / aspect_9_10_6)
    segment2_width_9_16 = cutout_9_16.shape[1]  # Use full width of 9:16 crop
    segment2_height_9_16 = int(segment2_width_9_16 / aspect_9_5_3)

    # Create segments for 9:16 twothirdhalfs var1 (9:10.6 then 9:5.3)
    # Both segments independently centered on the person with adjusted center point

    # First segment (9:10.6)
    segment_y_start = int(
        max(
            0,
            min(
                cutout_9_16_height - segment1_height_9_16,
                adjusted_9_16_center_y - segment1_height_9_16 // 2,
            ),
        )
    )
    segment_y_end = int(min(cutout_9_16_height, segment_y_start + segment1_height_9_16))
    segment1 = cutout_9_16[segment_y_start:segment_y_end, :].copy()

    # Add label
    cv2.putText(
        segment1,
        "9:10.6",
        (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )

    # Second segment (9:5.3)
    segment_y_start = int(
        max(
            0,
            min(
                cutout_9_16_height - segment2_height_9_16,
                person_top,
            ),
        )
    )
    segment_y_end = int(min(cutout_9_16_height, segment_y_start + segment2_height_9_16))
    segment2 = cutout_9_16[segment_y_start:segment_y_end, :].copy()

    # Add label
    cv2.putText(
        segment2,
        "9:5.3",
        (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )

    twothirdhalfs_9_16_var1_segments = [segment1, segment2]

    # Create segments for 9:16 twothirdhalfs var2 (9:5.3 then 9:10.6)
    # First segment (9:5.3) - reuse segment2 from var1
    # Second segment (9:10.6) - reuse segment1 from var1
    twothirdhalfs_9_16_var2_segments = [segment2.copy(), segment1.copy()]

    # ----- Create crops for twoequalhalfs layout -----
    # For 16:9 (two 8:9 segments side by side)
    aspect_8_9 = 8 / 9

    # Calculate dimensions for segments
    segment_height_16_9_equal = cutout_height  # Use full height
    segment_width_16_9_equal = int(segment_height_16_9_equal * aspect_8_9)

    # Create segments for 16:9 twoequalhalfs - both centered on the person
    # First segment (8:9)
    segment_x_start = int(
        max(
            0,
            min(
                cutout_width - segment_width_16_9_equal,
                cutout_center_x - segment_width_16_9_equal // 2,
            ),
        )
    )
    segment_x_end = int(min(cutout_width, segment_x_start + segment_width_16_9_equal))
    segment1 = cutout_image[:, segment_x_start:segment_x_end].copy()

    # Add label
    cv2.putText(
        segment1,
        "8:9 (1)",
        (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )

    # Second segment (identical to first for equal halfs)
    segment2 = segment1.copy()

    # Update label for segment 2
    cv2.putText(
        segment2,
        "8:9 (2)",
        (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )

    twoequalhalfs_16_9_segments = [segment1, segment2]

    # For 9:16 (two 9:8 segments stacked)
    aspect_9_8 = 9 / 8

    # Calculate dimensions for segments
    segment_width_9_16_equal = cutout_9_16.shape[1]  # Use full width of 9:16 crop
    segment_height_9_16_equal = int(segment_width_9_16_equal / aspect_9_8)

    # Create segments for 9:16 twoequalhalfs - both centered on the person with adjusted center point
    # First segment (9:8)
    segment_y_start = int(
        max(
            0,
            min(
                cutout_9_16_height - segment_height_9_16_equal,
                max(0, person_top - person_height * 0.05),
            ),
        )
    )
    segment_y_end = int(
        min(cutout_9_16_height, segment_y_start + segment_height_9_16_equal)
    )
    segment1 = cutout_9_16[segment_y_start:segment_y_end, :].copy()

    # Add label
    cv2.putText(
        segment1,
        "9:8 (1)",
        (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )

    # Second segment (identical to first for equal halfs)
    segment2 = segment1.copy()

    # Update label for segment 2
    cv2.putText(
        segment2,
        "9:8 (2)",
        (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX,
        0.8,
        (255, 255, 255),
        2,
        cv2.LINE_AA,
    )

    twoequalhalfs_9_16_segments = [segment1, segment2]

    # 6. Create composite layouts by joining segments
    # Function to create a composite image
    def create_composite(segments, horizontal=True):
        if not segments:
            return None

        if horizontal:
            # Calculate the total width and max height
            total_width = sum(segment.shape[1] for segment in segments)
            max_height = max(segment.shape[0] for segment in segments)

            # Create a canvas
            composite = np.zeros((max_height, total_width, 3), dtype=np.uint8)

            # Place segments side by side
            x_offset = 0
            for segment in segments:
                h, w = segment.shape[:2]
                composite[:h, x_offset : x_offset + w] = segment
                x_offset += w

        else:  # vertical stacking
            # Calculate the max width and total height
            max_width = max(segment.shape[1] for segment in segments)
            total_height = sum(segment.shape[0] for segment in segments)

            # Create a canvas
            composite = np.zeros((total_height, max_width, 3), dtype=np.uint8)

            # Place segments top to bottom
            y_offset = 0
            for segment in segments:
                h, w = segment.shape[:2]
                composite[y_offset : y_offset + h, :w] = segment
                y_offset += h

        return composite

    # Create composite layouts
    threehalfs_16_9_composite = create_composite(
        threehalfs_16_9_segments, horizontal=True
    )
    threehalfs_9_16_composite = create_composite(
        threehalfs_9_16_segments, horizontal=False
    )

    twothirdhalfs_16_9_var1_composite = create_composite(
        twothirdhalfs_16_9_var1_segments, horizontal=True
    )
    twothirdhalfs_16_9_var2_composite = create_composite(
        twothirdhalfs_16_9_var2_segments, horizontal=True
    )
    twothirdhalfs_9_16_var1_composite = create_composite(
        twothirdhalfs_9_16_var1_segments, horizontal=False
    )
    twothirdhalfs_9_16_var2_composite = create_composite(
        twothirdhalfs_9_16_var2_segments, horizontal=False
    )

    twoequalhalfs_16_9_composite = create_composite(
        twoequalhalfs_16_9_segments, horizontal=True
    )
    twoequalhalfs_9_16_composite = create_composite(
        twoequalhalfs_9_16_segments, horizontal=False
    )

    # Add labels to all composites
    def add_label(img, label):
        if img is None:
            return None

        font = cv2.FONT_HERSHEY_SIMPLEX
        label_settings = {
            "fontScale": 1.0,
            "fontFace": font,
            "thickness": 2,
        }

        # Draw background for text
        text_size = cv2.getTextSize(
            label,
            fontFace=label_settings["fontFace"],
            fontScale=label_settings["fontScale"],
            thickness=label_settings["thickness"],
        )

        cv2.rectangle(
            img,
            (10, 10),
            (10 + text_size[0][0] + 10, 10 + text_size[0][1] + 10),
            (0, 0, 0),
            -1,
        )  # Black background

        # Draw text
        cv2.putText(
            img,
            label,
            (15, 15 + text_size[0][1]),
            fontFace=label_settings["fontFace"],
            fontScale=label_settings["fontScale"],
            thickness=label_settings["thickness"],
            color=(255, 255, 255),
            lineType=cv2.LINE_AA,
        )
        return img

    # Label the basic crops
    cutout_image_labeled = add_label(cutout_image.copy(), "Cutout")
    cutout_16_9_labeled = add_label(cutout_16_9.copy(), "16:9")
    cutout_9_16_labeled = add_label(cutout_9_16.copy(), "9:16")

    # Label the composite layouts
    threehalfs_16_9_labeled = add_label(threehalfs_16_9_composite, "Three Halfs 16:9")
    threehalfs_9_16_labeled = add_label(threehalfs_9_16_composite, "Three Halfs 9:16")

    twothirdhalfs_16_9_var1_labeled = add_label(
        twothirdhalfs_16_9_var1_composite, "Two Thirds Var1 16:9"
    )
    twothirdhalfs_16_9_var2_labeled = add_label(
        twothirdhalfs_16_9_var2_composite, "Two Thirds Var2 16:9"
    )
    twothirdhalfs_9_16_var1_labeled = add_label(
        twothirdhalfs_9_16_var1_composite, "Two Thirds Var1 9:16"
    )
    twothirdhalfs_9_16_var2_labeled = add_label(
        twothirdhalfs_9_16_var2_composite, "Two Thirds Var2 9:16"
    )

    twoequalhalfs_16_9_labeled = add_label(
        twoequalhalfs_16_9_composite, "Two Equal Halfs 16:9"
    )
    twoequalhalfs_9_16_labeled = add_label(
        twoequalhalfs_9_16_composite, "Two Equal Halfs 9:16"
    )

    # Convert all output images to PIL format
    def cv2_to_pil(img):
        if img is None:
            return None
        return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

    # Convert standard crops
    standard_crops = {
        "cutout": cv2_to_pil(cutout_image_labeled),
        "16:9": cv2_to_pil(cutout_16_9_labeled),
        "9:16": cv2_to_pil(cutout_9_16_labeled),
    }

    # Convert threehalfs layouts
    threehalfs_layouts = {
        "16:9": cv2_to_pil(threehalfs_16_9_labeled),
        "9:16": cv2_to_pil(threehalfs_9_16_labeled),
    }

    # Convert twothirdhalfs layouts
    twothirdhalfs_layouts = {
        "16:9_var1": cv2_to_pil(twothirdhalfs_16_9_var1_labeled),
        "16:9_var2": cv2_to_pil(twothirdhalfs_16_9_var2_labeled),
        "9:16_var1": cv2_to_pil(twothirdhalfs_9_16_var1_labeled),
        "9:16_var2": cv2_to_pil(twothirdhalfs_9_16_var2_labeled),
    }

    # Convert twoequalhalfs layouts
    twoequalhalfs_layouts = {
        "16:9": cv2_to_pil(twoequalhalfs_16_9_labeled),
        "9:16": cv2_to_pil(twoequalhalfs_9_16_labeled),
    }

    return (
        standard_crops,
        threehalfs_layouts,
        twothirdhalfs_layouts,
        twoequalhalfs_layouts,
        visualization_data,
    )


def draw_layout_regions(
    image, left_division, right_division, visualization_data, layout_type
):
    """
    Create a visualization showing the layout regions overlaid on the original image.
    Each region is independently centered on the subject, as in practice different videos
    would be stacked in these layouts.

    Args:
        image: PIL Image
        left_division: Left division index (1-20)
        right_division: Right division index (1-20)
        visualization_data: Dictionary with visualization data from create_layouts
        layout_type: Type of layout to visualize (e.g., "standard", "threehalfs", "twothirdhalfs_var1", etc.)

    Returns:
        PIL Image: Original image with layout regions visualized
    """
    # Convert PIL Image to cv2 format
    if isinstance(image, Image.Image):
        image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    else:
        image_cv = image.copy()

    # Get a clean copy for drawing
    visualization = image_cv.copy()

    # Get image dimensions
    height, width = image_cv.shape[:2]

    # Extract visualization data
    original_center_x = visualization_data["original_center_x"]
    original_center_y = visualization_data["original_center_y"]
    original_person_top = visualization_data["original_person_top"]
    original_person_height = visualization_data["original_person_height"]
    left_boundary, right_boundary = visualization_data["cutout_bounds"]
    cutout_width = right_boundary - left_boundary

    # Define colors for different layouts (BGR format)
    colors = {
        "standard": {"16:9": (0, 255, 0), "9:16": (255, 0, 0)},  # Green, Blue
        "threehalfs": {"16:9": (0, 165, 255), "9:16": (255, 255, 0)},  # Orange, Cyan
        "twothirdhalfs_var1": {
            "16:9": (255, 0, 255),
            "9:16": (128, 0, 128),
        },  # Magenta, Purple
        "twothirdhalfs_var2": {
            "16:9": (0, 255, 255),
            "9:16": (128, 128, 0),
        },  # Yellow, Teal
        "twoequalhalfs": {
            "16:9": (0, 128, 128),
            "9:16": (255, 165, 0),
        },  # Dark Cyan, Blue-Green
    }

    # Define line thickness and font
    thickness = 3
    font = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = 0.8
    font_thickness = 2

    # Draw standard layouts (16:9 and 9:16)
    if layout_type == "standard":
        # Draw 16:9 crop
        aspect_16_9 = 16 / 9
        target_height_16_9 = int(cutout_width / aspect_16_9)

        # Calculate 20% of person height for top margin
        top_margin = int(original_person_height * 0.05)
        y_start = int(max(0, original_person_top - top_margin))
        if y_start + target_height_16_9 > height:
            y_start = int(max(0, height - target_height_16_9))
        y_end = int(min(height, y_start + target_height_16_9))

        cv2.rectangle(
            visualization,
            (left_boundary, y_start),
            (right_boundary, y_end),
            colors["standard"]["16:9"],
            thickness,
        )
        cv2.putText(
            visualization,
            "16:9",
            (left_boundary + 5, y_start + 30),
            font,
            font_scale,
            colors["standard"]["16:9"],
            font_thickness,
        )

        # Draw 9:16 crop
        aspect_9_16 = 9 / 16
        target_width_9_16 = int(height * aspect_9_16)

        x_start = max(
            0,
            min(width - target_width_9_16, original_center_x - target_width_9_16 // 2),
        )
        x_end = x_start + target_width_9_16

        cv2.rectangle(
            visualization,
            (x_start, 0),
            (x_end, height),
            colors["standard"]["9:16"],
            thickness,
        )
        cv2.putText(
            visualization,
            "9:16",
            (x_start + 5, 30),
            font,
            font_scale,
            colors["standard"]["9:16"],
            font_thickness,
        )

    # Draw threehalfs layouts - each segment is centered on the subject
    elif layout_type == "threehalfs":
        # For 16:9 (three 5.3:9 segments side by side - visually only)
        aspect_5_3_9 = 5.3 / 9
        segment_height = height
        segment_width = int(segment_height * aspect_5_3_9)

        # Calculate total width for visualization purposes
        total_width = segment_width * 3
        start_x = max(0, original_center_x - total_width // 2)

        for i in range(3):
            # For visualization, we'll place them side by side
            vis_segment_x_start = start_x + i * segment_width
            vis_segment_x_end = vis_segment_x_start + segment_width

            # But each segment would actually be centered on the subject independently
            # Here we also draw the centered version more faintly
            actual_segment_x_start = max(
                0, min(width - segment_width, original_center_x - segment_width // 2)
            )
            actual_segment_x_end = min(width, actual_segment_x_start + segment_width)

            # Draw the visualization placement (side by side)
            cv2.rectangle(
                visualization,
                (vis_segment_x_start, 0),
                (vis_segment_x_end, segment_height),
                colors["threehalfs"]["16:9"],
                thickness,
            )

            # Draw the actual centered placement with dashed lines
            if i > 0:  # Only draw centered versions for parts 2 and 3
                for j in range(0, segment_height, 20):  # Dashed line effect
                    if j % 40 < 20:  # Skip every other segment
                        cv2.line(
                            visualization,
                            (actual_segment_x_start, j),
                            (actual_segment_x_start, min(j + 20, segment_height)),
                            colors["threehalfs"]["16:9"],
                            1,
                        )
                        cv2.line(
                            visualization,
                            (actual_segment_x_end, j),
                            (actual_segment_x_end, min(j + 20, segment_height)),
                            colors["threehalfs"]["16:9"],
                            1,
                        )

            cv2.putText(
                visualization,
                f"16:9 Part {i+1}",
                (vis_segment_x_start + 5, 30 + i * 30),
                font,
                font_scale,
                colors["threehalfs"]["16:9"],
                font_thickness,
            )

        # For 9:16 (three 9:5.3 segments stacked top to bottom - visually only)
        aspect_9_16 = 9 / 16
        target_width_9_16 = int(height * aspect_9_16)
        x_start = max(
            0,
            min(width - target_width_9_16, original_center_x - target_width_9_16 // 2),
        )
        x_end = x_start + target_width_9_16

        aspect_9_5_3 = 9 / 5.3
        segment_width_9_16 = target_width_9_16
        segment_height_9_16 = int(segment_width_9_16 / aspect_9_5_3)

        # Calculate total height for visualization purposes
        total_height = segment_height_9_16 * 3
        start_y = max(0, height // 2 - total_height // 2)

        for i in range(3):
            # For visualization, we'll place them stacked
            vis_segment_y_start = start_y + i * segment_height_9_16
            vis_segment_y_end = min(height, vis_segment_y_start + segment_height_9_16)

            # But each segment would actually be centered on the subject independently
            # Here we also draw the centered version more faintly
            actual_segment_y_start = max(
                0,
                min(
                    height - segment_height_9_16,
                    original_center_y - segment_height_9_16 // 2,
                ),
            )
            actual_segment_y_end = min(
                height, actual_segment_y_start + segment_height_9_16
            )

            # Draw the visualization placement (stacked)
            cv2.rectangle(
                visualization,
                (x_start, vis_segment_y_start),
                (x_end, vis_segment_y_end),
                colors["threehalfs"]["9:16"],
                thickness,
            )

            # Draw the actual centered placement with dashed lines
            if i > 0:  # Only draw centered versions for parts 2 and 3
                for j in range(x_start, x_end, 20):  # Dashed line effect
                    if j % 40 < 20:  # Skip every other segment
                        cv2.line(
                            visualization,
                            (j, actual_segment_y_start),
                            (min(j + 20, x_end), actual_segment_y_start),
                            colors["threehalfs"]["9:16"],
                            1,
                        )
                        cv2.line(
                            visualization,
                            (j, actual_segment_y_end),
                            (min(j + 20, x_end), actual_segment_y_end),
                            colors["threehalfs"]["9:16"],
                            1,
                        )

            cv2.putText(
                visualization,
                f"9:16 Part {i+1}",
                (x_start + 5, vis_segment_y_start + 30),
                font,
                font_scale,
                colors["threehalfs"]["9:16"],
                font_thickness,
            )

    # Draw twothirdhalfs layouts
    elif layout_type == "twothirdhalfs_var1" or layout_type == "twothirdhalfs_var2":
        aspect_key = "16:9" if layout_type.endswith("var1") else "9:16"
        layout_color = colors[
            (
                "twothirdhalfs_var1"
                if layout_type.endswith("var1")
                else "twothirdhalfs_var2"
            )
        ][aspect_key]

        if aspect_key == "16:9":
            # For 16:9 (two segments side by side)
            aspect_10_6_9 = 10.6 / 9
            aspect_5_3_9 = 5.3 / 9

            segment1_height = height
            segment1_width = int(
                segment1_height
                * (aspect_10_6_9 if layout_type.endswith("var1") else aspect_5_3_9)
            )
            segment2_height = height
            segment2_width = int(
                segment2_height
                * (aspect_5_3_9 if layout_type.endswith("var1") else aspect_10_6_9)
            )

            # First segment
            segment_center_x = original_center_x - segment2_width // 4
            segment_x_start = int(
                max(
                    0,
                    min(width - segment1_width, segment_center_x - segment1_width // 2),
                )
            )
            segment_x_end = int(min(width, segment_x_start + segment1_width))

            cv2.rectangle(
                visualization,
                (segment_x_start, 0),
                (segment_x_end, segment1_height),
                layout_color,
                thickness,
            )
            cv2.putText(
                visualization,
                f"16:9 Part 1",
                (segment_x_start + 5, 30),
                font,
                font_scale,
                layout_color,
                font_thickness,
            )

            # Second segment
            segment_center_x = original_center_x + segment1_width // 4
            segment_x_start = int(
                max(
                    0,
                    min(width - segment2_width, segment_center_x - segment2_width // 2),
                )
            )
            segment_x_end = int(min(width, segment_x_start + segment2_width))

            cv2.rectangle(
                visualization,
                (segment_x_start, 0),
                (segment_x_end, segment2_height),
                layout_color,
                thickness,
            )
            cv2.putText(
                visualization,
                f"16:9 Part 2",
                (segment_x_start + 5, 60),
                font,
                font_scale,
                layout_color,
                font_thickness,
            )
        else:  # aspect_key == "9:16"
            # For 9:16 (two segments stacked)
            aspect_9_16 = 9 / 16
            target_width_9_16 = int(height * aspect_9_16)
            x_start = max(
                0,
                min(
                    width - target_width_9_16,
                    original_center_x - target_width_9_16 // 2,
                ),
            )
            x_end = x_start + target_width_9_16

            aspect_9_10_6 = 9 / 10.6
            aspect_9_5_3 = 9 / 5.3

            segment1_width = target_width_9_16
            segment1_height = int(
                segment1_width
                / (aspect_9_10_6 if layout_type.endswith("var1") else aspect_9_5_3)
            )
            segment2_width = target_width_9_16
            segment2_height = int(
                segment2_width
                / (aspect_9_5_3 if layout_type.endswith("var1") else aspect_9_10_6)
            )

            # First segment (top)
            segment_y_start = 0
            segment_y_end = min(height, segment_y_start + segment1_height)

            cv2.rectangle(
                visualization,
                (x_start, segment_y_start),
                (x_end, segment_y_end),
                layout_color,
                thickness,
            )
            cv2.putText(
                visualization,
                f"9:16 Part 1",
                (x_start + 5, segment_y_start + 30),
                font,
                font_scale,
                layout_color,
                font_thickness,
            )

            # Second segment (bottom)
            segment_y_start = segment_y_end
            segment_y_end = min(height, segment_y_start + segment2_height)

            cv2.rectangle(
                visualization,
                (x_start, segment_y_start),
                (x_end, segment_y_end),
                layout_color,
                thickness,
            )
            cv2.putText(
                visualization,
                f"9:16 Part 2",
                (x_start + 5, segment_y_start + 30),
                font,
                font_scale,
                layout_color,
                font_thickness,
            )

    # Draw twoequalhalfs layouts
    elif layout_type == "twoequalhalfs":
        # For 16:9 (two 8:9 segments side by side)
        aspect_8_9 = 8 / 9

        segment_height = height
        segment_width = int(segment_height * aspect_8_9)

        # First segment (left)
        segment_center_x = original_center_x - segment_width // 2
        segment_x_start = int(
            max(0, min(width - segment_width, segment_center_x - segment_width // 2))
        )
        segment_x_end = int(min(width, segment_x_start + segment_width))

        cv2.rectangle(
            visualization,
            (segment_x_start, 0),
            (segment_x_end, segment_height),
            colors["twoequalhalfs"]["16:9"],
            thickness,
        )
        cv2.putText(
            visualization,
            f"16:9 Equal 1",
            (segment_x_start + 5, 30),
            font,
            font_scale,
            colors["twoequalhalfs"]["16:9"],
            font_thickness,
        )

        # Second segment (right)
        segment_center_x = original_center_x + segment_width // 2
        segment_x_start = int(
            max(0, min(width - segment_width, segment_center_x - segment_width // 2))
        )
        segment_x_end = int(min(width, segment_x_start + segment_width))

        cv2.rectangle(
            visualization,
            (segment_x_start, 0),
            (segment_x_end, segment_height),
            colors["twoequalhalfs"]["16:9"],
            thickness,
        )
        cv2.putText(
            visualization,
            f"16:9 Equal 2",
            (segment_x_start + 5, 60),
            font,
            font_scale,
            colors["twoequalhalfs"]["16:9"],
            font_thickness,
        )

        # For 9:16 (two 9:8 segments stacked)
        aspect_9_16 = 9 / 16
        target_width_9_16 = int(height * aspect_9_16)
        x_start = max(
            0,
            min(width - target_width_9_16, original_center_x - target_width_9_16 // 2),
        )
        x_end = x_start + target_width_9_16

        aspect_9_8 = 9 / 8
        segment_width_9_16 = target_width_9_16
        segment_height_9_16 = int(segment_width_9_16 / aspect_9_8)

        # First segment (top)
        segment_y_start = 0
        segment_y_end = min(height, segment_y_start + segment_height_9_16)

        cv2.rectangle(
            visualization,
            (x_start, segment_y_start),
            (x_end, segment_y_end),
            colors["twoequalhalfs"]["9:16"],
            thickness,
        )
        cv2.putText(
            visualization,
            f"9:16 Equal 1",
            (x_start + 5, segment_y_start + 30),
            font,
            font_scale,
            colors["twoequalhalfs"]["9:16"],
            font_thickness,
        )

        # Second segment (bottom)
        segment_y_start = segment_y_end
        segment_y_end = min(height, segment_y_start + segment_height_9_16)

        cv2.rectangle(
            visualization,
            (x_start, segment_y_start),
            (x_end, segment_y_end),
            colors["twoequalhalfs"]["9:16"],
            thickness,
        )
        cv2.putText(
            visualization,
            f"9:16 Equal 2",
            (x_start + 5, segment_y_start + 30),
            font,
            font_scale,
            colors["twoequalhalfs"]["9:16"],
            font_thickness,
        )

    # Draw center point of person(s)
    center_radius = 8
    cv2.circle(
        visualization,
        (original_center_x, original_center_y),
        center_radius,
        (255, 255, 255),
        -1,
    )
    cv2.circle(
        visualization,
        (original_center_x, original_center_y),
        center_radius,
        (0, 0, 0),
        2,
    )

    # Convert back to PIL format
    visualization_pil = Image.fromarray(cv2.cvtColor(visualization, cv2.COLOR_BGR2RGB))

    return visualization_pil


def get_image_crop(cid=None, rsid=None, uid=None, ct=None):
    """
    Function that returns both standard and layout variations for visualization.

    Returns:
        gr.Gallery: Gallery of all generated images
    """
    try:
        sprites_data = get_sprite_firebase(cid, rsid, uid)
        image_paths = [sprite_data["url"] for sprite_data in sprites_data]
        durations = [sprite_data["duration"] for sprite_data in sprites_data]
    except Exception:
        image_paths = [
            # "data/C2-Roll3D-i2x-Take2-Nov19.24-PST02.31.31pm.jpg",
            # "data/E2-HamzaA-i2x-Take2-Nov19.24-PST02.31.31pm.jpg",
            "data/F2-Roll4D-i2x-Take2-Nov19.24-PST02.31.31pm.jpg",
            "data/G2-Roll5D-i2x-Take2-Nov19.24-PST02.31.31pm.jpg",
            "data/C1-Roll10D-i1x-Take2-Mar20.25-PST12.14.56pm.jpg",
            "data/C2-Roll10D-i2x-Take2-Mar20.25-PST12.14.56pm.jpg",
        ]

    # Lists to store all images
    all_images = []
    all_captions = []

    for image_path in image_paths:
        # Load image (from local file or URL)
        try:
            if image_path.startswith(("http://", "https://")):
                response = requests.get(image_path)
                input_image = Image.open(BytesIO(response.content))
            else:
                input_image = Image.open(image_path)
        except Exception as e:
            print(f"Error loading image {image_path}: {e}")
            continue

        # Get the middle thumbnail
        mid_image = get_middle_thumbnail(input_image)

        # Add numbered divisions for GPT-4V analysis
        numbered_mid_image = add_top_numbers(
            input_image=mid_image,
            num_divisions=20,
            margin=50,
            font_size=30,
            dot_spacing=20,
        )

        # Analyze the image to get optimal crop divisions
        # This uses GPT-4V to identify the optimal crop points
        (left_division, right_division, num_of_speakers) = analyze_image(
            numbered_mid_image, remove_unwanted_prompt(1), mid_image, ct
        )

        # Safety check for divisions
        if left_division <= 0:
            left_division = 1
        if right_division > 20:
            right_division = 20
        if left_division >= right_division:
            left_division = 1
            right_division = 20

        print(f"Using divisions: left={left_division}, right={right_division}")

        # Create layouts and cutouts using the new function
        (
            standard_crops,
            threehalfs_layouts,
            twothirdhalfs_layouts,
            twoequalhalfs_layouts,
            visualization_data,
        ) = create_layouts(mid_image, left_division, right_division, num_of_speakers)

        # Create all the required visualizations
        # 1. Standard aspect ratio visualization (16:9 and 9:16)
        standard_visualization = draw_layout_regions(
            mid_image, left_division, right_division, visualization_data, "standard"
        )
        all_images.append(standard_visualization)
        all_captions.append(
            f"Standard Aspect Ratios (16:9 & 9:16) {standard_visualization.size}"
        )

        # Add input and middle image to gallery
        all_images.append(input_image)
        all_captions.append(f"Input Image {input_image.size}")

        all_images.append(mid_image)
        all_captions.append(f"Middle Thumbnail {mid_image.size}")

        # Add standard crops
        for key, crop in standard_crops.items():
            all_images.append(crop)
            all_captions.append(f"{key} {crop.size}")

        # Add threehalfs layouts
        for key, layout in threehalfs_layouts.items():
            all_images.append(layout)
            all_captions.append(f"Three Halfs {key} {layout.size}")

        # Add twothirdhalfs layouts
        for key, layout in twothirdhalfs_layouts.items():
            all_images.append(layout)
            all_captions.append(f"Two-Thirds Halfs {key} {layout.size}")

        # Add twoequalhalfs layouts
        for key, layout in twoequalhalfs_layouts.items():
            all_images.append(layout)
            all_captions.append(f"Two Equal Halfs {key} {layout.size}")

    # Return gallery with all images
    return gr.Gallery(value=list(zip(all_images, all_captions)))