File size: 57,010 Bytes
87c3140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26c9c07
87c3140
 
26c9c07
 
 
87c3140
 
 
 
26c9c07
 
87c3140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, sys, inspect, json, shutil, cv2, time, glob #imagesize
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from PIL import Image
from tqdm import tqdm
from time import perf_counter
import concurrent.futures
from threading import Lock
from collections import defaultdict
import multiprocessing
import torch

currentdir = os.path.dirname(inspect.getfile(inspect.currentframe()))
parentdir = os.path.dirname(currentdir)
sys.path.append(currentdir)
from detect import run
sys.path.append(parentdir)
from landmark_processing import LeafSkeleton
from armature_processing import ArmatureSkeleton

def detect_plant_components(cfg, logger, dir_home, Project, Dirs):
    t1_start = perf_counter()
    logger.name = 'Locating Plant Components'
    logger.info(f"Detecting plant components in {len(os.listdir(Project.dir_images))} images")

    try:
        dir_exisiting_labels = cfg['leafmachine']['project']['use_existing_plant_component_detections']
    except:
        dir_exisiting_labels = None
    if cfg['leafmachine']['project']['num_workers'] is None:
        num_workers = 1
    else:
        num_workers = int(cfg['leafmachine']['project']['num_workers'])

    # Weights folder base
    dir_weights = os.path.join(dir_home, 'leafmachine2', 'component_detector','runs','train')
    
    # Detection threshold
    threshold = cfg['leafmachine']['plant_component_detector']['minimum_confidence_threshold']

    detector_version = cfg['leafmachine']['plant_component_detector']['detector_version']
    detector_iteration = cfg['leafmachine']['plant_component_detector']['detector_iteration']
    detector_weights = cfg['leafmachine']['plant_component_detector']['detector_weights']
    weights =  os.path.join(dir_weights,'Plant_Detector',detector_version,detector_iteration,'weights',detector_weights)

    do_save_prediction_overlay_images = not cfg['leafmachine']['plant_component_detector']['do_save_prediction_overlay_images']
    ignore_objects = cfg['leafmachine']['plant_component_detector']['ignore_objects_for_overlay']
    ignore_objects = ignore_objects or []

    if dir_exisiting_labels != None:
        logger.info("Loading existing plant labels")
        fetch_labels(dir_exisiting_labels, os.path.join(Dirs.path_plant_components, 'labels'))
        if len(Project.dir_images) <= 4000:
            logger.debug("Single-threaded create_dictionary_from_txt() len(Project.dir_images) <= 4000")
            A = create_dictionary_from_txt(logger, dir_exisiting_labels, 'Detections_Plant_Components', Project)
        else:
            logger.debug(f"Multi-threaded with ({str(cfg['leafmachine']['project']['num_workers'])}) threads create_dictionary_from_txt() len(Project.dir_images) > 4000")
            A = create_dictionary_from_txt_parallel(logger, cfg, dir_exisiting_labels, 'Detections_Plant_Components', Project)

    else:
        logger.info("Running YOLOv5 to generate plant labels")
        # run(weights = weights,
        #     source = Project.dir_images,
        #     project = Dirs.path_plant_components,
        #     name = Dirs.run_name,
        #     imgsz = (1280, 1280),
        #     nosave = do_save_prediction_overlay_images,
        #     anno_type = 'Plant_Detector',
        #     conf_thres = threshold, 
        #     ignore_objects_for_overlay = ignore_objects,
        #     mode = 'LM2',
        #     LOGGER=logger,)
        source = Project.dir_images
        project = Dirs.path_plant_components
        name = Dirs.run_name
        imgsz = (1280, 1280)
        nosave = do_save_prediction_overlay_images
        anno_type = 'Plant_Detector'
        conf_thres = threshold
        ignore_objects_for_overlay = ignore_objects
        mode = 'LM2'
        LOGGER = logger

        with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
            futures = [executor.submit(run_in_parallel, weights, source, project, name, imgsz, nosave, anno_type,
                                    conf_thres, 10, ignore_objects_for_overlay, mode, LOGGER, i, num_workers) for i in
                    range(num_workers)]
            for future in concurrent.futures.as_completed(futures):
                try:
                    _ = future.result()
                except Exception as e:
                    logger.error(f'Error in thread: {e}')
                    continue

        t2_stop = perf_counter()
        logger.info(f"[Plant components detection elapsed time] {round(t2_stop - t1_start)} seconds")
        logger.info(f"Threads [{num_workers}]")

        if len(Project.dir_images) <= 4000:
            logger.debug("Single-threaded create_dictionary_from_txt() len(Project.dir_images) <= 4000")
            A = create_dictionary_from_txt(logger, os.path.join(Dirs.path_plant_components, 'labels'), 'Detections_Plant_Components', Project)
        else:
            logger.debug(f"Multi-threaded with ({str(cfg['leafmachine']['project']['num_workers'])}) threads create_dictionary_from_txt() len(Project.dir_images) > 4000")
            A = create_dictionary_from_txt_parallel(logger, cfg, os.path.join(Dirs.path_plant_components, 'labels'), 'Detections_Plant_Components', Project)
    
    dict_to_json(Project.project_data, Dirs.path_plant_components, 'Detections_Plant_Components.json')
    
    t1_stop = perf_counter()
    logger.info(f"[Processing plant components elapsed time] {round(t1_stop - t1_start)} seconds")
    torch.cuda.empty_cache()
    return Project
    

