gui.cpp 9.19 KB
Newer Older
Patrick Snape's avatar
Patrick Snape committed
1
2
#ifndef DLIB_NO_GUI_SUPPORT

Davis King's avatar
Davis King committed
3
#include "opaque_types.h"
Patrick Snape's avatar
Patrick Snape committed
4
5
6
7
8
#include <dlib/python.h>
#include <dlib/geometry.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/gui_widgets.h>
9
#include "simple_object_detector_py.h"
Patrick Snape's avatar
Patrick Snape committed
10
11
12

using namespace dlib;
using namespace std;
13
14

namespace py = pybind11;
Patrick Snape's avatar
Patrick Snape committed
15
16
17
18
19

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

void image_window_set_image_fhog_detector (
    image_window& win,
20
    const simple_object_detector& det
Patrick Snape's avatar
Patrick Snape committed
21
22
23
24
25
)
{
    win.set_image(draw_fhog(det));
}

26
void image_window_set_image_simple_detector_py (
Patrick Snape's avatar
Patrick Snape committed
27
    image_window& win,
28
    const simple_object_detector_py& det
Patrick Snape's avatar
Patrick Snape committed
29
30
)
{
31
    win.set_image(draw_fhog(det.detector));
Patrick Snape's avatar
Patrick Snape committed
32
33
34
35
}

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

36
template <typename T>
Patrick Snape's avatar
Patrick Snape committed
37
38
void image_window_set_image (
    image_window& win,
39
    const numpy_image<T>& img
Patrick Snape's avatar
Patrick Snape committed
40
41
)
{
42
    win.set_image(img);
Patrick Snape's avatar
Patrick Snape committed
43
44
45
46
47
48
49
50
}

void add_overlay_rect (
    image_window& win,
    const rectangle& rect,
    const rgb_pixel& color
)
{
51
    win.add_overlay(rect, color);
Patrick Snape's avatar
Patrick Snape committed
52
53
}

54
55
56
57
58
59
60
61
62
63
void add_overlay_drect (
    image_window& win,
    const drectangle& drect,
    const rgb_pixel& color
)
{
    rectangle rect(drect.left(), drect.top(), drect.right(), drect.bottom());
    win.add_overlay(rect, color);
}

Patrick Snape's avatar
Patrick Snape committed
64
65
66
67
68
69
void add_overlay_parts (
    image_window& win,
    const full_object_detection& detection,
    const rgb_pixel& color
)
{
70
71
72
73
74
75
76
77
78
79
80
81
    if (detection.num_parts() == 5 || detection.num_parts() == 68)
    {
        win.add_overlay(render_face_detections(detection, color));
    }
    else
    {
        std::vector<image_display::overlay_circle> tmp;
        for (unsigned long i = 0; i < detection.num_parts(); ++i)
            tmp.emplace_back(detection.part(i), 0.5, color, std::to_string(i));
        win.add_overlay(tmp);
        win.add_overlay(detection.get_rect());
    }
Patrick Snape's avatar
Patrick Snape committed
82
83
}

84
85
86
87
88
89
90
91
92
void add_overlay_line (
    image_window& win,
    const line& l,
    const rgb_pixel& color
)
{
    win.add_overlay(l,color);
}

93
94
95
96
97
98
99
100
template <typename point_type>
void add_overlay_circle (
    image_window& win,
    const point_type& c,
    const double radius,
    const rgb_pixel& color
)
{
101
    win.add_overlay(image_window::overlay_circle(c,radius,color));
102
103
}

104
105
template <typename T>
std::shared_ptr<image_window> make_image_window_from_image(const numpy_image<T>& img)
Patrick Snape's avatar
Patrick Snape committed
106
{
107
    auto win = std::make_shared<image_window>();
Patrick Snape's avatar
Patrick Snape committed
108
109
110
111
    image_window_set_image(*win, img);
    return win;
}

