metadata_editor.cpp 20.9 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>
12
#include <dlib/image_transforms.h>
13
#include <dlib/image_processing.h>
Davis King's avatar
Davis King committed
14
#include <sstream>
15
#include <ctime>
16
17
18
19

using namespace std;
using namespace dlib;

Davis King's avatar
Davis King committed
20
21
extern const char* VERSION;

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
rgb_alpha_pixel string_to_color(
    const std::string& str
)
{
    if (str.size() == 0)
    {
        return rgb_alpha_pixel(255,0,0,255);
    }
    else
    {
        // make up a random color based on the string label.
        hsi_pixel pix;
        pix.h = static_cast<unsigned char>(dlib::hash(str)&0xFF); 
        pix.s = 255;
        pix.i = 150;
        rgb_alpha_pixel result;
        assign_pixel(result, pix);
        return result;
    }
}

43
44
45
46
47
48
49
50
// ----------------------------------------------------------------------------------------

metadata_editor::
metadata_editor(
    const std::string& filename_
) : 
    mbar(*this),
    lb_images(*this),
51
52
53
    image_pos(0),
    display(*this),
    overlay_label_name(*this),
54
55
56
    overlay_label(*this),
    keyboard_jump_pos(0),
    last_keyboard_jump_pos_update(0)
57
{
58
59
    file metadata_file(filename_);
    filename = metadata_file.full_name();
60
61
62
    // 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.
63
64
    set_current_dir(get_parent_directory(metadata_file).full_name());

65
66
67
68
69
70
71
72
73
74
75
76
77
    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);

78
79
    overlay_label_name.set_text("Next Label: ");
    overlay_label.set_width(200);
80

81
    display.set_overlay_rects_changed_handler(*this, &metadata_editor::on_overlay_rects_changed);
82
    display.set_overlay_rect_selected_handler(*this, &metadata_editor::on_overlay_rect_selected);
83
84
    overlay_label.set_text_modified_handler(*this, &metadata_editor::on_overlay_label_changed);

Davis King's avatar
Davis King committed
85
    mbar.set_number_of_menus(2);
86
    mbar.set_menu_name(0,"File",'F');
Davis King's avatar
Davis King committed
87
    mbar.set_menu_name(1,"Help",'H');
88
89
90
91
92
93
94


    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());
95
    mbar.menu(0).add_menu_item(menu_item_text("Exit",static_cast<base_window&>(*this),&drawable_window::close_window,'x'));
96

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

99
    // set the size of this window.
100
    on_window_resized();
Davis King's avatar
Davis King committed
101
    load_image_and_set_size(0);
102
103
104
    on_window_resized();
    if (image_pos < lb_images.size() )
        lb_images.select(image_pos);
105

106
107
108
109
110
111
112
    // 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);

113
114
115
116
117
118
119
120
121
122
123
124
125
126
    show();
} 

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

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

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

127
128
129
130
131
132
133
134
135
136
void metadata_editor::
add_labelable_part_name (
    const std::string& name
)
{
    display.add_labelable_part_name(name);
}

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

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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()
{
173
174
175
    dlib::queue<unsigned long>::kernel_1a list;
    lb_images.get_selected(list);
    list.reset();
176
    unsigned long min_idx = lb_images.size();
177
178
179
    while (list.move_next())
    {
        lb_images.unselect(list.element());
180
        min_idx = std::min(min_idx, list.element());
181
182
    }

183

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    // 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);


208
209
210
    if (min_idx != 0)
        min_idx--;
    select_image(min_idx);
211
212
213
214
215
216
217
218
219
220
221
222
223
224
}

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

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);
225
    lb_images.set_size(180, height - mbar.height());