def detect_archival_components(cfg, logger, dir_home, Project, Dirs, is_real_run=False, progress_report=None):
    if not cfg['leafmachine']['use_RGB_label_images']:
        logger.name = 'Skipping LeafMachine2 Label Detection'
        logger.info(f"Full image will be used instead of the label collage")  
        if is_real_run:
            progress_report.update_overall(f"Skipping LeafMachine2 Label Detection")             
    else:
        t1_start = perf_counter()
        logger.name = 'Locating Archival Components'
        logger.info(f"Detecting archival components in {len(os.listdir(Project.dir_images))} images")
        if is_real_run:
            progress_report.update_overall(f"Creating LeafMachine2 Label Collage")  

        
        try:
            dir_exisiting_labels = cfg['leafmachine']['project']['use_existing_archival_component_detections']
        except:
            dir_exisiting_labels = None
        if cfg['leafmachine']['project']['num_workers'] is None:
            num_workers = 1
        else:
            num_workers = int(cfg['leafmachine']['project']['num_workers'])
        
        # Weights folder base
        dir_weights = os.path.join(dir_home, 'leafmachine2', 'component_detector','runs','train')
        
        # Detection threshold
        threshold = cfg['leafmachine']['archival_component_detector']['minimum_confidence_threshold']

        detector_version = cfg['leafmachine']['archival_component_detector']['detector_version']
        detector_iteration = cfg['leafmachine']['archival_component_detector']['detector_iteration']
        detector_weights = cfg['leafmachine']['archival_component_detector']['detector_weights']
        weights =  os.path.join(dir_weights,'Archival_Detector',detector_version,detector_iteration,'weights',detector_weights)

        do_save_prediction_overlay_images = not cfg['leafmachine']['archival_component_detector']['do_save_prediction_overlay_images']
        ignore_objects = cfg['leafmachine']['archival_component_detector']['ignore_objects_for_overlay']
        ignore_objects = ignore_objects or []


        if dir_exisiting_labels != None:
            logger.info("Loading existing archival labels")
            fetch_labels(dir_exisiting_labels, os.path.join(Dirs.path_archival_components, 'labels'))
            if len(Project.dir_images) <= 4000:
                logger.debug("Single-threaded create_dictionary_from_txt() len(Project.dir_images) <= 4000")
                A = create_dictionary_from_txt(logger, dir_exisiting_labels, 'Detections_Archival_Components', Project)
            else:
                logger.debug(f"Multi-threaded with ({str(cfg['leafmachine']['project']['num_workers'])}) threads create_dictionary_from_txt() len(Project.dir_images) > 4000")
                A = create_dictionary_from_txt_parallel(logger, cfg, dir_exisiting_labels, 'Detections_Archival_Components', Project)

        else:
            logger.info("Running YOLOv5 to generate archival labels")
            # run(weights = weights,
            #     source = Project.dir_images,
            #     project = Dirs.path_archival_components,
            #     name = Dirs.run_name,
            #     imgsz = (1280, 1280),
            #     nosave = do_save_prediction_overlay_images,
            #     anno_type = 'Archival_Detector',
            #     conf_thres = threshold, 
            #     ignore_objects_for_overlay = ignore_objects,
            #     mode = 'LM2',
            #     LOGGER=logger)
            # split the image paths into 4 chunks
            source = Project.dir_images
            project = Dirs.path_archival_components
            name = Dirs.run_name
            imgsz = (1280, 1280)
            nosave = do_save_prediction_overlay_images
            anno_type = 'Archival_Detector'
            conf_thres = threshold
            ignore_objects_for_overlay = ignore_objects
            mode = 'LM2'
            LOGGER = logger

            with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
                futures = [executor.submit(run_in_parallel, weights, source, project, name, imgsz, nosave, anno_type,
                                        conf_thres, 10, ignore_objects_for_overlay, mode, LOGGER, i, num_workers) for i in
                        range(num_workers)]
                for future in concurrent.futures.as_completed(futures):
                    try:
                        _ = future.result()
                    except Exception as e:
                        logger.error(f'Error in thread: {e}')
                        continue

            t2_stop = perf_counter()
            logger.info(f"[Archival components detection elapsed time] {round(t2_stop - t1_start)} seconds")
            logger.info(f"Threads [{num_workers}]")

            if len(Project.dir_images) <= 4000:
                logger.debug("Single-threaded create_dictionary_from_txt() len(Project.dir_images) <= 4000")
                A = create_dictionary_from_txt(logger, os.path.join(Dirs.path_archival_components, 'labels'), 'Detections_Archival_Components', Project)
            else:
                logger.debug(f"Multi-threaded with ({str(cfg['leafmachine']['project']['num_workers'])}) threads create_dictionary_from_txt() len(Project.dir_images) > 4000")
                A = create_dictionary_from_txt_parallel(logger, cfg, os.path.join(Dirs.path_archival_components, 'labels'), 'Detections_Archival_Components', Project)
        
        dict_to_json(Project.project_data, Dirs.path_archival_components, 'Detections_Archival_Components.json')

        t1_stop = perf_counter()
        logger.info(f"[Processing archival components elapsed time] {round(t1_stop - t1_start)} seconds")
        torch.cuda.empty_cache()
    return Project


def detect_armature_components(cfg, logger, dir_home, Project, Dirs):
    t1_start = perf_counter()
    logger.name = 'Locating Armature Components'
    logger.info(f"Detecting armature components in {len(os.listdir(Project.dir_images))} images")

    if cfg['leafmachine']['project']['num_workers'] is None:
        num_workers = 1
    else:
        num_workers = int(cfg['leafmachine']['project']['num_workers'])

    # Weights folder base
    dir_weights = os.path.join(dir_home, 'leafmachine2', 'component_detector','runs','train')
    
    # Detection threshold
    threshold = cfg['leafmachine']['armature_component_detector']['minimum_confidence_threshold']

    detector_version = cfg['leafmachine']['armature_component_detector']['detector_version']
    detector_iteration = cfg['leafmachine']['armature_component_detector']['detector_iteration']
    detector_weights = cfg['leafmachine']['armature_component_detector']['detector_weights']
    weights =  os.path.join(dir_weights,'Armature_Detector',detector_version,detector_iteration,'weights',detector_weights)

    do_save_prediction_overlay_images = not cfg['leafmachine']['armature_component_detector']['do_save_prediction_overlay_images']
    ignore_objects = cfg['leafmachine']['armature_component_detector']['ignore_objects_for_overlay']
    ignore_objects = ignore_objects or []

    logger.info("Running YOLOv5 to generate armature labels")

    source = Project.dir_images
    project = Dirs.path_armature_components
    name = Dirs.run_name
    imgsz = (1280, 1280)
    nosave = do_save_prediction_overlay_images
    anno_type = 'Armature_Detector'
    conf_thres = threshold
    ignore_objects_for_overlay = ignore_objects
    mode = 'LM2'
    LOGGER = logger

    with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
        futures = [executor.submit(run_in_parallel, weights, source, project, name, imgsz, nosave, anno_type,
                                conf_thres, 10, ignore_objects_for_overlay, mode, LOGGER, i, num_workers) for i in
                range(num_workers)]
        for future in concurrent.futures.as_completed(futures):
            try:
                _ = future.result()
            except Exception as e:
                logger.error(f'Error in thread: {e}')
                continue

    t2_stop = perf_counter()
    logger.info(f"[Plant components detection elapsed time] {round(t2_stop - t1_start)} seconds")
    logger.info(f"Threads [{num_workers}]")

    if len(Project.dir_images) <= 4000:
        logger.debug("Single-threaded create_dictionary_from_txt() len(Project.dir_images) <= 4000")
        A = create_dictionary_from_txt(logger, os.path.join(Dirs.path_armature_components, 'labels'), 'Detections_Armature_Components', Project)
    else:
        logger.debug(f"Multi-threaded with ({str(cfg['leafmachine']['project']['num_workers'])}) threads create_dictionary_from_txt() len(Project.dir_images) > 4000")
        A = create_dictionary_from_txt_parallel(logger, cfg, os.path.join(Dirs.path_armature_components, 'labels'), 'Detections_Armature_Components', Project)

    dict_to_json(Project.project_data, Dirs.path_armature_components, 'Detections_Armature_Components.json')
    
    t1_stop = perf_counter()
    logger.info(f"[Processing armature components elapsed time] {round(t1_stop - t1_start)} seconds")
    torch.cuda.empty_cache()
    return Project


