Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dlib/data_io/image_dataset_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ namespace dlib
ensures
- returns true if label metadata is present and false otherwise.
!*/

bool operator==(const box& rhs) const
{
return rect == rhs.rect &&
parts == rhs.parts &&
label == rhs.label &&
difficult == rhs.difficult &&
truncated == rhs.truncated &&
occluded == rhs.occluded &&
ignore == rhs.ignore &&
pose == rhs.pose &&
detection_score == rhs.detection_score &&
angle == rhs.angle &&
gender == rhs.gender &&
age == rhs.age;
}

bool operator!=(const box& rhs) const
{
return !(*this == rhs);
}
};

// ------------------------------------------------------------------------------------
Expand Down
79 changes: 79 additions & 0 deletions tools/imglab/src/metadata_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ extern const char* VERSION;

// ----------------------------------------------------------------------------------------

std::vector<dlib::image_display::overlay_rect> get_overlays (
const dlib::image_dataset_metadata::image& data,
color_mapper& string_to_color
);

metadata_editor::
metadata_editor(
const std::string& filename_,
Expand Down Expand Up @@ -390,6 +395,25 @@ on_keydown (
select_image(image_pos);
}

if ((key == 'z' || key == 'Z') && (state&base_window::KBD_MOD_CONTROL) && !overlay_label.has_input_focus())
{
if (state&base_window::KBD_MOD_SHIFT)
{
perform_redo();
}
else
{
perform_undo();
}
return;
}

if ((key == 'y' || key == 'Y') && (state&base_window::KBD_MOD_CONTROL) && !overlay_label.has_input_focus())
{
perform_redo();
return;
}

// Make 'w' and 's' act like KEY_UP and KEY_DOWN
if ((key == 'w' || key == 'W') && !overlay_label.has_input_focus())
{
Expand Down Expand Up @@ -532,6 +556,12 @@ load_image(
if (idx >= metadata.images.size())
return;

if (image_pos != idx)
{
undo_history.clear();
redo_history.clear();
}

image_pos = idx;

array2d<rgb_pixel> img;
Expand All @@ -556,6 +586,40 @@ load_image(

// ----------------------------------------------------------------------------------------

void metadata_editor::
perform_undo()
{
if (!undo_history.empty())
{
redo_history.push_back(metadata.images[image_pos].boxes);
if (redo_history.size() > MAX_UNDO_HISTORY) redo_history.erase(redo_history.begin());

metadata.images[image_pos].boxes = undo_history.back();
undo_history.pop_back();
display.clear_overlay();
display.add_overlay(get_overlays(metadata.images[image_pos], string_to_color));
}
}

// ----------------------------------------------------------------------------------------

void metadata_editor::
perform_redo()
{
if (!redo_history.empty())
{
undo_history.push_back(metadata.images[image_pos].boxes);
if (undo_history.size() > MAX_UNDO_HISTORY) undo_history.erase(undo_history.begin());

metadata.images[image_pos].boxes = redo_history.back();
redo_history.pop_back();
display.clear_overlay();
display.add_overlay(get_overlays(metadata.images[image_pos], string_to_color));
}
}

// ----------------------------------------------------------------------------------------

void metadata_editor::
load_image_and_set_size(
unsigned long idx
Expand All @@ -564,6 +628,12 @@ load_image_and_set_size(
if (idx >= metadata.images.size())
return;

if (image_pos != idx)
{
undo_history.clear();
redo_history.clear();
}

image_pos = idx;

array2d<rgb_pixel> img;
Expand Down Expand Up @@ -616,6 +686,7 @@ on_overlay_rects_changed(
const std::vector<image_display::overlay_rect>& rects = display.get_overlay_rects();

std::vector<box>& boxes = metadata.images[image_pos].boxes;
std::vector<box> old_boxes = boxes;

boxes.clear();
for (unsigned long i = 0; i < rects.size(); ++i)
Expand All @@ -627,6 +698,14 @@ on_overlay_rects_changed(
temp.ignore = rects[i].crossed_out;
boxes.push_back(temp);
}

if (old_boxes != boxes)
{
undo_history.push_back(old_boxes);
if (undo_history.size() > MAX_UNDO_HISTORY)
undo_history.erase(undo_history.begin());
redo_history.clear();
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions tools/imglab/src/metadata_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <dlib/data_io.h>
#include <dlib/pixel.h>
#include <map>
#include <vector>

// ----------------------------------------------------------------------------------------

Expand Down Expand Up @@ -69,6 +70,8 @@ class metadata_editor : public dlib::drawable_window
);

private:
void perform_undo();
void perform_redo();

void file_save();
void file_save_as();
Expand Down Expand Up @@ -96,6 +99,10 @@ class metadata_editor : public dlib::drawable_window
std::string filename;
dlib::image_dataset_metadata::dataset metadata;

std::vector<std::vector<dlib::image_dataset_metadata::box>> undo_history;
std::vector<std::vector<dlib::image_dataset_metadata::box>> redo_history;
static constexpr size_t MAX_UNDO_HISTORY = 10;

dlib::menu_bar mbar;
dlib::list_box lb_images;
unsigned long image_pos;
Expand Down
Loading