226
227
228
229
230
231

    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());
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
void propagate_boxes(
    dlib::image_dataset_metadata::dataset& data,
    unsigned long prev,
    unsigned long next
)
{
    if (prev == next || next >= data.images.size())
        return;

    array2d<rgb_pixel> img1, img2;
    dlib::load_image(img1, data.images[prev].filename);
    dlib::load_image(img2, data.images[next].filename);
    for (unsigned long i = 0; i < data.images[prev].boxes.size(); ++i)
    {
        correlation_tracker tracker;
        tracker.start_track(img1, data.images[prev].boxes[i].rect);
        tracker.update(img2);
        dlib::image_dataset_metadata::box box = data.images[prev].boxes[i];
        box.rect = tracker.get_position();
        data.images[next].boxes.push_back(box);
    }
}

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

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
void propagate_labels(
    const std::string& label,
    dlib::image_dataset_metadata::dataset& data,
    unsigned long prev,
    unsigned long next
)
{
    if (prev == next || next >= data.images.size())
        return;


    for (unsigned long i = 0; i < data.images[prev].boxes.size(); ++i)
    {
        if (data.images[prev].boxes[i].label != label)
            continue;

        // figure out which box in the next image matches the current one the best
        const rectangle cur = data.images[prev].boxes[i].rect;
        double best_overlap = 0;
        unsigned long best_idx = 0;
        for (unsigned long j = 0; j < data.images[next].boxes.size(); ++j)
        {
            const rectangle next_box = data.images[next].boxes[j].rect;
            const double overlap = cur.intersect(next_box).area()/(double)(cur+next_box).area();
            if (overlap > best_overlap)
            {
                best_overlap = overlap;
                best_idx = j;
            }
        }

        // If we found a matching rectangle in the next image and the best match doesn't
        // already have a label.
        if (best_overlap > 0.5 && data.images[next].boxes[best_idx].label == "")
        {
            data.images[next].boxes[best_idx].label = label;
        }
    }

}

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// ----------------------------------------------------------------------------------------

bool has_label_or_all_boxes_labeled (
    const std::string& label,
    const dlib::image_dataset_metadata::image& img 
)
{
    if (label.size() == 0)
        return true;

    bool all_boxes_labeled = true;
    for (unsigned long i = 0; i < img.boxes.size(); ++i)
    {
        if (img.boxes[i].label == label)
            return true;
        if (img.boxes[i].label.size() == 0)
            all_boxes_labeled = false;
    }

    return all_boxes_labeled;
}

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

326
327
328
329
330
331
332
333
334
335
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)
336
337
338
339
340
341
342
    {
        if (key == '\t')
        {
            overlay_label.give_input_focus();
            overlay_label.select_all_text();
        }

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
        // If the user types a number then jump to that image.
        if ('0' <= key && key <= '9' && metadata.images.size() != 0 && !overlay_label.has_input_focus())
        {
            time_t curtime = time(0);
            // If it's been a while since the user typed numbers then forget the last jump
            // position and start accumulating numbers over again.
            if (curtime-last_keyboard_jump_pos_update >= 2)
                keyboard_jump_pos = 0;
            last_keyboard_jump_pos_update = curtime;

            keyboard_jump_pos *= 10;
            keyboard_jump_pos += key-'0';
            if (keyboard_jump_pos >= metadata.images.size())
                keyboard_jump_pos = metadata.images.size()-1;

            image_pos = keyboard_jump_pos;
            select_image(image_pos);
        }
        else
        {
            last_keyboard_jump_pos_update = 0;
        }

366
        if (key == 'd' && (state&base_window::KBD_MOD_ALT))
367
368
369
370
        {
            remove_selected_images();
        }

371
372
373
374
375
376
        if (key == 'e' && !overlay_label.has_input_focus())
        {
            display_equialized_image = !display_equialized_image;
            select_image(image_pos);
        }

377

378
        return;
379
    }