''' RUN IN PARALLEL'''
def run_in_parallel(weights, source, project, name, imgsz, nosave, anno_type, conf_thres, line_thickness, ignore_objects_for_overlay, mode, LOGGER, chunk, n_workers):
    num_files = len(os.listdir(source))
    LOGGER.info(f"The number of worker threads: ({n_workers}), number of files ({num_files}).")

    chunk_size = len(os.listdir(source)) // n_workers
    start = chunk * chunk_size
    end = start + chunk_size if chunk < (n_workers-1) else len(os.listdir(source))

    sub_source = [os.path.join(source, f) for f in os.listdir(source)[start:end] if f.lower().endswith('.jpg')]

    run(weights=weights,
        source=sub_source,
        project=project,
        name=name,
        imgsz=imgsz,
        nosave=nosave,
        anno_type=anno_type,
        conf_thres=conf_thres,
        ignore_objects_for_overlay=ignore_objects_for_overlay,
        mode=mode,
        LOGGER=LOGGER)

''' RUN IN PARALLEL'''


###### Multi-thread NOTE this works, but unless there are several thousand images, it will be slower
def process_file(logger, file, dir_components, component, Project, lock):
    file_name = str(file.split('.')[0])
    with open(os.path.join(dir_components, file), "r") as f:
        with lock:
            Project.project_data[file_name][component] = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
            try:
                image_path = glob.glob(os.path.join(Project.dir_images, file_name + '.*'))[0]
                name_ext = os.path.basename(image_path)
                with Image.open(image_path) as im:
                    _, ext = os.path.splitext(name_ext)
                    if ext not in ['.jpg']:
                        im = im.convert('RGB')
                        im.save(os.path.join(Project.dir_images, file_name) + '.jpg', quality=100)
                        # file_name += '.jpg'
                    width, height = im.size
            except Exception as e:
                print(f"Unable to get image dimensions. Error: {e}")
                logger.info(f"Unable to get image dimensions. Error: {e}")
                width, height = None, None
            if width and height:
                Project.project_data[file_name]['height'] = int(height)
                Project.project_data[file_name]['width'] = int(width)


def create_dictionary_from_txt_parallel(logger, cfg, dir_components, component, Project):
    if cfg['leafmachine']['project']['num_workers'] is None:
        num_workers = 4 
    else:
        num_workers = int(cfg['leafmachine']['project']['num_workers'])

    files = [file for file in os.listdir(dir_components) if file.endswith(".txt")]
    lock = Lock()
    with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
        futures = []
        for file in files:
            futures.append(executor.submit(process_file, logger, file, dir_components, component, Project, lock))
        for future in concurrent.futures.as_completed(futures):
            pass
    return Project.project_data

######





# Single threaded
def create_dictionary_from_txt(logger, dir_components, component, Project):
    # dict_labels = {}
    for file in tqdm(os.listdir(dir_components), desc="Loading Annotations", colour='green'):
        if file.endswith(".txt"):
            file_name = str(file.split('.')[0])
            with open(os.path.join(dir_components, file), "r") as f:
                # dict_labels[file] = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
                Project.project_data[file_name][component] = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
                try:
                    image_path = glob.glob(os.path.join(Project.dir_images, file_name + '.*'))[0]
                    name_ext = os.path.basename(image_path)
                    with Image.open(image_path) as im:
                        _, ext = os.path.splitext(name_ext)
                        if ext not in ['.jpg']:
                            im = im.convert('RGB')
                            im.save(os.path.join(Project.dir_images, file_name) + '.jpg', quality=100)
                            # file_name += '.jpg'
                        width, height = im.size
                except Exception as e:
                    # print(f"Unable to get image dimensions. Error: {e}")
                    logger.info(f"Unable to get image dimensions. Error: {e}")
                    width, height = None, None
                if width and height:
                    Project.project_data[file_name]['height'] = int(height)
                    Project.project_data[file_name]['width'] = int(width)
    # for key, value in dict_labels.items():
    #     print(f'{key}  --> {value}')
    return Project.project_data


# old below   
'''def create_dictionary_from_txt(dir_components, component, Project):
    # dict_labels = {}
    for file in os.listdir(dir_components):
        if file.endswith(".txt"):
            file_name = str(file.split('.')[0])
            with open(os.path.join(dir_components, file), "r") as f:
                # dict_labels[file] = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
                Project.project_data[file_name][component] = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
                try:
                    width, height = imagesize.get(os.path.join(Project.dir_images, '.'.join([file_name,'jpg'])))
                except Exception as e:
                    print(f"Image not in 'jpg' format. Trying 'jpeg'. Note that other formats are not supported.{e}")
                    width, height = imagesize.get(os.path.join(Project.dir_images, '.'.join([file_name,'jpeg'])))
                Project.project_data[file_name]['height'] = int(height)
                Project.project_data[file_name]['width'] = int(width)
    # for key, value in dict_labels.items():
    #     print(f'{key}  --> {value}')
    return Project.project_data'''



def dict_to_json(dict_labels, dir_components, name_json):
    dir_components = os.path.join(dir_components, 'JSON')
    with open(os.path.join(dir_components, name_json), "w") as outfile:
        json.dump(dict_labels, outfile)

def fetch_labels(dir_exisiting_labels, new_dir):
    shutil.copytree(dir_exisiting_labels, new_dir)


