Add support for loading custom label fonts in imglab (#2733)

This commit is contained in:
Adrià Arrufat 2023-02-24 12:34:06 +09:00 committed by GitHub
parent dc94754607
commit eb5de0d534
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -21,7 +21,7 @@
#include <dlib/dir_nav.h> #include <dlib/dir_nav.h>
const char* VERSION = "1.19"; const char* VERSION = "1.20";
@ -599,6 +599,7 @@ int main(int argc, char** argv)
parser.add_option("h","Displays this information."); parser.add_option("h","Displays this information.");
parser.add_option("v","Display version."); parser.add_option("v","Display version.");
parser.add_option("font", "Set the label font in the GUI.", 1);
parser.set_group_name("Creating XML files"); parser.set_group_name("Creating XML files");
parser.add_option("c","Create an XML file named <arg> listing a set of images.",1); parser.add_option("c","Create an XML file named <arg> listing a set of images.",1);
@ -672,6 +673,8 @@ int main(int argc, char** argv)
parser.parse(argc, argv); parser.parse(argc, argv);
const std::string font_path = get_option(parser, "font", "");
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-basic", "flip", "rotate", "tile", "size", "cluster", "resample", "min-object-size", "rmempty", "flip-basic", "flip", "rotate", "tile", "size", "cluster", "resample", "min-object-size", "rmempty",
"crop-size", "cropped-object-size", "rmlabel", "rm-other-labels", "rm-if-overlaps", "sort-num-objects", "crop-size", "cropped-object-size", "rmlabel", "rm-other-labels", "rm-if-overlaps", "sort-num-objects",
@ -1263,7 +1266,7 @@ int main(int argc, char** argv)
if (parser.number_of_arguments() == 1) if (parser.number_of_arguments() == 1)
{ {
metadata_editor editor(parser[0]); metadata_editor editor(parser[0], font_path);
if (parser.option("parts")) if (parser.option("parts"))
{ {
std::vector<string> parts = split(parser.option("parts").argument()); std::vector<string> parts = split(parser.option("parts").argument());

View File

@ -23,7 +23,8 @@ extern const char* VERSION;
metadata_editor:: metadata_editor::
metadata_editor( metadata_editor(
const std::string& filename_ const std::string& filename_,
const std::string& font_path
) : ) :
mbar(*this), mbar(*this),
lb_images(*this), lb_images(*this),
@ -36,6 +37,26 @@ metadata_editor(
{ {
file metadata_file(filename_); file metadata_file(filename_);
filename = metadata_file.full_name(); filename = metadata_file.full_name();
// Set the custom label fonts
if (!font_path.empty())
{
const auto custom_font = std::make_shared<dlib::bdf_font>();
std::ifstream fin(font_path);
if (fin.good())
{
custom_font->read_bdf_file(fin, 0xFFFF);
custom_font->adjust_metrics();
font = std::move(custom_font);
}
else
{
std::cerr << "WARNING: could not open file '" + font_path + "', using default font\n.";
}
}
display.set_main_font(font);
overlay_label.set_main_font(font);
// Make our current directory be the one that contains the metadata file. We // Make our current directory be the one that contains the metadata file. We
// do this because that file might contain relative paths to the image files // do this because that file might contain relative paths to the image files
// we are supposed to be loading. // we are supposed to be loading.

View File

@ -58,7 +58,8 @@ class metadata_editor : public dlib::drawable_window
{ {
public: public:
metadata_editor( metadata_editor(
const std::string& filename_ const std::string& filename_,
const std::string& font_path = ""
); );
~metadata_editor(); ~metadata_editor();
@ -107,6 +108,8 @@ private:
time_t last_keyboard_jump_pos_update; time_t last_keyboard_jump_pos_update;
bool display_equialized_image = false; bool display_equialized_image = false;
color_mapper string_to_color; color_mapper string_to_color;
std::shared_ptr<dlib::font> font = dlib::default_font::get_font();
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------