Skip to content

Commit bea59f8

Browse files
author
Gin
committed
improve labeled image export
1 parent fc9deef commit bea59f8

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

SerialPrograms/Source/ML/DataLabeling/ML_AnnotationIO.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,13 @@ void export_image_annotations_to_yolo_dataset(
163163
line.clear();
164164
}
165165

166-
std::map<std::string, size_t> label_indices;
166+
std::map<std::string, size_t> label_counts; // label name -> how many such label in the dataseet
167+
std::map<std::string, size_t> label_indices; // label name -> label ID
167168
cout << "Load dataset labels: " << endl;
168169
for(size_t i = 0; i < label_names.size(); i++){
169170
cout << "- " << label_names[i] << endl;
170171
label_indices[label_names[i]] = i;
172+
label_counts[label_names[i]] = 0;
171173
}
172174

173175

@@ -183,15 +185,16 @@ void export_image_annotations_to_yolo_dataset(
183185
QString::fromStdString("Folder " + target_folder.string() + " already exists."));
184186
return;
185187
}
186-
const auto target_image_folder = target_folder / "images";
187-
const auto target_label_folder = target_folder / "labels";
188+
const auto target_image_folder = target_folder / "images/train";
189+
const auto target_label_folder = target_folder / "labels/train";
188190
cout << "Export to image folder: " << target_image_folder << endl;
189191
cout << "Export to label folder: " << target_label_folder << endl;
190192

191193
fs::create_directories(target_image_folder);
192194
fs::create_directories(target_label_folder);
193195

194196
fs::path anno_folder(annotation_folder_path);
197+
std::set<std::string> missing_labels;
195198
for(size_t i = 0; i < image_paths.size(); i++){
196199
const auto& image_path = image_paths[i];
197200
const auto image_file = fs::path(image_path);
@@ -255,8 +258,10 @@ void export_image_annotations_to_yolo_dataset(
255258
}
256259
}
257260
if (it == label_indices.end()){
261+
missing_labels.insert(label);
258262
continue; // label not part of the YOLO dataset. Ignored.
259263
}
264+
label_counts[label]++;
260265

261266
const size_t label_id = it->second;
262267

@@ -286,6 +291,17 @@ void export_image_annotations_to_yolo_dataset(
286291
fout << file_line << "\n";
287292
}
288293
}
294+
295+
cout << "Found labels -> count: " << endl;
296+
for(const auto& p : label_counts){
297+
cout << "- " << p.first << ": " << p.second << endl;
298+
}
299+
if (missing_labels.size() > 0){
300+
cout << "Labels not exported: " << endl;
301+
for(const auto& label : missing_labels){
302+
cout << "- " << label << endl;
303+
}
304+
}
289305
cout << "Done exporting " << image_paths.size() << " annotations to YOLOv5 dataset" << endl;
290306
}
291307

0 commit comments

Comments
 (0)