'''Landmarks - uses YOLO, but works differently than above. A hybrid between segmentation and component detector'''
def detect_landmarks(cfg, logger, dir_home, Project, batch, n_batches, Dirs, segmentation_complete):
    start_t = perf_counter()
    logger.name = f'[BATCH {batch+1} Detect Landmarks]'
    logger.info(f'Detecting landmarks for batch {batch+1} of {n_batches}')

    landmark_whole_leaves = cfg['leafmachine']['landmark_detector']['landmark_whole_leaves']
    landmark_partial_leaves = cfg['leafmachine']['landmark_detector']['landmark_partial_leaves']

    landmarks_whole_leaves_props = {}
    landmarks_whole_leaves_overlay = {}
    landmarks_partial_leaves_props = {}
    landmarks_partial_leaves_overlay = {}

    if landmark_whole_leaves:
        run_landmarks(cfg, logger, dir_home, Project, batch, n_batches, Dirs, 'Landmarks_Whole_Leaves', segmentation_complete)
    if landmark_partial_leaves:
        run_landmarks(cfg, logger, dir_home, Project, batch, n_batches, Dirs, 'Landmarks_Partial_Leaves', segmentation_complete)

    # if cfg['leafmachine']['leaf_segmentation']['segment_whole_leaves']:
    #     landmarks_whole_leaves_props_batch, landmarks_whole_leaves_overlay_batch = run_landmarks(Instance_Detector_Whole, Project.project_data_list[batch], 0, 
    #                                                                                 "Segmentation_Whole_Leaf", "Whole_Leaf_Cropped", cfg, Project, Dirs, batch, n_batches)#, start+1, end)
    #     landmarks_whole_leaves_props.update(landmarks_whole_leaves_props_batch)
    #     landmarks_whole_leaves_overlay.update(landmarks_whole_leaves_overlay_batch)
    # if cfg['leafmachine']['leaf_segmentation']['segment_partial_leaves']:
    #     landmarks_partial_leaves_props_batch, landmarks_partial_leaves_overlay_batch = run_landmarks(Instance_Detector_Partial, Project.project_data_list[batch], 1, 
    #                                                                                 "Segmentation_Partial_Leaf", "Partial_Leaf_Cropped", cfg, Project, Dirs, batch, n_batches)#, start+1, end)
    #     landmarks_partial_leaves_props.update(landmarks_partial_leaves_props_batch)
    #     landmarks_partial_leaves_overlay.update(landmarks_partial_leaves_overlay_batch)
    
    end_t = perf_counter()
    logger.info(f'Batch {batch+1}/{n_batches}: Landmark Detection Duration --> {round((end_t - start_t)/60)} minutes')
    return Project


def detect_armature(cfg, logger, dir_home, Project, batch, n_batches, Dirs, segmentation_complete):
    start_t = perf_counter()
    logger.name = f'[BATCH {batch+1} Detect Armature]'
    logger.info(f'Detecting armature for batch {batch+1} of {n_batches}')

    landmark_armature = cfg['leafmachine']['modules']['armature']

    landmarks_armature_props = {}
    landmarks_armature_overlay = {}

    if landmark_armature:
        run_armature(cfg, logger, dir_home, Project, batch, n_batches, Dirs, 'Landmarks_Armature', segmentation_complete)

    end_t = perf_counter()
    logger.info(f'Batch {batch+1}/{n_batches}: Armature Detection Duration --> {round((end_t - start_t)/60)} minutes')
    return Project


def run_armature(cfg, logger, dir_home, Project, batch, n_batches, Dirs, leaf_type, segmentation_complete):
    
    logger.info('Detecting armature landmarks from scratch')
    if leaf_type == 'Landmarks_Armature':
        dir_overlay = os.path.join(Dirs.landmarks_armature_overlay, ''.join(['batch_',str(batch+1)]))

    # if not segmentation_complete: # If segmentation was run, then don't redo the unpack, just do the crop into the temp folder
    if leaf_type == 'Landmarks_Armature': # TODO THE 0 is for prickles. For spines I'll need to add a 1 like with partial_leaves or just do it for all
        Project.project_data_list[batch] = unpack_class_from_components_armature(Project.project_data_list[batch], 0, 'Armature_YOLO', 'Armature_BBoxes', Project)
        Project.project_data_list[batch], dir_temp = crop_images_to_bbox_armature(Project.project_data_list[batch], 0, 'Armature_Cropped', "Armature_BBoxes", Project, Dirs, True, cfg)

    # Weights folder base
    dir_weights = os.path.join(dir_home, 'leafmachine2', 'component_detector','runs','train')
    
    # Detection threshold
    threshold = cfg['leafmachine']['landmark_detector_armature']['minimum_confidence_threshold']

    detector_version = cfg['leafmachine']['landmark_detector_armature']['detector_version']
    detector_iteration = cfg['leafmachine']['landmark_detector_armature']['detector_iteration']
    detector_weights = cfg['leafmachine']['landmark_detector_armature']['detector_weights']
    weights =  os.path.join(dir_weights,'Landmark_Detector_YOLO',detector_version,detector_iteration,'weights',detector_weights)

    do_save_prediction_overlay_images = not cfg['leafmachine']['landmark_detector_armature']['do_save_prediction_overlay_images']
    ignore_objects = cfg['leafmachine']['landmark_detector_armature']['ignore_objects_for_overlay']
    ignore_objects = ignore_objects or []
    if cfg['leafmachine']['project']['num_workers'] is None:
        num_workers = 1
    else:
        num_workers = int(cfg['leafmachine']['project']['num_workers'])

    has_images = False
    if len(os.listdir(dir_temp)) > 0:
        has_images = True
        source = dir_temp
        project = dir_overlay
        name = Dirs.run_name
        imgsz = (1280, 1280)
        nosave = do_save_prediction_overlay_images
        anno_type = 'Armature_Detector'
        conf_thres = threshold
        line_thickness = 2
        ignore_objects_for_overlay = ignore_objects
        mode = 'Landmark'
        LOGGER = logger

        # Initialize a Lock object to ensure thread safety
        lock = Lock()

        with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
            futures = [executor.submit(run_in_parallel, weights, source, project, name, imgsz, nosave, anno_type,
                                    conf_thres, line_thickness, ignore_objects_for_overlay, mode, LOGGER, i, num_workers) for i in
                    range(num_workers)]
            for future in concurrent.futures.as_completed(futures):
                try:
                    _ = future.result()
                except Exception as e:
                    logger.error(f'Error in thread: {e}')
                    continue

        with lock:
            if has_images:
                dimensions_dict = get_cropped_dimensions(dir_temp)
                A = add_to_dictionary_from_txt_armature(cfg, logger, Dirs, leaf_type, os.path.join(dir_overlay, 'labels'), leaf_type, Project, dimensions_dict, dir_temp, batch, n_batches)
            else:
                # TODO add empty placeholder to the image data
                pass
    
    # delete the temp dir
    try:
        shutil.rmtree(dir_temp)
    except:
        try:
            time.sleep(5)
            shutil.rmtree(dir_temp)
        except:
            try:
                time.sleep(5)
                shutil.rmtree(dir_temp)
            except:
                pass

    torch.cuda.empty_cache()

    return Project


