load_image_dataset.h 7.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright (C) 2012  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_LOAD_IMAGE_DaTASET_H__
#define DLIB_LOAD_IMAGE_DaTASET_H__

#include "load_image_dataset_abstract.h"
#include "../misc_api.h"
#include "../dir_nav.h"
#include "../image_io.h"
#include "../array.h"
#include <vector>
#include "../geometry.h"
#include "image_dataset_metadata.h"
#include <string>
15
#include "../image_processing/full_object_detection.h"
16
17
18
19
20
21
22
23
24
25
26


namespace dlib
{

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

    template <
        typename image_type, 
        typename MM
        >
27
    std::vector<std::vector<rectangle> > load_image_dataset (
28
29
30
        array<image_type,MM>& images,
        std::vector<std::vector<rectangle> >& object_locations,
        const std::string& filename,
31
32
        const std::string& label,
        bool skip_empty_images = false
33
34
35
36
37
38
    )
    {
        images.clear();
        object_locations.clear();
        const std::string old_working_dir = get_current_dir();

39
40
        std::vector<std::vector<rectangle> > ignored_rects;

41
42
43
44
45
46
47
48
49
50
51
52
        // Set the current directory to be the one that contains the
        // metadata file. We do this because the file might contain
        // file paths which are relative to this folder.
        const std::string parent_dir = get_parent_directory(file(filename)).full_name();
        set_current_dir(parent_dir);


        using namespace dlib::image_dataset_metadata;

        dataset data;
        load_image_dataset_metadata(data, filename);

53
        image_type img;
54
        std::vector<rectangle> rects, ignored;
55
56
57
        for (unsigned long i = 0; i < data.images.size(); ++i)
        {
            rects.clear();
58
            ignored.clear();
59
60
61
62
            for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
            {
                if (label.size() == 0 || data.images[i].boxes[j].label == label)
                {
63
64
65
66
                    if (data.images[i].boxes[j].ignore)
                        ignored.push_back(data.images[i].boxes[j].rect);
                    else
                        rects.push_back(data.images[i].boxes[j].rect);
67
68
                }
            }
69
70
71
72

            if (!skip_empty_images || rects.size() != 0)
            {
                object_locations.push_back(rects);
73
                ignored_rects.push_back(ignored);
74
75
76
                load_image(img, data.images[i].filename);
                images.push_back(img);
            }
77
78
79
        }

        set_current_dir(old_working_dir);
80
        return ignored_rects;
81
82
83
84
    }

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

Davis King's avatar
Davis King committed
85
86
87
88
    template <
        typename image_type, 
        typename MM
        >
89
    std::vector<std::vector<rectangle> > load_image_dataset (
90
91
92
93
94
        array<image_type,MM>& images,
        std::vector<std::vector<rectangle> >& object_locations,
        const std::string& filename
    )
    {
95
        return load_image_dataset(images, object_locations, filename, "");
96
97
98
    }

// ----------------------------------------------------------------------------------------
99
100
101
102
103
104
105
106
107

    template <
        typename image_type, 
        typename MM
        >
    std::vector<std::string> load_image_dataset (
        array<image_type,MM>& images,
        std::vector<std::vector<full_object_detection> >& object_locations,
        const std::string& filename,
108
109
        const std::string& label,
        bool skip_empty_images = false
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    )
    {
        images.clear();
        object_locations.clear();
        const std::string old_working_dir = get_current_dir();

        // Set the current directory to be the one that contains the
        // metadata file. We do this because the file might contain
        // file paths which are relative to this folder.
        const std::string parent_dir = get_parent_directory(file(filename)).full_name();
        set_current_dir(parent_dir);


        using namespace dlib::image_dataset_metadata;

        dataset data;
        load_image_dataset_metadata(data, filename);
        std::set<std::string> all_parts;

        // find out what parts are being used in the dataset.  Store results in all_parts.
        for (unsigned long i = 0; i < data.images.size(); ++i)
        {
            for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
            {
                if (label.size() == 0 || data.images[i].boxes[j].label == label)
                {
                    const std::map<std::string,point>& parts = data.images[i].boxes[j].parts;
                    std::map<std::string,point>::const_iterator itr;

                    for (itr = parts.begin(); itr != parts.end(); ++itr)
                    {
                        all_parts.insert(itr->first);
                    }
                }
            }
        }

        // make a mapping between part names and the integers [0, all_parts.size())
        std::map<std::string,int> parts_idx;
        std::vector<std::string> ret_parts_list;
        for (std::set<std::string>::iterator i = all_parts.begin(); i != all_parts.end(); ++i)
        {
            parts_idx[*i] = ret_parts_list.size();
            ret_parts_list.push_back(*i);
        }

156
        image_type img;
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        std::vector<full_object_detection> object_dets;
        for (unsigned long i = 0; i < data.images.size(); ++i)
        {
            object_dets.clear();
            for (unsigned long j = 0; j < data.images[i].boxes.size(); ++j)
            {
                if (label.size() == 0 || data.images[i].boxes[j].label == label)
                {
                    std::vector<point> partlist(parts_idx.size(), OBJECT_PART_NOT_PRESENT);

                    // populate partlist with all the parts present in this box.
                    const std::map<std::string,point>& parts = data.images[i].boxes[j].parts;
                    std::map<std::string,point>::const_iterator itr;
                    for (itr = parts.begin(); itr != parts.end(); ++itr)
                    {
                        partlist[parts_idx[itr->first]] = itr->second;
                    }

                    object_dets.push_back(full_object_detection(data.images[i].boxes[j].rect, partlist));
                }
            }
178
179
180
181
182
183
184

            if (!skip_empty_images || object_dets.size() != 0)
            {
                object_locations.push_back(object_dets);
                load_image(img, data.images[i].filename);
                images.push_back(img);
            }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
        }

        set_current_dir(old_working_dir);

        return ret_parts_list;
    }

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

    template <
        typename image_type, 
        typename MM
        >
    std::vector<std::string> load_image_dataset (
        array<image_type,MM>& images,
        std::vector<std::vector<full_object_detection> >& object_locations,
        const std::string& filename
    )
    {
        return load_image_dataset(images, object_locations, filename, "");
    }

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

209
210
211
212
}

#endif // DLIB_LOAD_IMAGE_DaTASET_H__