main.cpp 15.1 KB
Newer Older
1

2
#include "dlib/data_io.h"
3
#include "dlib/string.h"
4
#include "metadata_editor.h"
Davis King's avatar
Davis King committed
5
#include "convert_pascal_xml.h"
6
#include "convert_pascal_v1.h"
7
#include "convert_idl.h"
Davis King's avatar
Davis King committed
8
#include <dlib/cmd_line_parser.h>
9
#include <dlib/svm.h>
10

11
12
#include <iostream>
#include <fstream>
13
#include <string>
14
#include <set>
15

16
#include <dlib/dir_nav.h>
17
18


19
const char* VERSION = "0.6";
Davis King's avatar
Davis King committed
20
21


22
23
24
25

using namespace std;
using namespace dlib;

26
27
// ----------------------------------------------------------------------------------------

Davis King's avatar
Davis King committed
28
void create_new_dataset (
Davis King's avatar
Davis King committed
29
    const command_line_parser& parser
Davis King's avatar
Davis King committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
)
{
    using namespace dlib::image_dataset_metadata;

    const std::string filename = parser.option("c").argument();
    // make sure the file exists so we can use the get_parent_directory() command to
    // figure out it's parent directory.
    make_empty_file(filename);
    const std::string parent_dir = get_parent_directory(file(filename)).full_name();

    unsigned long depth = 0;
    if (parser.option("r"))
        depth = 30;

    dataset meta;
    meta.name = "imglab dataset";
    meta.comment = "Created by imglab tool.";
    for (unsigned long i = 0; i < parser.number_of_arguments(); ++i)
    {
        try
        {
            const string temp = strip_path(file(parser[i]).full_name(), parent_dir);
            meta.images.push_back(image(temp));
        }
        catch (dlib::file::file_not_found&)
        {
            // then parser[i] should be a directory

            std::vector<file> files = get_files_in_directory_tree(parser[i], 
                                                                  match_endings(".png .PNG .jpeg .JPEG .jpg .JPG .bmp .BMP .dng .DNG"),
                                                                  depth);
            sort(files.begin(), files.end());

            for (unsigned long j = 0; j < files.size(); ++j)
            {
                meta.images.push_back(image(strip_path(files[j].full_name(), parent_dir)));
            }
        }
    }

    save_image_dataset_metadata(meta, filename);
}

// ----------------------------------------------------------------------------------------
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
int split_dataset (
    const command_line_parser& parser
)
{
    if (parser.number_of_arguments() != 1)
    {
        cerr << "The --split option requires you to give one XML file on the command line." << endl;
        return EXIT_FAILURE;
    }

    const std::string label = parser.option("split").argument();

    dlib::image_dataset_metadata::dataset data, data_with, data_without;
    load_image_dataset_metadata(data, parser[0]);

    data_with.name = data.name;
    data_with.comment = data.comment;
    data_without.name = data.name;
    data_without.comment = data.comment;

    for (unsigned long i = 0; i < data.images.size(); ++i)
    {
        dlib::image_dataset_metadata::image temp = data.images[i];

        bool has_the_label = false;
        // check for the label we are looking for
        for (unsigned long j = 0; j < temp.boxes.size(); ++j)
        {
            if (temp.boxes[j].label == label)
            {
                has_the_label = true;
                break;
            }
        }

        if (has_the_label)
        {
            std::vector<dlib::image_dataset_metadata::box> boxes;
            // remove other labels
            for (unsigned long j = 0; j < temp.boxes.size(); ++j)
            {
                if (temp.boxes[j].label == label)
                {
                    // put only the boxes with the label we want into boxes
                    boxes.push_back(temp.boxes[j]);
                }
            }
            temp.boxes = boxes;
            data_with.images.push_back(temp);
        }
        else
        {
            data_without.images.push_back(temp);
        }
    }


    save_image_dataset_metadata(data_with, left_substr(parser[0],".") + "_with_"+label + ".xml");
    save_image_dataset_metadata(data_without, left_substr(parser[0],".") + "_without_"+label + ".xml");

    return EXIT_SUCCESS;
}

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

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
void print_all_labels (
    const dlib::image_dataset_metadata::dataset& data
)
{
    std::set<std::string> labels;
    for (unsigned long i = 0; i < data.images.size(); ++i)
    {
        for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
        {
            labels.insert(data.images[i].boxes[j].label);
        }
    }

    for (std::set<std::string>::iterator i = labels.begin(); i != labels.end(); ++i)
    {
        if (i->size() != 0)
        {
            cout << *i << endl;
        }
    }
}

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

