gui.cpp 4.71 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
37
}

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

void image_window_set_image (
    image_window& win,
38
    py::object img
Patrick Snape's avatar
Patrick Snape committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
)
{
    if (is_gray_python_image(img))
        return win.set_image(numpy_gray_image(img));
    else if (is_rgb_python_image(img))
        return win.set_image(numpy_rgb_image(img));
    else
        throw dlib::error("Unsupported image type, must be 8bit gray or RGB image.");
}

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

58
59
60
61
62
63
64
65
66
67
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
68
69
70
71
72
73
void add_overlay_parts (
    image_window& win,
    const full_object_detection& detection,
    const rgb_pixel& color
)
{
74
    win.add_overlay(render_face_detections(detection, color));
Patrick Snape's avatar
Patrick Snape committed
75
76
}

77
std::shared_ptr<image_window> make_image_window_from_image(py::object img)
Patrick Snape's avatar
Patrick Snape committed
78
{
79
    auto win = std::make_shared<image_window>();
Patrick Snape's avatar
Patrick Snape committed
80
81
82
83
    image_window_set_image(*win, img);
    return win;
}

84
std::shared_ptr<image_window> make_image_window_from_image_and_title(py::object img, const string& title)
Patrick Snape's avatar
Patrick Snape committed
85
{
86
    auto win = std::make_shared<image_window>();
Patrick Snape's avatar
Patrick Snape committed
87
88
89
90
91
92
93
    image_window_set_image(*win, img);
    win->set_title(title);
    return win;
}

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

94
void bind_gui(py::module& m)
Patrick Snape's avatar
Patrick Snape committed
95
96
97
98
99
{
    {
    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);
100
    py::class_<type, std::shared_ptr<type>>(m, "image_window",
Patrick Snape's avatar
Patrick Snape committed
101
        "This is a GUI window capable of showing images on the screen.")
102
103
        .def(py::init())
        .def(py::init(&make_image_window_from_image),
Patrick Snape's avatar
Patrick Snape committed
104
            "Create an image window that displays the given numpy image.")
105
        .def(py::init(&make_image_window_from_image_and_title),
Patrick Snape's avatar
Patrick Snape committed
106
            "Create an image window that displays the given numpy image and also has the given title.")
107
        .def("set_image", image_window_set_image_simple_detector_py, py::arg("detector"),
Patrick Snape's avatar
Patrick Snape committed
108
            "Make the image_window display the given HOG detector's filters.")
109
        .def("set_image", image_window_set_image_fhog_detector, py::arg("detector"),
Patrick Snape's avatar
Patrick Snape committed
110
            "Make the image_window display the given HOG detector's filters.")
111
112
113
        .def("set_image", image_window_set_image, py::arg("image"),
            "Make the image_window display the given image.")
        .def("set_title", (set_title_funct)&type::set_title, py::arg("title"),
Patrick Snape's avatar
Patrick Snape committed
114
115
            "Set the title of the window to the given value.")
        .def("clear_overlay", &type::clear_overlay, "Remove all overlays from the image_window.")
116
        .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
117
            "Add a list of rectangles to the image_window. They will be displayed as red boxes by default, but the color can be passed.")
118
        .def("add_overlay", add_overlay_rect, py::arg("rectangle"), py::arg("color")=rgb_pixel(255, 0, 0),
Patrick Snape's avatar
Patrick Snape committed
119
            "Add a rectangle to the image_window.  It will be displayed as a red box by default, but the color can be passed.")
120
        .def("add_overlay", add_overlay_drect, py::arg("rectangle"), py::arg("color")=rgb_pixel(255, 0, 0),
121
            "Add a rectangle to the image_window.  It will be displayed as a red box by default, but the color can be passed.")
122
        .def("add_overlay", add_overlay_parts, py::arg("detection"), py::arg("color")=rgb_pixel(0, 0, 255),
Patrick Snape's avatar
Patrick Snape committed
123
124
125
126
127
128
            "Add full_object_detection parts to the image window. They will be displayed as blue lines by default, but the color can be passed.")
        .def("wait_until_closed", &type::wait_until_closed,
            "This function blocks until the window is closed.");
    }
}
#endif