main.cpp 7.75 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"
8

9
10
#include <iostream>
#include <fstream>
11
#include <string>
12
#include <set>
13

14
#include <dlib/dir_nav.h>
15
16


17
const char* VERSION = "0.4";
Davis King's avatar
Davis King committed
18
19


20
21
22
23

using namespace std;
using namespace dlib;

24
25
// ----------------------------------------------------------------------------------------

Davis King's avatar
Davis King committed
26
27
28
29
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
void create_new_dataset (
    const parser_type& parser
)
{
    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);
}

// ----------------------------------------------------------------------------------------
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
        }
    }
}

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

Davis King's avatar
Davis King committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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;
        }
    }

}

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

116
117
118
119
120
121
122
123
124
int main(int argc, char** argv)
{
    try
    {

        parser_type parser;

        parser.add_option("h","Displays this information.");
        parser.add_option("c","Create an XML file named <arg> listing a set of images.",1);
125
        parser.add_option("r","Search directories recursively for images.");
126
        parser.add_option("l","List all the labels in the given XML file.");
Davis King's avatar
Davis King committed
127
        parser.add_option("rename", "Rename all labels of <arg1> to <arg2>.",2);
Davis King's avatar
Davis King committed
128
        parser.add_option("v","Display version.");
129
130
        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);
131
        parser.add_option("convert","Convert foreign image Annotations from <arg> format to the imglab format. "
Davis King's avatar
Davis King committed
132
                          "Supported formats: pascal-xml, pascal-v1, idl.",1);
133
134
135

        parser.parse(argc, argv);

136
        const char* singles[] = {"h","c","r","l","convert","parts"};
137
        parser.check_one_time_options(singles);
138
139
        const char* c_sub_ops[] = {"r", "convert"};
        parser.check_sub_options("c", c_sub_ops);
Davis King's avatar
Davis King committed
140
141
        parser.check_incompatible_options("c", "l");
        parser.check_incompatible_options("c", "rename");
142
        parser.check_incompatible_options("c", "parts");
Davis King's avatar
Davis King committed
143
        parser.check_incompatible_options("l", "rename");
144
        parser.check_incompatible_options("l", "parts");
145
146
        parser.check_incompatible_options("convert", "l");
        parser.check_incompatible_options("convert", "rename");
147
        parser.check_incompatible_options("convert", "parts");
148
        const char* convert_args[] = {"pascal-xml","pascal-v1","idl"};
149
        parser.check_option_arg_range("convert", convert_args);
150
151
152

        if (parser.option("h"))
        {
153
            cout << "Usage: imglab [options] <image files/directories or XML file>\n";
154
            parser.print_options(cout);
Davis King's avatar
Davis King committed
155
            cout << endl << endl;
156
157
158
            return EXIT_SUCCESS;
        }

Davis King's avatar
Davis King committed
159
160
161
162
163
164
165
166
167
        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;
        }

168
169
        if (parser.option("c"))
        {
170
171
            if (parser.option("convert"))
            {
Davis King's avatar
Davis King committed
172
173
                if (parser.option("convert").argument() == "pascal-xml")
                    convert_pascal_xml(parser);
174
175
                else if (parser.option("convert").argument() == "pascal-v1")
                    convert_pascal_v1(parser);
176
177
                else if (parser.option("convert").argument() == "idl")
                    convert_idl(parser);
178
179
180
181
182
            }
            else
            {
                create_new_dataset(parser);
            }
183
184
185
            return EXIT_SUCCESS;
        }

186
187
188
189
190
191
192
193
194
195
196
197
198
199
        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;
        }

Davis King's avatar
Davis King committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        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;
        }

218
219
        if (parser.number_of_arguments() == 1)
        {
220
            metadata_editor editor(parser[0]);
221
222
223
224
225
226
227
228
            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]);
                }
            }
229
            editor.wait_until_closed();
230
        }
231
232
233
    }
    catch (exception& e)
    {
234
        cerr << e.what() << endl;
235
236
237
238
        return EXIT_FAILURE;
    }
}

Davis King's avatar
Davis King committed
239
240
// ----------------------------------------------------------------------------------------