def run_landmarks(cfg, logger, dir_home, Project, batch, n_batches, Dirs, leaf_type, segmentation_complete):
    use_existing_landmark_detections = cfg['leafmachine']['landmark_detector']['use_existing_landmark_detections']
    
    if use_existing_landmark_detections is None:
        logger.info('Detecting landmarks from scratch')
        if leaf_type == 'Landmarks_Whole_Leaves':
            dir_overlay = os.path.join(Dirs.landmarks_whole_leaves_overlay, ''.join(['batch_',str(batch+1)]))
        elif leaf_type == 'Landmarks_Partial_Leaves':
            dir_overlay = os.path.join(Dirs.landmarks_partial_leaves_overlay, ''.join(['batch_',str(batch+1)]))

        # if not segmentation_complete: # If segmentation was run, then don't redo the unpack, just do the crop into the temp folder
        if leaf_type == 'Landmarks_Whole_Leaves':
            Project.project_data_list[batch] = unpack_class_from_components(Project.project_data_list[batch], 0, 'Whole_Leaf_BBoxes_YOLO', 'Whole_Leaf_BBoxes', Project)
            Project.project_data_list[batch], dir_temp = crop_images_to_bbox(Project.project_data_list[batch], 0, 'Whole_Leaf_Cropped', "Whole_Leaf_BBoxes", Project, Dirs)

        elif leaf_type == 'Landmarks_Partial_Leaves':
            Project.project_data_list[batch] = unpack_class_from_components(Project.project_data_list[batch], 1, 'Partial_Leaf_BBoxes_YOLO', 'Partial_Leaf_BBoxes', Project)
            Project.project_data_list[batch], dir_temp = crop_images_to_bbox(Project.project_data_list[batch], 1, 'Partial_Leaf_Cropped', "Partial_Leaf_BBoxes", Project, Dirs)
        # else:
        #     if leaf_type == 'Landmarks_Whole_Leaves':
        #         Project.project_data_list[batch], dir_temp = crop_images_to_bbox(Project.project_data_list[batch], 0, 'Whole_Leaf_Cropped', "Whole_Leaf_BBoxes", Project, Dirs)
        #     elif leaf_type == 'Landmarks_Partial_Leaves':
        #         Project.project_data_list[batch], dir_temp = crop_images_to_bbox(Project.project_data_list[batch], 1, 'Partial_Leaf_Cropped', "Partial_Leaf_BBoxes", Project, Dirs)

        # Weights folder base
        dir_weights = os.path.join(dir_home, 'leafmachine2', 'component_detector','runs','train')
        
        # Detection threshold
        threshold = cfg['leafmachine']['landmark_detector']['minimum_confidence_threshold']

        detector_version = cfg['leafmachine']['landmark_detector']['detector_version']
        detector_iteration = cfg['leafmachine']['landmark_detector']['detector_iteration']
        detector_weights = cfg['leafmachine']['landmark_detector']['detector_weights']
        weights =  os.path.join(dir_weights,'Landmark_Detector_YOLO',detector_version,detector_iteration,'weights',detector_weights)

        do_save_prediction_overlay_images = not cfg['leafmachine']['landmark_detector']['do_save_prediction_overlay_images']
        ignore_objects = cfg['leafmachine']['landmark_detector']['ignore_objects_for_overlay']
        ignore_objects = ignore_objects or []
        if cfg['leafmachine']['project']['num_workers'] is None:
            num_workers = 1
        else:
            num_workers = int(cfg['leafmachine']['project']['num_workers'])

        has_images = False
        if len(os.listdir(dir_temp)) > 0:
            has_images = True
            # run(weights = weights,
            #     source = dir_temp,
            #     project = dir_overlay,
            #     name = Dirs.run_name,
            #     imgsz = (1280, 1280),
            #     nosave = do_save_prediction_overlay_images,
            #     anno_type = 'Landmark_Detector_YOLO',
            #     conf_thres = threshold, 
            #     line_thickness = 2,
            #     ignore_objects_for_overlay = ignore_objects,
            #     mode = 'Landmark')
            source = dir_temp
            project = dir_overlay
            name = Dirs.run_name
            imgsz = (1280, 1280)
            nosave = do_save_prediction_overlay_images
            anno_type = 'Landmark_Detector'
            conf_thres = threshold
            line_thickness = 2
            ignore_objects_for_overlay = ignore_objects
            mode = 'Landmark'
            LOGGER = logger

            # Initialize a Lock object to ensure thread safety
            lock = Lock()

            with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
                futures = [executor.submit(run_in_parallel, weights, source, project, name, imgsz, nosave, anno_type,
                                        conf_thres, line_thickness, ignore_objects_for_overlay, mode, LOGGER, i, num_workers) for i in
                        range(num_workers)]
                for future in concurrent.futures.as_completed(futures):
                    try:
                        _ = future.result()
                    except Exception as e:
                        logger.error(f'Error in thread: {e}')
                        continue

            with lock:
                if has_images:
                    dimensions_dict = get_cropped_dimensions(dir_temp)
                    A = add_to_dictionary_from_txt(cfg, logger, Dirs, leaf_type, os.path.join(dir_overlay, 'labels'), leaf_type, Project, dimensions_dict, dir_temp, batch, n_batches)
                else:
                    # TODO add empty placeholder to the image data
                    pass
    else:
        logger.info('Loading existing landmark annotations')
        dir_temp = os.path.join(use_existing_landmark_detections, f'batch_{str(batch+1)}', 'labels')
        dimensions_dict = get_cropped_dimensions(dir_temp)
        A = add_to_dictionary_from_txt(cfg, logger, Dirs, leaf_type, use_existing_landmark_detections, leaf_type, Project, dimensions_dict, dir_temp, batch, n_batches)

    
    # delete the temp dir
    try:
        shutil.rmtree(dir_temp)
    except:
        try:
            time.sleep(5)
            shutil.rmtree(dir_temp)
        except:
            try:
                time.sleep(5)
                shutil.rmtree(dir_temp)
            except:
                pass

    torch.cuda.empty_cache()

    return Project
    '''def add_to_dictionary_from_txt(cfg, Dirs, leaf_type, dir_components, component, Project, dimensions_dict, dir_temp):
    # dict_labels = {}
    for file in os.listdir(dir_components):
        file_name = str(file.split('.')[0])
        file_name_parent = file_name.split('__')[0]
        Project.project_data[file_name_parent][component] = {}

        if file.endswith(".txt"):
            with open(os.path.join(dir_components, file), "r") as f:
                all_points = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
                Project.project_data[file_name_parent][component][file_name] = all_points
                
                height = dimensions_dict[file_name][0]
                width = dimensions_dict[file_name][1]

                Leaf_Skeleton = LeafSkeleton(cfg, Dirs, leaf_type, all_points, height, width, dir_temp, file_name)
                QC_add = Leaf_Skeleton.get_QC()'''


    return Project.project_data

