Add option --rm-other-labels to imglab (#323)

* Add option --rm-other-labels to imglab

* Add support for comma separated list of labels
This commit is contained in:
Avi Haiat 2016-11-10 03:36:52 +02:00 committed by Davis E. King
parent 08a89c80c9
commit 85e2bcce02

View File

@ -627,6 +627,7 @@ int main(int argc, char** argv)
"the results as cluster_###.xml and cluster_###.jpg files.",1); "the results as cluster_###.xml and cluster_###.jpg files.",1);
parser.add_option("ignore", "Mark boxes labeled as <arg> as ignored. The resulting XML file is output as a separate file and the original is not modified.",1); parser.add_option("ignore", "Mark boxes labeled as <arg> as ignored. The resulting XML file is output as a separate file and the original is not modified.",1);
parser.add_option("rmlabel","Remove all boxes labeled <arg> and save the results to a new XML file.",1); parser.add_option("rmlabel","Remove all boxes labeled <arg> and save the results to a new XML file.",1);
parser.add_option("rm-other-labels","Remove all boxes not labeled <arg> and save the results to a new XML file.",1);
parser.add_option("rmignore","Remove all boxes marked ignore and save the results to a new XML file."); parser.add_option("rmignore","Remove all boxes marked ignore and save the results to a new XML file.");
parser.add_option("rm-if-overlaps","Remove all boxes labeled <arg> if they overlap any box not labeled <arg> and save the results to a new XML file.",1); parser.add_option("rm-if-overlaps","Remove all boxes labeled <arg> if they overlap any box not labeled <arg> and save the results to a new XML file.",1);
parser.add_option("jpg", "When saving images to disk, write them as jpg files instead of png."); parser.add_option("jpg", "When saving images to disk, write them as jpg files instead of png.");
@ -645,7 +646,7 @@ int main(int argc, char** argv)
const char* singles[] = {"h","c","r","l","files","convert","parts","rmdiff", "rmtrunc", "rmdupes", "seed", "shuffle", "split", "add", const char* singles[] = {"h","c","r","l","files","convert","parts","rmdiff", "rmtrunc", "rmdupes", "seed", "shuffle", "split", "add",
"flip", "rotate", "tile", "size", "cluster", "resample", "min-object-size", "rmempty", "flip", "rotate", "tile", "size", "cluster", "resample", "min-object-size", "rmempty",
"crop-size", "cropped-object-size", "rmlabel", "rm-if-overlaps", "sort-num-objects", "one-object-per-image", "jpg", "rmignore"}; "crop-size", "cropped-object-size", "rmlabel", "rm-other-labels", "rm-if-overlaps", "sort-num-objects", "one-object-per-image", "jpg", "rmignore"};
parser.check_one_time_options(singles); parser.check_one_time_options(singles);
const char* c_sub_ops[] = {"r", "convert"}; const char* c_sub_ops[] = {"r", "convert"};
parser.check_sub_options("c", c_sub_ops); parser.check_sub_options("c", c_sub_ops);
@ -659,6 +660,7 @@ int main(int argc, char** argv)
parser.check_incompatible_options("c", "rmdiff"); parser.check_incompatible_options("c", "rmdiff");
parser.check_incompatible_options("c", "rmempty"); parser.check_incompatible_options("c", "rmempty");
parser.check_incompatible_options("c", "rmlabel"); parser.check_incompatible_options("c", "rmlabel");
parser.check_incompatible_options("c", "rm-other-labels");
parser.check_incompatible_options("c", "rmignore"); parser.check_incompatible_options("c", "rmignore");
parser.check_incompatible_options("c", "rm-if-overlaps"); parser.check_incompatible_options("c", "rm-if-overlaps");
parser.check_incompatible_options("c", "rmdupes"); parser.check_incompatible_options("c", "rmdupes");
@ -712,6 +714,8 @@ int main(int argc, char** argv)
parser.check_incompatible_options("rmempty", "rename"); parser.check_incompatible_options("rmempty", "rename");
parser.check_incompatible_options("rmlabel", "ignore"); parser.check_incompatible_options("rmlabel", "ignore");
parser.check_incompatible_options("rmlabel", "rename"); parser.check_incompatible_options("rmlabel", "rename");
parser.check_incompatible_options("rm-other-labels", "ignore");
parser.check_incompatible_options("rm-other-labels", "rename");
parser.check_incompatible_options("rmignore", "ignore"); parser.check_incompatible_options("rmignore", "ignore");
parser.check_incompatible_options("rmignore", "rename"); parser.check_incompatible_options("rmignore", "rename");
parser.check_incompatible_options("rm-if-overlaps", "ignore"); parser.check_incompatible_options("rm-if-overlaps", "ignore");
@ -875,6 +879,37 @@ int main(int argc, char** argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
if (parser.option("rm-other-labels"))
{
if (parser.number_of_arguments() != 1)
{
cerr << "The --rm-other-labels option requires you to give one XML file on the command line." << endl;
return EXIT_FAILURE;
}
dlib::image_dataset_metadata::dataset data;
load_image_dataset_metadata(data, parser[0]);
const auto labels = parser.option("rm-other-labels").argument();
// replace comma by dash to form the file name
std::string strlabels = labels;
std::replace(strlabels.begin(), strlabels.end(), ',', '-');
std::vector<string> all_labels = split(labels, ",");
for (auto&& img : data.images)
{
std::vector<dlib::image_dataset_metadata::box> boxes;
for (auto&& b : img.boxes)
{
if (std::find(all_labels.begin(), all_labels.end(), b.label) != all_labels.end())
boxes.push_back(b);
}
img.boxes = boxes;
}
save_image_dataset_metadata(data, parser[0] + ".rm-other-labels-"+ strlabels +".xml");
return EXIT_SUCCESS;
}
if (parser.option("rmignore")) if (parser.option("rmignore"))
{ {
if (parser.number_of_arguments() != 1) if (parser.number_of_arguments() != 1)