"examples/sampling/graphbolt/vscode:/vscode.git/clone" did not exist on "e22bd78fe052a9480c354cace98d98e08ed20bcc"
metadata_editor.cpp 12.4 KB
Newer Older
1
2
3
4
5
6
// Copyright (C) 2011  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.

#include "metadata_editor.h"
#include <dlib/array.h>
#include <dlib/queue.h>
7
#include <dlib/static_set.h>
8
9
10
11
#include <dlib/misc_api.h>
#include <dlib/image_io.h>
#include <dlib/array2d.h>
#include <dlib/pixel.h>
Davis King's avatar
Davis King committed
12
#include <sstream>
13
14
15
16

using namespace std;
using namespace dlib;

Davis King's avatar
Davis King committed
17
18
extern const char* VERSION;

19
20
21
22
23
24
25
26
// ----------------------------------------------------------------------------------------

metadata_editor::
metadata_editor(
    const std::string& filename_
) : 
    mbar(*this),
    lb_images(*this),
27
28
29
30
    image_pos(0),
    display(*this),
    overlay_label_name(*this),
    overlay_label(*this)
31
{
32
33
    file metadata_file(filename_);
    filename = metadata_file.full_name();
34
35
36
    // 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
    // we are supposed to be loading.
37
38
    set_current_dir(get_parent_directory(metadata_file).full_name());

39
40
41
42
43
44
45
46
47
48
49
50
51
    load_image_dataset_metadata(metadata, filename);

    dlib::array<std::string>::expand_1a files;
    files.resize(metadata.images.size());
    for (unsigned long i = 0; i < metadata.images.size(); ++i)
    {
        files[i] = metadata.images[i].filename;
    }
    lb_images.load(files);
    lb_images.enable_multiple_select();

    lb_images.set_click_handler(*this, &metadata_editor::on_lb_images_clicked);

52
53
    overlay_label_name.set_text("Next Label: ");
    overlay_label.set_width(200);
54

55
    display.set_overlay_rects_changed_handler(*this, &metadata_editor::on_overlay_rects_changed);
56
    display.set_overlay_rect_selected_handler(*this, &metadata_editor::on_overlay_rect_selected);
57
58
    overlay_label.set_text_modified_handler(*this, &metadata_editor::on_overlay_label_changed);

Davis King's avatar
Davis King committed
59
    mbar.set_number_of_menus(2);
60
    mbar.set_menu_name(0,"File",'F');
Davis King's avatar
Davis King committed
61
    mbar.set_menu_name(1,"Help",'H');
62
63
64
65
66
67
68


    mbar.menu(0).add_menu_item(menu_item_text("Save",*this,&metadata_editor::file_save,'S'));
    mbar.menu(0).add_menu_item(menu_item_text("Save As",*this,&metadata_editor::file_save_as,'A'));
    mbar.menu(0).add_menu_item(menu_item_separator());
    mbar.menu(0).add_menu_item(menu_item_text("Remove Selected Images",*this,&metadata_editor::remove_selected_images,'R'));
    mbar.menu(0).add_menu_item(menu_item_separator());
69
    mbar.menu(0).add_menu_item(menu_item_text("Exit",static_cast<base_window&>(*this),&drawable_window::close_window,'x'));
70

Davis King's avatar
Davis King committed
71
    mbar.menu(1).add_menu_item(menu_item_text("About",*this,&metadata_editor::display_about,'A'));
72

73
    // set the size of this window.
74
    on_window_resized();
Davis King's avatar
Davis King committed
75
    load_image_and_set_size(0);
76
77
78
    on_window_resized();
    if (image_pos < lb_images.size() )
        lb_images.select(image_pos);
79

80
81
82
83
84
85
86
87
    // make sure the window is centered on the screen.
    unsigned long width, height;
    get_size(width, height);
    unsigned long screen_width, screen_height;
    get_display_size(screen_width, screen_height);
    set_pos((screen_width-width)/2, (screen_height-height)/2);

    set_title("Image Labeler - " + metadata.name);
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    show();
} 

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

metadata_editor::
~metadata_editor(
)
{
    close_window();
}

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

102
103
104
105
106
107
108
109
110
111
void metadata_editor::
add_labelable_part_name (
    const std::string& name
)
{
    display.add_labelable_part_name(name);
}

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

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
void metadata_editor::
file_save()
{
    save_metadata_to_file(filename);
}

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