def add_to_dictionary_from_txt_armature(cfg, logger, Dirs, leaf_type, dir_components, component, Project, dimensions_dict, dir_temp, batch, n_batches):
    dpi = cfg['leafmachine']['overlay']['overlay_dpi']
    if leaf_type == 'Landmarks_Armature':
        logger.info(f'Detecting landmarks armature')
        pdf_path = os.path.join(Dirs.landmarks_armature_overlay_QC, ''.join(['landmarks_armature_overlay_QC__',str(batch+1), 'of', str(n_batches), '.pdf']))
        pdf_path_final = os.path.join(Dirs.landmarks_armature_overlay_final, ''.join(['landmarks_armature_overlay_final__',str(batch+1), 'of', str(n_batches), '.pdf']))

    ### FINAL
    # dict_labels = {}
    fig = plt.figure(figsize=(8.27, 11.69), dpi=dpi) # A4 size, 300 dpi
    row, col = 0, 0
    with PdfPages(pdf_path_final) as pdf:
        
        

        for file in os.listdir(dir_components):
            file_name = str(file.split('.')[0])
            file_name_parent = file_name.split('__')[0]

            # Project.project_data_list[batch][file_name_parent][component] = []

            if file_name_parent in Project.project_data_list[batch]:

                

                if file.endswith(".txt"):
                    with open(os.path.join(dir_components, file), "r") as f:
                        all_points = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
                        # Project.project_data_list[batch][file_name_parent][component][file_name] = all_points

                        height = dimensions_dict[file_name][0]
                        width = dimensions_dict[file_name][1]

                        Armature_Skeleton = ArmatureSkeleton(cfg, logger, Dirs, leaf_type, all_points, height, width, dir_temp, file_name)
                        Project = add_armature_skeleton_to_project(cfg, logger, Project, batch, file_name_parent, component, Dirs, leaf_type, all_points, height, width, dir_temp, file_name, Armature_Skeleton)
                        final_add = cv2.cvtColor(Armature_Skeleton.get_final(), cv2.COLOR_BGR2RGB)

                        # Add image to the current subplot
                        ax = fig.add_subplot(5, 3, row * 3 + col + 1)
                        ax.imshow(final_add)
                        ax.axis('off')

                        col += 1
                        if col == 3:
                            col = 0
                            row += 1
                        if row == 5:
                            row = 0
                            pdf.savefig(fig)  # Save the current page
                            fig = plt.figure(figsize=(8.27, 11.69), dpi=300) # Create a new page
            else:
                pass

        if row != 0 or col != 0:
            pdf.savefig(fig)  # Save the remaining images on the last page

def add_to_dictionary_from_txt(cfg, logger, Dirs, leaf_type, dir_components, component, Project, dimensions_dict, dir_temp, batch, n_batches):
    dpi = cfg['leafmachine']['overlay']['overlay_dpi']
    if leaf_type == 'Landmarks_Whole_Leaves':
        logger.info(f'Detecting landmarks whole leaves')
        pdf_path = os.path.join(Dirs.landmarks_whole_leaves_overlay_QC, ''.join(['landmarks_whole_leaves_overlay_QC__',str(batch+1), 'of', str(n_batches), '.pdf']))
        pdf_path_final = os.path.join(Dirs.landmarks_whole_leaves_overlay_final, ''.join(['landmarks_whole_leaves_overlay_final__',str(batch+1), 'of', str(n_batches), '.pdf']))
    elif leaf_type == 'Landmarks_Partial_Leaves':
        logger.info(f'Detecting landmarks partial leaves')
        pdf_path = os.path.join(Dirs.landmarks_partial_leaves_overlay_QC, ''.join(['landmarks_partial_leaves_overlay_QC__',str(batch+1), 'of', str(n_batches), '.pdf']))
        pdf_path_final = os.path.join(Dirs.landmarks_partial_leaves_overlay_final, ''.join(['landmarks_partial_leaves_overlay_final__',str(batch+1), 'of', str(n_batches), '.pdf']))
    elif leaf_type == 'Landmarks_Armature':
        logger.info(f'Detecting landmarks armature')
        pdf_path = os.path.join(Dirs.landmarks_armature_overlay_QC, ''.join(['landmarks_armature_overlay_QC__',str(batch+1), 'of', str(n_batches), '.pdf']))
        pdf_path_final = os.path.join(Dirs.landmarks_armature_overlay_final, ''.join(['landmarks_armature_overlay_final__',str(batch+1), 'of', str(n_batches), '.pdf']))

    ### FINAL
    # dict_labels = {}
    fig = plt.figure(figsize=(8.27, 11.69), dpi=dpi) # A4 size, 300 dpi
    row, col = 0, 0
    with PdfPages(pdf_path_final) as pdf:
        
        

        for file in os.listdir(dir_components):
            file_name = str(file.split('.')[0])
            file_name_parent = file_name.split('__')[0]

            # Project.project_data_list[batch][file_name_parent][component] = []

            if file_name_parent in Project.project_data_list[batch]:

                

                if file.endswith(".txt"):
                    with open(os.path.join(dir_components, file), "r") as f:
                        all_points = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
                        # Project.project_data_list[batch][file_name_parent][component][file_name] = all_points

                        height = dimensions_dict[file_name][0]
                        width = dimensions_dict[file_name][1]

                        Leaf_Skeleton = LeafSkeleton(cfg, logger, Dirs, leaf_type, all_points, height, width, dir_temp, file_name)
                        Project = add_leaf_skeleton_to_project(cfg, logger, Project, batch, file_name_parent, component, Dirs, leaf_type, all_points, height, width, dir_temp, file_name, Leaf_Skeleton)
                        final_add = cv2.cvtColor(Leaf_Skeleton.get_final(), cv2.COLOR_BGR2RGB)

                        # Add image to the current subplot
                        ax = fig.add_subplot(5, 3, row * 3 + col + 1)
                        ax.imshow(final_add)
                        ax.axis('off')

                        col += 1
                        if col == 3:
                            col = 0
                            row += 1
                        if row == 5:
                            row = 0
                            pdf.savefig(fig)  # Save the current page
                            fig = plt.figure(figsize=(8.27, 11.69), dpi=300) # Create a new page
            else:
                pass

        if row != 0 or col != 0:
            pdf.savefig(fig)  # Save the remaining images on the last page

    ### QC
    '''do_save_QC_pdf = False # TODO refine this
    if do_save_QC_pdf:
        # dict_labels = {}
        fig = plt.figure(figsize=(8.27, 11.69), dpi=dpi) # A4 size, 300 dpi
        row, col = 0, 0
        with PdfPages(pdf_path) as pdf:



            for file in os.listdir(dir_components):
                file_name = str(file.split('.')[0])
                file_name_parent = file_name.split('__')[0]

                if file_name_parent in Project.project_data_list[batch]:

                    if file.endswith(".txt"):
                        with open(os.path.join(dir_components, file), "r") as f:
                            all_points = [[int(line.split()[0])] + list(map(float, line.split()[1:])) for line in f]
                            Project.project_data_list[batch][file_name_parent][component][file_name] = all_points

                            height = dimensions_dict[file_name][0]
                            width = dimensions_dict[file_name][1]

                            Leaf_Skeleton = LeafSkeleton(cfg, logger, Dirs, leaf_type, all_points, height, width, dir_temp, file_name)
                            QC_add = cv2.cvtColor(Leaf_Skeleton.get_QC(), cv2.COLOR_BGR2RGB)

                            # Add image to the current subplot
                            ax = fig.add_subplot(5, 3, row * 3 + col + 1)
                            ax.imshow(QC_add)
                            ax.axis('off')

                            col += 1
                            if col == 3:
                                col = 0
                                row += 1
                            if row == 5:
                                row = 0
                                pdf.savefig(fig)  # Save the current page
                                fig = plt.figure(figsize=(8.27, 11.69), dpi=300) # Create a new page
                else:
                    pass

            if row != 0 or col != 0:
                pdf.savefig(fig)  # Save the remaining images on the last page'''


