simple_object_detector_py.h 5.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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 <boost/python/args.hpp>
#include <dlib/geometry.h>
#include <dlib/image_processing/frontal_face_detector.h>

namespace dlib
{
    typedef object_detector<scan_fhog_pyramid<pyramid_down<6> > > simple_object_detector;

16
17
18
19
    inline void split_rect_detections (
        std::vector<rect_detection>& rect_detections,
        std::vector<rectangle>& rectangles,
        std::vector<double>& detection_confidences,
20
        std::vector<double>& weight_indices
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    )
    {
        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);
        }
    }

35
36
37
    inline std::vector<dlib::rectangle> run_detector_with_upscale (
        dlib::simple_object_detector& detector,
        boost::python::object img,
38
39
        const unsigned int upsampling_amount,
        std::vector<double>& detection_confidences,
40
        std::vector<double>& weight_indices
41
42
43
44
    )
    {
        pyramid_down<2> pyr;

45
46
47
        std::vector<rectangle> rectangles;
        std::vector<rect_detection> rect_detections;

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

68
69
70
71
72
73
74
75
                detector(temp, rect_detections, 0.0);
                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;
76
77
78
79
80
81
82
            }
        }
        else if (is_rgb_python_image(img))
        {
            array2d<rgb_pixel> temp;
            if (upsampling_amount == 0)
            {
83
84
85
86
                detector(numpy_rgb_image(img), rect_detections, 0.0);
                split_rect_detections(rect_detections, rectangles,
                                      detection_confidences, weight_indices);
                return rectangles;
87
88
89
90
91
92
93
94
95
96
97
            }
            else
            {
                pyramid_up(numpy_rgb_image(img), temp, pyr);
                unsigned int levels = upsampling_amount-1;
                while (levels > 0)
                {
                    levels--;
                    pyramid_up(temp);
                }

98
99
100
101
102
103
104
105
                detector(temp, rect_detections, 0.0);
                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;
106
107
108
109
110
111
112
113
            }
        }
        else
        {
            throw dlib::error("Unsupported image type, must be 8bit gray or RGB image.");
        }
    }

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    inline boost::python::tuple run_rect_detector (
                                        dlib::simple_object_detector& detector,
                                        boost::python::object img,
                                        const unsigned int upsampling_amount)
    {
        boost::python::tuple t;

        std::vector<double> detection_confidences;
        std::vector<double> weight_indices;
        std::vector<rectangle> rectangles;

        rectangles = run_detector_with_upscale(detector, img, upsampling_amount,
                                               detection_confidences, weight_indices);

        return boost::python::make_tuple(rectangles,
                                         detection_confidences, weight_indices);
    }

132
133
134
135
136
137
138
139
140
    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) {}

141
142
143
144
        std::vector<dlib::rectangle> run_detector1 (boost::python::object img,
                                                    const unsigned int upsampling_amount_)
        {
            std::vector<double> detection_confidences;
145
            std::vector<double> weight_indices;
146
147
148
149

            return run_detector_with_upscale(detector, img, upsampling_amount_,
                detection_confidences, weight_indices);
        }
150
151

        std::vector<dlib::rectangle> run_detector2 (boost::python::object img)
152
153
        {
            std::vector<double> detection_confidences;
154
            std::vector<double> weight_indices;
155
156
157
158
159
            return run_detector_with_upscale(detector, img, upsampling_amount,
                detection_confidences, weight_indices);
        }


160
161
162
163
    };
}

#endif // DLIB_SIMPLE_OBJECT_DETECTOR_PY_H__