164
165
166
167
168
void print_all_label_stats (
    const dlib::image_dataset_metadata::dataset& data
)
{
    std::map<std::string, running_stats<double> > area_stats, aspect_ratio;
169
    std::map<std::string, int> image_hits;
170
171
172
    std::set<std::string> labels;
    for (unsigned long i = 0; i < data.images.size(); ++i)
    {
173
        std::set<std::string> temp;
174
175
176
        for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
        {
            labels.insert(data.images[i].boxes[j].label);
177
            temp.insert(data.images[i].boxes[j].label);
178
179
180
181
182

            area_stats[data.images[i].boxes[j].label].add(data.images[i].boxes[j].rect.area());
            aspect_ratio[data.images[i].boxes[j].label].add(data.images[i].boxes[j].rect.width()/
                                                    (double)data.images[i].boxes[j].rect.height());
        }
183
184
185
186

        // count the number of images for each label
        for (std::set<std::string>::iterator i = temp.begin(); i != temp.end(); ++i)
            image_hits[*i] += 1;
187
188
    }

189
    cout << "Number of images: "<< data.images.size() << endl;
190
191
192
193
194
195
196
    cout << "Number of different labels: "<< labels.size() << endl << endl;

    for (std::set<std::string>::iterator i = labels.begin(); i != labels.end(); ++i)
    {
        if (i->size() != 0)
        {
            cout << "Label: "<< *i << endl;
197
            cout << "   number of images:      " << image_hits[*i] << endl;
198
199
200
201
202
203
204
205
206
207
208
209
210
211
            cout << "   number of occurrences: " << area_stats[*i].current_n() << endl;
            cout << "   min box area:    " << area_stats[*i].min() << endl;
            cout << "   max box area:    " << area_stats[*i].max() << endl;
            cout << "   mean box area:   " << area_stats[*i].mean() << endl;
            cout << "   stddev box area: " << area_stats[*i].stddev() << endl;
            cout << "   mean width/height ratio:   " << aspect_ratio[*i].mean() << endl;
            cout << "   stddev width/height ratio: " << aspect_ratio[*i].stddev() << endl;
            cout << endl;
        }
    }
}

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

Davis King's avatar
Davis King committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
void rename_labels (
    dlib::image_dataset_metadata::dataset& data,
    const std::string& from,
    const std::string& to
)
{
    for (unsigned long i = 0; i < data.images.size(); ++i)
    {
        for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
        {
            if (data.images[i].boxes[j].label == from)
                data.images[i].boxes[j].label = to;
        }
    }

}

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