380
381
382

    if (key == base_window::KEY_UP)
    {
383
384
385
386
387
388
389
390
391
392
393
394
        if ((state&KBD_MOD_CONTROL) && (state&KBD_MOD_SHIFT))
        {
            // Don't do anything if there are no boxes in the current image.
            if (metadata.images[image_pos].boxes.size() == 0)
                return;
            // Also don't do anything if there *are* boxes in the next image.
            if (image_pos > 1 && metadata.images[image_pos-1].boxes.size() != 0)
                return;

            propagate_boxes(metadata, image_pos, image_pos-1);
        }
        else if (state&base_window::KBD_MOD_CONTROL)
395
396
397
398
399
400
401
402
403
404
        {
            // If the label we are supposed to propagate doesn't exist in the current image
            // then don't advance.
            if (!has_label_or_all_boxes_labeled(display.get_default_overlay_rect_label(),metadata.images[image_pos]))
                return;

            // if the next image is going to be empty then fast forward to the next one
            while (image_pos > 1 && metadata.images[image_pos-1].boxes.size() == 0)
                --image_pos;

405
            propagate_labels(display.get_default_overlay_rect_label(), metadata, image_pos, image_pos-1);
406
        }
407
408
409
410
        select_image(image_pos-1);
    }
    else if (key == base_window::KEY_DOWN)
    {
411
412
413
414
415
416
417
418
419
420
421
422
        if ((state&KBD_MOD_CONTROL) && (state&KBD_MOD_SHIFT))
        {
            // Don't do anything if there are no boxes in the current image.
            if (metadata.images[image_pos].boxes.size() == 0)
                return;
            // Also don't do anything if there *are* boxes in the next image.
            if (image_pos+1 < metadata.images.size() && metadata.images[image_pos+1].boxes.size() != 0)
                return;

            propagate_boxes(metadata, image_pos, image_pos+1);
        }
        else if (state&base_window::KBD_MOD_CONTROL)
423
424
425
426
427
428
429
430
431
432
        {
            // If the label we are supposed to propagate doesn't exist in the current image
            // then don't advance.
            if (!has_label_or_all_boxes_labeled(display.get_default_overlay_rect_label(),metadata.images[image_pos]))
                return;

            // if the next image is going to be empty then fast forward to the next one
            while (image_pos+1 < metadata.images.size() && metadata.images[image_pos+1].boxes.size() == 0)
                ++image_pos;

433
            propagate_labels(display.get_default_overlay_rect_label(), metadata, image_pos, image_pos+1);
434
        }
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
        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);
459
        load_image(idx);
460
    }
Davis King's avatar
Davis King committed
461
    else if (lb_images.size() == 0)
Davis King's avatar
Davis King committed
462
463
464
465
466
    {
        display.clear_overlay();
        array2d<unsigned char> empty_img;
        display.set_image(empty_img);
    }
467
468
469
470
471
472
473
474
475
}

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

void metadata_editor::
on_lb_images_clicked(
    unsigned long idx
) 
{ 
476
    load_image(idx);
477
478
479
480
}

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

Davis King's avatar
Davis King committed
481
482
483
484
485
486
487
488
489
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;
490
        temp[i].parts = data.boxes[i].parts;
491
        temp[i].crossed_out = data.boxes[i].ignore;
492
        temp[i].color = string_to_color(data.boxes[i].label);
Davis King's avatar
Davis King committed
493
494
495
496
497
498
    }
    return temp;
}

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

499
500
501
502
503
504
505
506
void metadata_editor::
load_image(
    unsigned long idx
)
{
    if (idx >= metadata.images.size())
        return;

Davis King's avatar
Davis King committed
507
508
    image_pos = idx; 

509
510
511
512
513
    array2d<rgb_pixel> img;
    display.clear_overlay();
    try
    {
        dlib::load_image(img, metadata.images[idx].filename);
514
        set_title(metadata.name + " #"+cast_to_string(idx)+": " +metadata.images[idx].filename);
515
516
517
518
519
520
    }
    catch (exception& e)
    {
        message_box("Error loading image", e.what());
    }

521
522
    if (display_equialized_image)
        equalize_histogram(img);
523
    display.set_image(img);
Davis King's avatar
Davis King committed
524
    display.add_overlay(get_overlays(metadata.images[idx]));
525
526
527
528
529
530
531
532
533
534
535
536
}

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

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