def add_armature_skeleton_to_project(cfg, logger, Project, batch, file_name_parent, component, Dirs, leaf_type, all_points, height, width, dir_temp, file_name, ARM):
    if ARM.is_complete:
        try:
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'armature_status': 'complete'}, {'armature': ARM}]})
        except:
            Project.project_data_list[batch][file_name_parent][component] = []
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'armature_status': 'complete'}, {'armature': ARM}]})

    else:
        try:
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'armature_status': 'incomplete'}, {'armature': ARM}]})
        except:
            Project.project_data_list[batch][file_name_parent][component] = []
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'armature_status': 'incomplete'}, {'armature': ARM}]})


    return Project


def add_leaf_skeleton_to_project(cfg, logger, Project, batch, file_name_parent, component, Dirs, leaf_type, all_points, height, width, dir_temp, file_name, LS):

    if LS.is_complete_leaf:
        try:
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'landmark_status': 'complete_leaf'}, {'landmarks': LS}]})
        except:
            Project.project_data_list[batch][file_name_parent][component] = []
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'landmark_status': 'complete_leaf'}, {'landmarks': LS}]})
        # Project.project_data_list[batch][file_name_parent][component][file_name].update({'landmark_status': 'complete_leaf'})
        # Project.project_data_list[batch][file_name_parent][component][file_name].update({'landmarks': LS})

    elif LS.is_leaf_no_width:
        try:
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'landmark_status': 'leaf_no_width'}, {'landmarks': LS}]})
        except:
            Project.project_data_list[batch][file_name_parent][component] = []
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'landmark_status': 'leaf_no_width'}, {'landmarks': LS}]})
        # Project.project_data_list[batch][file_name_parent][component][file_name].update({'landmark_status': 'leaf_no_width'})
        # Project.project_data_list[batch][file_name_parent][component][file_name].update({'landmarks': LS})

    else:
        try:
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'landmark_status': 'incomplete'}, {'landmarks': LS}]})
        except:
            Project.project_data_list[batch][file_name_parent][component] = []
            Project.project_data_list[batch][file_name_parent][component].append({file_name: [{'landmark_status': 'incomplete'}, {'landmarks': LS}]})

        # Project.project_data_list[batch][file_name_parent][component][file_name].update({'landmark_status': 'incomplete'})
        # Project.project_data_list[batch][file_name_parent][component][file_name].update({'landmarks': LS})

    return Project


'''
self.determine_lamina_length('final') 

# Lamina tip and base
if self.has_lamina_tip:
    cv2.circle(self.image_final, self.lamina_tip, radius=4, color=(0, 255, 0), thickness=2)
    cv2.circle(self.image_final, self.lamina_tip, radius=2, color=(255, 255, 255), thickness=-1)
if self.has_lamina_base:
    cv2.circle(self.image_final, self.lamina_base, radius=4, color=(255, 0, 0), thickness=2)
    cv2.circle(self.image_final, self.lamina_base, radius=2, color=(255, 255, 255), thickness=-1)

# Apex angle
# if self.apex_center != []:
#     cv2.circle(self.image_final, self.apex_center, radius=3, color=(0, 255, 0), thickness=-1)
if self.apex_left != []:
    cv2.circle(self.image_final, self.apex_left, radius=3, color=(255, 0, 0), thickness=-1)
if self.apex_right != []:
    cv2.circle(self.image_final, self.apex_right, radius=3, color=(0, 0, 255), thickness=-1)

# Base angle
# if self.base_center:
#     cv2.circle(self.image_final, self.base_center, radius=3, color=(0, 255, 0), thickness=-1)
if self.base_left:
    cv2.circle(self.image_final, self.base_left, radius=3, color=(255, 0, 0), thickness=-1)
if self.base_right:
    cv2.circle(self.image_final, self.base_right, radius=3, color=(0, 0, 255), thickness=-1)

# Draw line of fit
for point in self.width_infer:


'''









def get_cropped_dimensions(dir_temp):
    dimensions_dict = {}
    for file_name in os.listdir(dir_temp):
        if file_name.endswith(".jpg"):
            img = cv2.imread(os.path.join(dir_temp, file_name))
            height, width, channels = img.shape
            stem = os.path.splitext(file_name)[0]
            dimensions_dict[stem] = (height, width)
    return dimensions_dict

def unpack_class_from_components_armature(dict_big, cls, dict_name_yolo, dict_name_location, Project):
    # Get the dict that contains plant parts, find the whole leaves
    for filename, value in dict_big.items():
        if "Detections_Armature_Components" in value:
            filtered_components = [val for val in value["Detections_Armature_Components"] if val[0] == cls]
            value[dict_name_yolo] = filtered_components

    for filename, value in dict_big.items():
        if "Detections_Armature_Components" in value:
            filtered_components = [val for val in value["Detections_Armature_Components"] if val[0] == cls]
            height = value['height']
            width = value['width']
            converted_list = [[convert_index_to_class_armature(val[0]), int((val[1] * width) - ((val[3] * width) / 2)), 
                                                                int((val[2] * height) - ((val[4] * height) / 2)), 
                                                                int(val[3] * width) + int((val[1] * width) - ((val[3] * width) / 2)), 
                                                                int(val[4] * height) + int((val[2] * height) - ((val[4] * height) / 2))] for val in filtered_components]
            # Verify that the crops are correct
            # img = Image.open(os.path.join(Project., '.'.join([filename,'jpg'])))
            # for d in converted_list:
            #     img_crop = img.crop((d[1], d[2], d[3], d[4]))
            #     img_crop.show() 
            value[dict_name_location] = converted_list
    # print(dict)
    return dict_big