112
113
template <typename T>
std::shared_ptr<image_window> make_image_window_from_image_and_title(const numpy_image<T>& img, const string& title)
Patrick Snape's avatar
Patrick Snape committed
114
{
115
    auto win = std::make_shared<image_window>();
Patrick Snape's avatar
Patrick Snape committed
116
117
118
119
120
    image_window_set_image(*win, img);
    win->set_title(title);
    return win;
}

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
std::shared_ptr<image_window> make_image_window_from_detector(const simple_object_detector& detector)
{
    auto win = std::make_shared<image_window>();
    win->set_image(draw_fhog(detector));
    return win;
}

std::shared_ptr<image_window> make_image_window_from_detector_py(const simple_object_detector_py& detector)
{
    auto win = std::make_shared<image_window>();
    win->set_image(draw_fhog(detector.detector));
    return win;
}

std::shared_ptr<image_window> make_image_window_from_detector_and_title(const simple_object_detector& detector, const string& title)
{
    auto win = std::make_shared<image_window>();
    win->set_image(draw_fhog(detector));
    win->set_title(title);
    return win;
}

std::shared_ptr<image_window> make_image_window_from_detector_py_and_title(const simple_object_detector_py& detector, const string& title)
{
    auto win = std::make_shared<image_window>();
    win->set_image(draw_fhog(detector.detector));
    win->set_title(title);
    return win;
}

Patrick Snape's avatar
Patrick Snape committed
151
152
// ----------------------------------------------------------------------------------------