void metadata_editor::
save_metadata_to_file (
    const std::string& file
)
{
    try
    {
        save_image_dataset_metadata(metadata, file);
    }
    catch (dlib::error& e)
    {
        message_box("Error saving file", e.what());
    }
}

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

void metadata_editor::
file_save_as()
{
    save_file_box(*this, &metadata_editor::save_metadata_to_file);
}

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

void metadata_editor::
remove_selected_images()
{
148
149
150
    dlib::queue<unsigned long>::kernel_1a list;
    lb_images.get_selected(list);
    list.reset();
151
    unsigned long min_idx = lb_images.size();
152
153
154
    while (list.move_next())
    {
        lb_images.unselect(list.element());
155
        min_idx = std::min(min_idx, list.element());
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
    // remove all the selected items from metadata.images
    dlib::static_set<unsigned long>::kernel_1a to_remove;
    to_remove.load(list);
    std::vector<dlib::image_dataset_metadata::image> images;
    for (unsigned long i = 0; i < metadata.images.size(); ++i)
    {
        if (to_remove.is_member(i) == false)
        {
            images.push_back(metadata.images[i]);
        }
    }
    images.swap(metadata.images);


    // reload metadata into lb_images
    dlib::array<std::string>::expand_1a files;
    files.resize(metadata.images.size());
    for (unsigned long i = 0; i < metadata.images.size(); ++i)
    {
        files[i] = metadata.images[i].filename;
    }
    lb_images.load(files);


183
184
185
    if (min_idx != 0)
        min_idx--;
    select_image(min_idx);
186
187
188
189
190
191
192
193
194
195
196
197
198
199
}

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

void metadata_editor::
on_window_resized(
)
{
    drawable_window::on_window_resized();

    unsigned long width, height;
    get_size(width, height);

    lb_images.set_pos(0,mbar.bottom()+1);
200
    lb_images.set_size(180, height - mbar.height());
201
202
203
204
205
206

    overlay_label_name.set_pos(lb_images.right()+10, mbar.bottom() + (overlay_label.height()-overlay_label_name.height())/2+1);
    overlay_label.set_pos(overlay_label_name.right(), mbar.bottom()+1);
    display.set_pos(lb_images.right(), overlay_label.bottom()+3);

    display.set_size(width - display.left(), height - display.top());
207
208
209
210
211
212
213
214
215
216
217
218
219
220
}

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

void metadata_editor::
on_keydown (
    unsigned long key,
    bool is_printable,
    unsigned long state
)
{
    drawable_window::on_keydown(key, is_printable, state);

    if (is_printable)
221
222
223
224
225
226
227
    {
        if (key == '\t')
        {
            overlay_label.give_input_focus();
            overlay_label.select_all_text();
        }

228
        return;
229
    }
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

    if (key == base_window::KEY_UP)
    {
        select_image(image_pos-1);
    }
    else if (key == base_window::KEY_DOWN)
    {
        select_image(image_pos+1);
    }
}

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

void metadata_editor::
select_image(
    unsigned long idx
)
{
    if (idx < lb_images.size())
    {
        // unselect all currently selected images
        dlib::queue<unsigned long>::kernel_1a list;
        lb_images.get_selected(list);
        list.reset();
        while (list.move_next())
        {
            lb_images.unselect(list.element());
        }


        lb_images.select(idx);
261
        load_image(idx);
262
    }
Davis King's avatar
Davis King committed
263
    else if (lb_images.size() == 0)
Davis King's avatar
Davis King committed
264
265
266
267
268
    {
        display.clear_overlay();
        array2d<unsigned char> empty_img;
        display.set_image(empty_img);
    }
269
270
271
272
273
274
275
276
277
}

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

void metadata_editor::
on_lb_images_clicked(
    unsigned long idx
) 
{ 
278
    load_image(idx);
279
280
281
282
}

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

Davis King's avatar
Davis King committed
283
284
285
286
287
288
289
290
291
std::vector<dlib::image_display::overlay_rect> get_overlays (
    const dlib::image_dataset_metadata::image& data
)
{
    std::vector<dlib::image_display::overlay_rect> temp(data.boxes.size());
    for (unsigned long i = 0; i < temp.size(); ++i)
    {
        temp[i].rect = data.boxes[i].rect;
        temp[i].label = data.boxes[i].label;
292
        temp[i].parts = data.boxes[i].parts;
293
        temp[i].crossed_out = data.boxes[i].ignore;
Davis King's avatar
Davis King committed
294
295
296
297
298
299
300
        assign_pixel(temp[i].color, rgb_pixel(255,0,0));
    }
    return temp;
}

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

301
302
303
304
305
306
307
308
void metadata_editor::
load_image(
    unsigned long idx
)
{
    if (idx >= metadata.images.size())
        return;

Davis King's avatar
Davis King committed
309
310
    image_pos = idx; 

311
312
313
314
315
316
317
318
319
320
321
322
    array2d<rgb_pixel> img;
    display.clear_overlay();
    try
    {
        dlib::load_image(img, metadata.images[idx].filename);

    }
    catch (exception& e)
    {
        message_box("Error loading image", e.what());
    }

323
    display.set_image(img);
Davis King's avatar
Davis King committed
324
    display.add_overlay(get_overlays(metadata.images[idx]));
325
326
327
328
329
330
331
332
333
334
335
336
}

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

void metadata_editor::
load_image_and_set_size(
    unsigned long idx
)
{
    if (idx >= metadata.images.size())
        return;

Davis King's avatar
Davis King committed
337
338
    image_pos = idx; 

339
340
341
342
343
344
345
346
347
348
349
350
    array2d<rgb_pixel> img;
    display.clear_overlay();
    try
    {
        dlib::load_image(img, metadata.images[idx].filename);

    }
    catch (exception& e)
    {
        message_box("Error loading image", e.what());
    }

351

352
353
    unsigned long screen_width, screen_height;
    get_display_size(screen_width, screen_height);
354

355

356
357
358
359
360
361
362
363
364
    unsigned long needed_width = display.left() + img.nc() + 4;
    unsigned long needed_height = display.top() + img.nr() + 4;
	if (needed_width < 300) needed_width = 300;
	if (needed_height < 300) needed_height = 300;

    if (needed_width+50 < screen_width &&
        needed_height+50 < screen_height)
    {
        set_size(needed_width, needed_height);
365
366
    }

367

368
    display.set_image(img);
Davis King's avatar
Davis King committed
369
    display.add_overlay(get_overlays(metadata.images[idx]));
370
371
372
373
}

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

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
void metadata_editor::
on_overlay_rects_changed(
)
{
    using namespace dlib::image_dataset_metadata;
    if (image_pos < metadata.images.size())
    {
        const std::vector<image_display::overlay_rect>& rects = display.get_overlay_rects();

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

        boxes.clear();
        for (unsigned long i = 0; i < rects.size(); ++i)
        {
            box temp;
            temp.label = rects[i].label;
            temp.rect = rects[i].rect;
391
            temp.parts = rects[i].parts;
392
            temp.ignore = rects[i].crossed_out;
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
            boxes.push_back(temp);
        }
    }
}

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

void metadata_editor::
on_overlay_label_changed(
)
{
    display.set_default_overlay_rect_label(trim(overlay_label.text()));
}

// ----------------------------------------------------------------------------------------
408

409
410
411
412
413
414
415
416
417
418
419
void metadata_editor::
on_overlay_rect_selected(
    const image_display::overlay_rect& orect
)
{
    overlay_label.set_text(orect.label);
    display.set_default_overlay_rect_label(orect.label);
}

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

Davis King's avatar
Davis King committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
void metadata_editor::
display_about(
)
{
    std::ostringstream sout;
    sout << wrap_string("Image Labeler v" + string(VERSION) + "." ,0,0) << endl << endl;
    sout << wrap_string("This program is a tool for labeling images with rectangles. " ,0,0) << endl << endl;

    sout << wrap_string("You can add new rectangles by holding the shift key, left clicking "
                        "the mouse, and dragging it.  Hit tab to give input focus to the next "
                        "label text field and supply a label for a rectangle.  Double clicking "
                        "a rectangle selects it and the delete key removes it."
                        ,0,0) << endl << endl;

434
435
436
437
438
439
    sout << wrap_string("It is also possible to label object parts by selecting a rectangle and "
                        "then right clicking.  A popup menu will appear and you can select a part label. "
                        "Note that you must define the allowable part labels by giving --parts on the "
                        "command line.  An example would be '--parts \"leye reye nose mouth\"'."
                        ,0,0) << endl << endl;

Davis King's avatar
Davis King committed
440
441
442
443
444
445
446
447
    sout << wrap_string("Finally, hold ctrl and scroll the mouse wheel to zoom while normal left click "
                        "and drag allows you to navigate around the image.",0,0) << endl;

    message_box("About Image Labeler",sout.str());
}

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