simple_object_detector_py.h 10.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
// Copyright (C) 2014  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_SIMPLE_OBJECT_DETECTOR_PY_H__
#define DLIB_SIMPLE_OBJECT_DETECTOR_PY_H__

#include <dlib/python.h>
#include <dlib/matrix.h>
#include <dlib/geometry.h>
#include <dlib/image_processing/frontal_face_detector.h>

11
12
namespace py = pybind11;

13
14
15
16
namespace dlib
{
    typedef object_detector<scan_fhog_pyramid<pyramid_down<6> > > simple_object_detector;

17
18
19
20
    inline void split_rect_detections (
        std::vector<rect_detection>& rect_detections,
        std::vector<rectangle>& rectangles,
        std::vector<double>& detection_confidences,
21
        std::vector<unsigned long>& weight_indices
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    )
    {
        rectangles.clear();
        detection_confidences.clear();
        weight_indices.clear();

        for (unsigned long i = 0; i < rect_detections.size(); ++i)
        {
            rectangles.push_back(rect_detections[i].rect);
            detection_confidences.push_back(rect_detections[i].detection_confidence);
            weight_indices.push_back(rect_detections[i].weight_index);
        }
    }

Jack Culpepper's avatar
Jack Culpepper committed
36
37

    inline std::vector<dlib::rectangle> run_detector_with_upscale1 (
38
        dlib::simple_object_detector& detector,
39
        py::object img,
40
        const unsigned int upsampling_amount,
41
        const double adjust_threshold,
42
        std::vector<double>& detection_confidences,
43
        std::vector<unsigned long>& weight_indices
44
45
46
47
    )
    {
        pyramid_down<2> pyr;

48
49
50
        std::vector<rectangle> rectangles;
        std::vector<rect_detection> rect_detections;

51
52
53
54
55
        if (is_gray_python_image(img))
        {
            array2d<unsigned char> temp;
            if (upsampling_amount == 0)
            {
56
                detector(numpy_gray_image(img), rect_detections, adjust_threshold);
57
58
59
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);
                return rectangles;
60
61
62
63
64
65
66
67
68
69
70
            }
            else
            {
                pyramid_up(numpy_gray_image(img), temp, pyr);
                unsigned int levels = upsampling_amount-1;
                while (levels > 0)
                {
                    levels--;
                    pyramid_up(temp);
                }

71
                detector(temp, rect_detections, adjust_threshold);
72
73
74
75
76
77
78
                for (unsigned long i = 0; i < rect_detections.size(); ++i)
                    rect_detections[i].rect = pyr.rect_down(rect_detections[i].rect,
                                                            upsampling_amount);
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);

                return rectangles;
79
80
81
82
83
84
85
            }
        }
        else if (is_rgb_python_image(img))
        {
            array2d<rgb_pixel> temp;
            if (upsampling_amount == 0)
            {
86
                detector(numpy_rgb_image(img), rect_detections, adjust_threshold);
87
88
89
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);
                return rectangles;
90
91
92
93
94
95
96
97
98
99
100
            }
            else
            {
                pyramid_up(numpy_rgb_image(img), temp, pyr);
                unsigned int levels = upsampling_amount-1;
                while (levels > 0)
                {
                    levels--;
                    pyramid_up(temp);
                }

101
                detector(temp, rect_detections, adjust_threshold);
102
103
104
105
106
107
108
                for (unsigned long i = 0; i < rect_detections.size(); ++i)
                    rect_detections[i].rect = pyr.rect_down(rect_detections[i].rect,
                                                            upsampling_amount);
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);

                return rectangles;
109
110
111
112
113
114
115
116
            }
        }
        else
        {
            throw dlib::error("Unsupported image type, must be 8bit gray or RGB image.");
        }
    }

117
118
    inline std::vector<dlib::rectangle> run_detectors_with_upscale1 (
        std::vector<simple_object_detector >& detectors,
119
        py::object img,
120
121
122
        const unsigned int upsampling_amount,
        const double adjust_threshold,
        std::vector<double>& detection_confidences,
123
        std::vector<unsigned long>& weight_indices
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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    )
    {
        pyramid_down<2> pyr;

        std::vector<rectangle> rectangles;
        std::vector<rect_detection> rect_detections;

        if (is_gray_python_image(img))
        {
            array2d<unsigned char> temp;
            if (upsampling_amount == 0)
            {
                evaluate_detectors(detectors, numpy_gray_image(img), rect_detections, adjust_threshold);
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);
                return rectangles;
            }
            else
            {
                pyramid_up(numpy_gray_image(img), temp, pyr);
                unsigned int levels = upsampling_amount-1;
                while (levels > 0)
                {
                    levels--;
                    pyramid_up(temp);
                }

                evaluate_detectors(detectors, temp, rect_detections, adjust_threshold);
                for (unsigned long i = 0; i < rect_detections.size(); ++i)
                    rect_detections[i].rect = pyr.rect_down(rect_detections[i].rect,
                                                            upsampling_amount);
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);

                return rectangles;
            }
        }
        else if (is_rgb_python_image(img))
        {
            array2d<rgb_pixel> temp;
            if (upsampling_amount == 0)
            {
                evaluate_detectors(detectors, numpy_rgb_image(img), rect_detections, adjust_threshold);
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);
                return rectangles;
            }
            else
            {
                pyramid_up(numpy_rgb_image(img), temp, pyr);
                unsigned int levels = upsampling_amount-1;
                while (levels > 0)
                {
                    levels--;
                    pyramid_up(temp);
                }

                evaluate_detectors(detectors, temp, rect_detections, adjust_threshold);
                for (unsigned long i = 0; i < rect_detections.size(); ++i)
                    rect_detections[i].rect = pyr.rect_down(rect_detections[i].rect,
                                                            upsampling_amount);
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);

                return rectangles;
            }
        }
        else
        {
            throw dlib::error("Unsupported image type, must be 8bit gray or RGB image.");
        }
    }

