File size: 9,751 Bytes
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
import os, yaml, platform

def get_default_download_folder():
    system_platform = platform.system()  # Gets the system platform, e.g., 'Linux', 'Windows', 'Darwin'

    if system_platform == "Windows":
        # Typically, the Downloads folder for Windows is in the user's profile folder
        default_output_folder = os.path.join(os.getenv('USERPROFILE'), 'Downloads')
    elif system_platform == "Darwin":
        # Typically, the Downloads folder for macOS is in the user's home directory
        default_output_folder = os.path.join(os.path.expanduser("~"), 'Downloads')
    elif system_platform == "Linux":
        # Typically, the Downloads folder for Linux is in the user's home directory
        default_output_folder = os.path.join(os.path.expanduser("~"), 'Downloads')
    else:
        default_output_folder = "set/path/to/downloads/folder"
        print("Please manually set the output folder")
    return default_output_folder

def build_LM2_config():
    dir_home = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))


    # Initialize the base structure
    config_data = {
        'leafmachine': {}
    }

    # Modular sections to be added to 'leafmachine'
    do_section = {
        'check_for_illegal_filenames': True,
        'check_for_corrupt_images_make_vertical': True,
        'run_leaf_processing': True
    }

    print_section = {
        'verbose': True,
        'optional_warnings': True
    }

    logging_section = {
        'log_level': None
    }

    default_output_folder = get_default_download_folder()
    project_section = {
        'dir_output': default_output_folder,
        # 'dir_output': 'D:/D_Desktop/LM2', 
        'run_name': 'test',
        'image_location': 'local',
        'GBIF_mode': 'all',
        'batch_size': 40,
        'num_workers': 2,
        'dir_images_local': '',
        # 'dir_images_local': 'D:\Dropbox\LM2_Env\Image_Datasets\Manuscript_Images',
        'path_combined_csv_local': None,
        'path_occurrence_csv_local': None,
        'path_images_csv_local': None,
        'use_existing_plant_component_detections': None,
        'use_existing_archival_component_detections': None,
        'process_subset_of_images': False,
        'dir_images_subset': '',
        'n_images_per_species': 10,
        'species_list': ''
    }

    cropped_components_section = {
        'do_save_cropped_annotations': False,
        'save_cropped_annotations': ['label'],
        'save_per_image': False,
        'save_per_annotation_class': True,
        'binarize_labels': False,
        'binarize_labels_skeletonize': False
    }

    modules_section = {
        'armature': False,
        'specimen_crop': False
    }

    data_section = {
        'save_json_rulers': False,
        'save_json_measurements': False,
        'save_individual_csv_files_rulers': False,
        'save_individual_csv_files_measurements': False,
        'save_individual_csv_files_landmarks': False,
        'save_individual_efd_files': False,
        'include_darwin_core_data_from_combined_file': False,
        'do_apply_conversion_factor': True
    }

    overlay_section = {
        'save_overlay_to_pdf': False,
        'save_overlay_to_jpgs': True,
        'overlay_dpi': 300, # Between 100 to 300
        'overlay_background_color': 'black', # Either 'white' or 'black'

        'show_archival_detections': True,
        'show_plant_detections': True,
        'show_segmentations': True,
        'show_landmarks': True,
        'ignore_archival_detections_classes': [],
        'ignore_plant_detections_classes': ['leaf_whole', 'specimen'], # Could also include 'leaf_partial' and others if needed
        'ignore_landmark_classes': [],

        'line_width_archival': 12, # Previous value given was 2
        'line_width_plant': 12, # Previous value given was 6
        'line_width_seg': 12, # 12 is specified as "thick"
        'line_width_efd': 12, # 3 is specified as "thick" but 12 is given here
        'alpha_transparency_archival': 0.3,
        'alpha_transparency_plant': 0,
        'alpha_transparency_seg_whole_leaf': 0.4,
        'alpha_transparency_seg_partial_leaf': 0.3
    }

    plant_component_detector_section = {
        'detector_type': 'Plant_Detector',
        'detector_version': 'PLANT_GroupAB_200',
        'detector_iteration': 'PLANT_GroupAB_200',
        'detector_weights': 'best.pt',
        'minimum_confidence_threshold': 0.3, # Default is 0.5
        'do_save_prediction_overlay_images': True,
        'ignore_objects_for_overlay': [] # 'leaf_partial' can be included if needed
    }

    archival_component_detector_section = {
        'detector_type': 'Archival_Detector',
        'detector_version': 'PREP_final',
        'detector_iteration': 'PREP_final',
        'detector_weights': 'best.pt',
        'minimum_confidence_threshold': 0.5, # Default is 0.5
        'do_save_prediction_overlay_images': True,
        'ignore_objects_for_overlay': []
    }

    armature_component_detector_section = {
        'detector_type': 'Armature_Detector',
        'detector_version': 'ARM_A_1000',
        'detector_iteration': 'ARM_A_1000',
        'detector_weights': 'best.pt',
        'minimum_confidence_threshold': 0.5, # Optionally: 0.2
        'do_save_prediction_overlay_images': True,
        'ignore_objects_for_overlay': []
    }

    landmark_detector_section = {
        'landmark_whole_leaves': True,
        'landmark_partial_leaves': False,
        'detector_type': 'Landmark_Detector_YOLO',
        'detector_version': 'Landmarks',
        'detector_iteration': 'Landmarks_V2',
        'detector_weights': 'best.pt',
        'minimum_confidence_threshold': 0.02,
        'do_save_prediction_overlay_images': True,
        'ignore_objects_for_overlay': [],
        'use_existing_landmark_detections': None, # Example path provided
        'do_show_QC_images': False,
        'do_save_QC_images': True,
        'do_show_final_images': False,
        'do_save_final_images': True
    }

    landmark_detector_armature_section = {
        'upscale_factor': 10,
        'detector_type': 'Landmark_Detector_YOLO',
        'detector_version': 'Landmarks_Arm_A_200',
        'detector_iteration': 'Landmarks_Arm_A_200',
        'detector_weights': 'last.pt',
        'minimum_confidence_threshold': 0.06,
        'do_save_prediction_overlay_images': True,
        'ignore_objects_for_overlay': [],
        'use_existing_landmark_detections': None, # Example path provided
        'do_show_QC_images': True,
        'do_save_QC_images': True,
        'do_show_final_images': True,
        'do_save_final_images': True
    }

    ruler_detection_section = {
        'detect_ruler_type': True,
        'ruler_detector': 'ruler_classifier_38classes_v-1.pt',
        'ruler_binary_detector': 'model_scripted_resnet_720_withCompression.pt',
        'minimum_confidence_threshold': 0.4,
        'save_ruler_validation': False,
        'save_ruler_validation_summary': True,
        'save_ruler_processed': False
    }

    leaf_segmentation_section = {
        'segment_whole_leaves': True,
        'segment_partial_leaves': False,

        'keep_only_best_one_leaf_one_petiole': True,

        'save_segmentation_overlay_images_to_pdf': True,
        'save_each_segmentation_overlay_image': True,
        'save_individual_overlay_images': True, # Not recommended due to potential file count
        'overlay_line_width': 1, # Default is 1

        'use_efds_for_png_masks': False, # Requires calculate_elliptic_fourier_descriptors to be True
        'save_masks_color': True,
        'save_full_image_masks_color': True,
        'save_rgb_cropped_images': True,

        'find_minimum_bounding_box': True,

        'calculate_elliptic_fourier_descriptors': True, # Default is True
        'elliptic_fourier_descriptor_order': 40, # Default is 40

        'segmentation_model': 'GroupB_Dataset_100000_Iter_1176PTS_512Batch_smooth_l1_LR00025_BGR',
        'minimum_confidence_threshold': 0.7, # Alternatively: 0.9
        'generate_overlay': True,
        'overlay_dpi': 300, # Range: 100 to 300
        'overlay_background_color': 'black' # Options: 'white' or 'black'
    }

    # Add the sections to the 'leafmachine' key
    config_data['leafmachine']['do'] = do_section
    config_data['leafmachine']['print'] = print_section
    config_data['leafmachine']['logging'] = logging_section
    config_data['leafmachine']['project'] = project_section
    config_data['leafmachine']['cropped_components'] = cropped_components_section
    config_data['leafmachine']['modules'] = modules_section
    config_data['leafmachine']['data'] = data_section
    config_data['leafmachine']['overlay'] = overlay_section
    config_data['leafmachine']['plant_component_detector'] = plant_component_detector_section
    config_data['leafmachine']['archival_component_detector'] = archival_component_detector_section
    config_data['leafmachine']['armature_component_detector'] = armature_component_detector_section
    config_data['leafmachine']['landmark_detector'] = landmark_detector_section
    config_data['leafmachine']['landmark_detector_armature'] = landmark_detector_armature_section
    config_data['leafmachine']['ruler_detection'] = ruler_detection_section
    config_data['leafmachine']['leaf_segmentation'] = leaf_segmentation_section

    return config_data, dir_home

def write_config_file(config_data, dir_home, filename="LeafMachine2.yaml"):
    file_path = os.path.join(dir_home, filename)

    # Write the data to a YAML file
    with open(file_path, "w") as outfile:
        yaml.dump(config_data, outfile, default_flow_style=False)

if __name__ == '__main__':
    config_data, dir_home = build_LM2_config()
    write_config_file(config_data, dir_home)