153
void bind_gui(py::module& m)
Patrick Snape's avatar
Patrick Snape committed
154
155
156
157
158
{
    {
    typedef image_window type;
    typedef void (image_window::*set_title_funct)(const std::string&);
    typedef void (image_window::*add_overlay_funct)(const std::vector<rectangle>& r, rgb_pixel p);
159
160
161
162

    const char* docs1 = "Create an image window that displays the given numpy image.";
    const char* docs2 = "Create an image window that displays the given numpy image and also has the given title.";
    const char* docs3 = "Make the image_window display the given image.";
163
    py::class_<type, std::shared_ptr<type>>(m, "image_window",
Patrick Snape's avatar
Patrick Snape committed
164
        "This is a GUI window capable of showing images on the screen.")
165
        .def(py::init())
166
167
168
169
        .def(py::init(&make_image_window_from_detector))
        .def(py::init(&make_image_window_from_detector_py))
        .def(py::init(&make_image_window_from_detector_and_title))
        .def(py::init(&make_image_window_from_detector_py_and_title))
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        .def(py::init(&make_image_window_from_image<uint8_t>))
        .def(py::init(&make_image_window_from_image<uint16_t>))
        .def(py::init(&make_image_window_from_image<uint32_t>))
        .def(py::init(&make_image_window_from_image<uint64_t>))
        .def(py::init(&make_image_window_from_image<int8_t>))
        .def(py::init(&make_image_window_from_image<int16_t>))
        .def(py::init(&make_image_window_from_image<int32_t>))
        .def(py::init(&make_image_window_from_image<int64_t>))
        .def(py::init(&make_image_window_from_image<float>))
        .def(py::init(&make_image_window_from_image<double>))
        .def(py::init(&make_image_window_from_image<rgb_pixel>), docs1)
        .def(py::init(&make_image_window_from_image_and_title<uint8_t>))
        .def(py::init(&make_image_window_from_image_and_title<uint16_t>))
        .def(py::init(&make_image_window_from_image_and_title<uint32_t>))
        .def(py::init(&make_image_window_from_image_and_title<uint64_t>))
        .def(py::init(&make_image_window_from_image_and_title<int8_t>))
        .def(py::init(&make_image_window_from_image_and_title<int16_t>))
        .def(py::init(&make_image_window_from_image_and_title<int32_t>))
        .def(py::init(&make_image_window_from_image_and_title<int64_t>))
        .def(py::init(&make_image_window_from_image_and_title<float>))
        .def(py::init(&make_image_window_from_image_and_title<double>))
        .def(py::init(&make_image_window_from_image_and_title<rgb_pixel>), docs2)
192
        .def("set_image", image_window_set_image_simple_detector_py, py::arg("detector"),
Patrick Snape's avatar
Patrick Snape committed
193
            "Make the image_window display the given HOG detector's filters.")
194
        .def("set_image", image_window_set_image_fhog_detector, py::arg("detector"),
Patrick Snape's avatar
Patrick Snape committed
195
            "Make the image_window display the given HOG detector's filters.")
196
197
198
199
200
201
202
203
204
205
206
        .def("set_image", image_window_set_image<uint8_t>, py::arg("image"))
        .def("set_image", image_window_set_image<uint16_t>, py::arg("image"))
        .def("set_image", image_window_set_image<uint32_t>, py::arg("image"))
        .def("set_image", image_window_set_image<uint64_t>, py::arg("image"))
        .def("set_image", image_window_set_image<int8_t>, py::arg("image"))
        .def("set_image", image_window_set_image<int16_t>, py::arg("image"))
        .def("set_image", image_window_set_image<int32_t>, py::arg("image"))
        .def("set_image", image_window_set_image<int64_t>, py::arg("image"))
        .def("set_image", image_window_set_image<float>, py::arg("image"))
        .def("set_image", image_window_set_image<double>, py::arg("image"))
        .def("set_image", image_window_set_image<rgb_pixel>, py::arg("image"), docs3)
207
        .def("set_title", (set_title_funct)&type::set_title, py::arg("title"),
Patrick Snape's avatar
Patrick Snape committed
208
209
            "Set the title of the window to the given value.")
        .def("clear_overlay", &type::clear_overlay, "Remove all overlays from the image_window.")
210
        .def("add_overlay", (add_overlay_funct)&type::add_overlay<rgb_pixel>, py::arg("rectangles"), py::arg("color")=rgb_pixel(255, 0, 0),
Patrick Snape's avatar
Patrick Snape committed
211
            "Add a list of rectangles to the image_window. They will be displayed as red boxes by default, but the color can be passed.")
212
        .def("add_overlay", add_overlay_rect, py::arg("rectangle"), py::arg("color")=rgb_pixel(255, 0, 0),
Patrick Snape's avatar
Patrick Snape committed
213
            "Add a rectangle to the image_window.  It will be displayed as a red box by default, but the color can be passed.")
214
        .def("add_overlay", add_overlay_drect, py::arg("rectangle"), py::arg("color")=rgb_pixel(255, 0, 0),
215
            "Add a rectangle to the image_window.  It will be displayed as a red box by default, but the color can be passed.")
216
        .def("add_overlay", add_overlay_parts, py::arg("detection"), py::arg("color")=rgb_pixel(0, 0, 255),
Patrick Snape's avatar
Patrick Snape committed
217
            "Add full_object_detection parts to the image window. They will be displayed as blue lines by default, but the color can be passed.")
218
219
        .def("add_overlay", add_overlay_line, py::arg("line"), py::arg("color")=rgb_pixel(255, 0, 0),
            "Add line to the image window.")
220
221
222
223
        .def("add_overlay_circle", add_overlay_circle<point>, py::arg("center"), py::arg("radius"), py::arg("color")=rgb_pixel(255, 0, 0),
            "Add circle to the image window.")
        .def("add_overlay_circle", add_overlay_circle<dpoint>, py::arg("center"), py::arg("radius"), py::arg("color")=rgb_pixel(255, 0, 0),
            "Add circle to the image window.")
Patrick Snape's avatar
Patrick Snape committed
224
225
226
227
228
        .def("wait_until_closed", &type::wait_until_closed,
            "This function blocks until the window is closed.");
    }
}
#endif