def unpack_class_from_components(dict_big, cls, dict_name_yolo, dict_name_location, Project):
    # Get the dict that contains plant parts, find the whole leaves
    for filename, value in dict_big.items():
        if "Detections_Plant_Components" in value:
            filtered_components = [val for val in value["Detections_Plant_Components"] if val[0] == cls]
            value[dict_name_yolo] = filtered_components

    for filename, value in dict_big.items():
        if "Detections_Plant_Components" in value:
            filtered_components = [val for val in value["Detections_Plant_Components"] if val[0] == cls]
            height = value['height']
            width = value['width']
            converted_list = [[convert_index_to_class(val[0]), int((val[1] * width) - ((val[3] * width) / 2)), 
                                                                int((val[2] * height) - ((val[4] * height) / 2)), 
                                                                int(val[3] * width) + int((val[1] * width) - ((val[3] * width) / 2)), 
                                                                int(val[4] * height) + int((val[2] * height) - ((val[4] * height) / 2))] for val in filtered_components]
            # Verify that the crops are correct
            # img = Image.open(os.path.join(Project., '.'.join([filename,'jpg'])))
            # for d in converted_list:
            #     img_crop = img.crop((d[1], d[2], d[3], d[4]))
            #     img_crop.show() 
            value[dict_name_location] = converted_list
    # print(dict)
    return dict_big


def crop_images_to_bbox_armature(dict_big, cls, dict_name_cropped, dict_from, Project, Dirs, do_upscale=False, cfg=None):
    dir_temp = os.path.join(Dirs.landmarks, 'TEMP_landmarks')
    os.makedirs(dir_temp, exist_ok=True)
    # For each image, iterate through the whole leaves, segment, report data back to dict_plant_components
    for filename, value in dict_big.items():
        value[dict_name_cropped] = []
        if dict_from in value:
            bboxes_whole_leaves = [val for val in value[dict_from] if val[0] == convert_index_to_class_armature(cls)]
            if len(bboxes_whole_leaves) == 0:
                m = str(''.join(['No objects for class ', convert_index_to_class_armature(0), ' were found']))
                # Print_Verbose(cfg, 3, m).plain()
            else:
                try:
                    img = cv2.imread(os.path.join(Project.dir_images, '.'.join([filename,'jpg'])))
                    # img = cv2.imread(os.path.join(Project, '.'.join([filename,'jpg']))) # Testing
                except:
                    img = cv2.imread(os.path.join(Project.dir_images, '.'.join([filename,'jpeg'])))
                    # img = cv2.imread(os.path.join(Project, '.'.join([filename,'jpeg']))) # Testing
                
                for d in bboxes_whole_leaves:
                    # img_crop = img.crop((d[1], d[2], d[3], d[4])) # PIL
                    img_crop = img[d[2]:d[4], d[1]:d[3]]
                    loc = '-'.join([str(d[1]), str(d[2]), str(d[3]), str(d[4])])
                    # value[dict_name_cropped].append({crop_name: img_crop})
                    if do_upscale:
                        upscale_factor = int(cfg['leafmachine']['landmark_detector_armature']['upscale_factor'])
                        if cls == 0:
                            crop_name = '__'.join([filename,f"PRICKLE-{upscale_factor}x",loc])
                        height, width, _ = img_crop.shape
                        img_crop = cv2.resize(img_crop, ((width * upscale_factor), (height * upscale_factor)), interpolation=cv2.INTER_LANCZOS4)
                    else:
                        if cls == 0:
                            crop_name = '__'.join([filename,'PRICKLE',loc])

                    cv2.imwrite(os.path.join(dir_temp, '.'.join([crop_name,'jpg'])), img_crop)
                    # cv2.imshow('img_crop', img_crop)
                    # cv2.waitKey(0)
                    # img_crop.show() # PIL
    return dict_big, dir_temp


def crop_images_to_bbox(dict_big, cls, dict_name_cropped, dict_from, Project, Dirs):
    dir_temp = os.path.join(Dirs.landmarks, 'TEMP_landmarks')
    os.makedirs(dir_temp, exist_ok=True)
    # For each image, iterate through the whole leaves, segment, report data back to dict_plant_components
    for filename, value in dict_big.items():
        value[dict_name_cropped] = []
        if dict_from in value:
            bboxes_whole_leaves = [val for val in value[dict_from] if val[0] == convert_index_to_class(cls)]
            if len(bboxes_whole_leaves) == 0:
                m = str(''.join(['No objects for class ', convert_index_to_class(0), ' were found']))
                # Print_Verbose(cfg, 3, m).plain()
            else:
                try:
                    img = cv2.imread(os.path.join(Project.dir_images, '.'.join([filename,'jpg'])))
                    # img = cv2.imread(os.path.join(Project, '.'.join([filename,'jpg']))) # Testing
                except:
                    img = cv2.imread(os.path.join(Project.dir_images, '.'.join([filename,'jpeg'])))
                    # img = cv2.imread(os.path.join(Project, '.'.join([filename,'jpeg']))) # Testing
                
                for d in bboxes_whole_leaves:
                    # img_crop = img.crop((d[1], d[2], d[3], d[4])) # PIL
                    img_crop = img[d[2]:d[4], d[1]:d[3]]
                    loc = '-'.join([str(d[1]), str(d[2]), str(d[3]), str(d[4])])
                    if cls == 0:
                        crop_name = '__'.join([filename,'L',loc])
                    elif cls == 1:
                        crop_name = '__'.join([filename,'PL',loc])
                    elif cls == 2:
                        crop_name = '__'.join([filename,'ARM',loc])
                    # value[dict_name_cropped].append({crop_name: img_crop})
                    cv2.imwrite(os.path.join(dir_temp, '.'.join([crop_name,'jpg'])), img_crop)
                    # cv2.imshow('img_crop', img_crop)
                    # cv2.waitKey(0)
                    # img_crop.show() # PIL
    return dict_big, dir_temp

def convert_index_to_class(ind):
    mapping = {
        0: 'apex_angle',
        1: 'base_angle',
        2: 'lamina_base',
        3: 'lamina_tip',
        4: 'lamina_width',
        5: 'lobe_tip',
        6: 'midvein_trace',
        7: 'petiole_tip',
        8: 'petiole_trace',
    }
    return mapping.get(ind, 'Invalid class').lower()

def convert_index_to_class_armature(ind):
    mapping = {
        0: 'tip',
        1: 'middle',
        2: 'outer',
    }
    return mapping.get(ind, 'Invalid class').lower()