Jack Culpepper's avatar
Jack Culpepper committed
197
198
    inline std::vector<dlib::rectangle> run_detector_with_upscale2 (
        dlib::simple_object_detector& detector,
199
        py::object img,
Jack Culpepper's avatar
Jack Culpepper committed
200
        const unsigned int upsampling_amount
201

Jack Culpepper's avatar
Jack Culpepper committed
202
203
204
    )
    {
        std::vector<double> detection_confidences;
205
        std::vector<unsigned long> weight_indices;
206
        const double adjust_threshold = 0.0;
Jack Culpepper's avatar
Jack Culpepper committed
207
208

        return run_detector_with_upscale1(detector, img, upsampling_amount,
209
                                          adjust_threshold,
Jack Culpepper's avatar
Jack Culpepper committed
210
211
212
                                          detection_confidences, weight_indices);
    }

213
    inline py::tuple run_rect_detector (
Jack Culpepper's avatar
Jack Culpepper committed
214
        dlib::simple_object_detector& detector,
215
        py::object img,
216
217
        const unsigned int upsampling_amount,
        const double adjust_threshold)
218
    {
219
        py::tuple t;
220
221

        std::vector<double> detection_confidences;
222
        std::vector<unsigned long> weight_indices;
223
224
        std::vector<rectangle> rectangles;

Jack Culpepper's avatar
Jack Culpepper committed
225
        rectangles = run_detector_with_upscale1(detector, img, upsampling_amount,
226
                                                adjust_threshold,
Jack Culpepper's avatar
Jack Culpepper committed
227
                                                detection_confidences, weight_indices);
228

229
        return py::make_tuple(rectangles,
230
231
                              vector_to_python_list(detection_confidences), 
                              vector_to_python_list(weight_indices));
232
233
    }

234
235
236
    inline py::tuple run_multiple_rect_detectors (
        py::list& detectors,
        py::object img,
237
238
239
        const unsigned int upsampling_amount,
        const double adjust_threshold)
    {
240
        py::tuple t;
241
242
243
244
245
246

        std::vector<simple_object_detector > vector_detectors;
        const unsigned long num_detectors = len(detectors);
        // Now copy the data into dlib based objects.
        for (unsigned long i = 0; i < num_detectors; ++i)
        {
247
            vector_detectors.push_back(detectors[i].cast<simple_object_detector >());
248
        }
249

250
        std::vector<double> detection_confidences;
251
        std::vector<unsigned long> weight_indices;
252
253
254
255
256
257
        std::vector<rectangle> rectangles;

        rectangles = run_detectors_with_upscale1(vector_detectors, img, upsampling_amount,
                                                adjust_threshold,
                                                detection_confidences, weight_indices);

258
        return py::make_tuple(rectangles,
259
260
                              vector_to_python_list(detection_confidences),
                              vector_to_python_list(weight_indices));
261
262
263
264
    }



265
266
267
268
269
270
271
272
273
    struct simple_object_detector_py
    {
        simple_object_detector detector;
        unsigned int upsampling_amount;

        simple_object_detector_py() {}
        simple_object_detector_py(simple_object_detector& _detector, unsigned int _upsampling_amount) :
            detector(_detector), upsampling_amount(_upsampling_amount) {}

274
        std::vector<dlib::rectangle> run_detector1 (py::object img,
275
276
                                                    const unsigned int upsampling_amount_)
        {
Jack Culpepper's avatar
Jack Culpepper committed
277
            return run_detector_with_upscale2(detector, img, upsampling_amount_);
278
        }
279

280
        std::vector<dlib::rectangle> run_detector2 (py::object img)
281
        {
Jack Culpepper's avatar
Jack Culpepper committed
282
            return run_detector_with_upscale2(detector, img, upsampling_amount);
283
284
285
        }


286
287
288
289
    };
}

#endif // DLIB_SIMPLE_OBJECT_DETECTOR_PY_H__