231
232
233
234
235
int main(int argc, char** argv)
{
    try
    {

Davis King's avatar
Davis King committed
236
        command_line_parser parser;
237
238

        parser.add_option("h","Displays this information.");
239
240
241
        parser.add_option("v","Display version.");

        parser.set_group_name("Creating XML files");
242
        parser.add_option("c","Create an XML file named <arg> listing a set of images.",1);
243
        parser.add_option("r","Search directories recursively for images.");
244
245
246
247
        parser.add_option("convert","Convert foreign image Annotations from <arg> format to the imglab format. "
                          "Supported formats: pascal-xml, pascal-v1, idl.",1);

        parser.set_group_name("Viewing/Editing XML files");
248
        parser.add_option("l","List all the labels in the given XML file.");
249
        parser.add_option("stats","List detailed statistics on the object labels in the given XML file.");
Davis King's avatar
Davis King committed
250
        parser.add_option("rename", "Rename all labels of <arg1> to <arg2>.",2);
251
252
        parser.add_option("parts","The display will allow image parts to be labeled.  The set of allowable parts "
                          "defined in a space separated list contained in <arg>.",1);
253
        parser.add_option("rmdiff","Remove boxes marked as difficult.");
254
255
256
257
258
259
        parser.add_option("shuffle","Randomly shuffle the order of the images listed in file <arg>.");
        parser.add_option("seed", "When using --shuffle, set the random seed to the string <arg>.",1);
        parser.add_option("split", "Split the contents of an XML file into two separate files.  One containing the "
            "images with objects labeled <arg> and another file with all the other images.  Additionally, the file "
            "containing the <arg> labeled objects will not contain any other labels other than <arg>. "
            "That is, the images in the first file are stripped of all labels other than the <arg> labels.",1);
260
261
262

        parser.parse(argc, argv);

263
        const char* singles[] = {"h","c","r","l","convert","parts","rmdiff","seed", "shuffle", "split"};
264
        parser.check_one_time_options(singles);
265
266
        const char* c_sub_ops[] = {"r", "convert"};
        parser.check_sub_options("c", c_sub_ops);
267
        parser.check_sub_option("shuffle", "seed");
Davis King's avatar
Davis King committed
268
        parser.check_incompatible_options("c", "l");
269
        parser.check_incompatible_options("c", "rmdiff");
Davis King's avatar
Davis King committed
270
        parser.check_incompatible_options("c", "rename");
271
        parser.check_incompatible_options("c", "parts");
Davis King's avatar
Davis King committed
272
        parser.check_incompatible_options("l", "rename");
273
        parser.check_incompatible_options("l", "parts");
274
275
        parser.check_incompatible_options("convert", "l");
        parser.check_incompatible_options("convert", "rename");
276
        parser.check_incompatible_options("convert", "parts");
277
        parser.check_incompatible_options("rmdiff", "rename");
278
        const char* convert_args[] = {"pascal-xml","pascal-v1","idl"};
279
        parser.check_option_arg_range("convert", convert_args);
280
281
282

        if (parser.option("h"))
        {
283
            cout << "Usage: imglab [options] <image files/directories or XML file>\n";
284
            parser.print_options(cout);
Davis King's avatar
Davis King committed
285
            cout << endl << endl;
286
287
288
            return EXIT_SUCCESS;
        }

Davis King's avatar
Davis King committed
289
290
291
292
293
294
295
296
297
        if (parser.option("v"))
        {
            cout << "imglab v" << VERSION 
                 << "\nCompiled: " << __TIME__ << " " << __DATE__ 
                 << "\nWritten by Davis King\n";
            cout << "Check for updates at http://dlib.net\n\n";
            return EXIT_SUCCESS;
        }

298
299
        if (parser.option("c"))
        {
300
301
            if (parser.option("convert"))
            {
Davis King's avatar
Davis King committed
302
303
                if (parser.option("convert").argument() == "pascal-xml")
                    convert_pascal_xml(parser);
304
305
                else if (parser.option("convert").argument() == "pascal-v1")
                    convert_pascal_v1(parser);
306
307
                else if (parser.option("convert").argument() == "idl")
                    convert_idl(parser);
308
309
310
311
312
            }
            else
            {
                create_new_dataset(parser);
            }
313
314
            return EXIT_SUCCESS;
        }
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
        
        if (parser.option("rmdiff"))
        {
            if (parser.number_of_arguments() != 1)
            {
                cerr << "The --rmdiff 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]);
            for (unsigned long i = 0; i < data.images.size(); ++i)
            {
                std::vector<dlib::image_dataset_metadata::box> boxes;
                for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
                {
                    if (!data.images[i].boxes[j].difficult)
                        boxes.push_back(data.images[i].boxes[j]);
                }
                data.images[i].boxes = boxes;
            }
            save_image_dataset_metadata(data, parser[0]);
            return EXIT_SUCCESS;
        }
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
        if (parser.option("l"))
        {
            if (parser.number_of_arguments() != 1)
            {
                cerr << "The -l 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]);
            print_all_labels(data);
            return EXIT_SUCCESS;
        }

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
        if (parser.option("split"))
        {
            return split_dataset(parser);
        }

        if (parser.option("shuffle"))
        {
            if (parser.number_of_arguments() != 1)
            {
                cerr << "The -shuffle 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 string default_seed = cast_to_string(time(0));
            const string seed = get_option(parser, "seed", default_seed);
            dlib::rand rnd(seed);
            randomize_samples(data.images, rnd);
            save_image_dataset_metadata(data, parser[0]);
            return EXIT_SUCCESS;
        }

377
378
379
380
381
382
383
384
385
386
387
388
389
390
        if (parser.option("stats"))
        {
            if (parser.number_of_arguments() != 1)
            {
                cerr << "The --stats 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]);
            print_all_label_stats(data);
            return EXIT_SUCCESS;
        }

Davis King's avatar
Davis King committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
        if (parser.option("rename"))
        {
            if (parser.number_of_arguments() != 1)
            {
                cerr << "The --rename 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]);
            for (unsigned long i = 0; i < parser.option("rename").count(); ++i)
            {
                rename_labels(data, parser.option("rename").argument(0,i), parser.option("rename").argument(1,i));
            }
            save_image_dataset_metadata(data, parser[0]);
            return EXIT_SUCCESS;
        }

409
410
        if (parser.number_of_arguments() == 1)
        {
411
            metadata_editor editor(parser[0]);
412
413
414
415
416
417
418
419
            if (parser.option("parts"))
            {
                std::vector<string> parts = split(parser.option("parts").argument());
                for (unsigned long i = 0; i < parts.size(); ++i)
                {
                    editor.add_labelable_part_name(parts[i]);
                }
            }
420
            editor.wait_until_closed();
421
        }
422
423
424
    }
    catch (exception& e)
    {
425
        cerr << e.what() << endl;
426
427
428
429
        return EXIT_FAILURE;
    }
}

Davis King's avatar
Davis King committed
430
431
// ----------------------------------------------------------------------------------------