Davis King's avatar
Davis King committed
537
538
    image_pos = idx; 

539
540
541
542
543
    array2d<rgb_pixel> img;
    display.clear_overlay();
    try
    {
        dlib::load_image(img, metadata.images[idx].filename);
544
        set_title(metadata.name + " #"+cast_to_string(idx)+": " +metadata.images[idx].filename);
545
546
547
548
549
550
    }
    catch (exception& e)
    {
        message_box("Error loading image", e.what());
    }

551

552
553
    unsigned long screen_width, screen_height;
    get_display_size(screen_width, screen_height);
554

555

556
557
558
559
560
    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;

561
562
563
564
565
566
    if (needed_width > 100 + screen_width)
        needed_width = screen_width - 100;
    if (needed_height > 100 + screen_height)
        needed_height = screen_height - 100;

    set_size(needed_width, needed_height);
567

568

569
570
    if (display_equialized_image)
        equalize_histogram(img);
571
    display.set_image(img);
Davis King's avatar
Davis King committed
572
    display.add_overlay(get_overlays(metadata.images[idx]));
573
574
575
576
}

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

577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
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;
594
            temp.parts = rects[i].parts;
595
            temp.ignore = rects[i].crossed_out;
596
597
598
599
600
601
602
603
604
605
606
607
            boxes.push_back(temp);
        }
    }
}

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

void metadata_editor::
on_overlay_label_changed(
)
{
    display.set_default_overlay_rect_label(trim(overlay_label.text()));
608
    display.set_default_overlay_rect_color(string_to_color(trim(overlay_label.text())));
609
610
611
}

// ----------------------------------------------------------------------------------------
612

613
614
615
616
617
618
619
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);
620
    display.set_default_overlay_rect_color(string_to_color(orect.label));
621
622
623
624
}

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

Davis King's avatar
Davis King committed
625
626
627
628
629
630
631
632
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;

633
634
635
636
637
638
    sout << wrap_string("You can add a new rectangle by holding the shift key, left clicking "
                        "the mouse, and dragging it.  New rectangles are given the label from the \"Next Label\" "
                        "field at the top of the application.  You can quickly edit the contents of the Next Label field "
                        "by hitting the tab key. Double clicking "
                        "a rectangle selects it and the delete key removes it.  You can also mark "
                        "a rectangle as ignored by hitting the i key when it is selected.  Ignored "
639
                        "rectangles are visually displayed with an X through them.  You can remove an image "
640
                        "entirely by selecting it in the list on the left and pressing alt+d."
Davis King's avatar
Davis King committed
641
642
                        ,0,0) << endl << endl;

643
644
645
646
647
648
    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;

649
    sout << wrap_string("Additionally, you can hold ctrl and then scroll the mouse wheel to zoom.  A normal left click "
650
651
                        "and drag allows you to navigate around the image.  Holding ctrl and "
                        "left clicking a rectangle will give it the label from the Next Label field. "
652
                        "Holding shift + right click and then dragging allows you to move things around. "
653
654
                        "Holding ctrl and pressing the up or down keyboard keys will propagate "
                        "rectangle labels from one image to the next and also skip empty images. " 
655
656
                        "Similarly, holding ctrl+shift will propagate entire boxes via a visual tracking " 
                        "algorithm from one image to the next. "
657
658
659
660
661
                        "Finally, typing a number on the keyboard will jump you to a specific image.",0,0) << endl << endl;

    sout << wrap_string("You can also toggle image histogram equalization by pressing the e key."
                        ,0,0) << endl;

Davis King's avatar
Davis King committed
662
663
664
665
666
667

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

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