davisking / dlib

A toolkit for making real world machine learning and data analysis applications in C++
http://dlib.net
Boost Software License 1.0
13.58k stars 3.38k forks source link

How to get 128D vector from 68+N landmarks? #1775

Closed sherwinkh closed 5 years ago

sherwinkh commented 5 years ago

Dlib just use 5 or 68 landmarks(shape_predictor_5_face_landmarks.dat/shape_predictor_68_face_landmarks.dat) to extract 128D vector. If I have a 68+N landmarks model,how can I get the 128D vector from the 68+N landmarks model?

    inline chip_details get_face_chip_details (
        const full_object_detection& det,
        const unsigned long size = 200,
        const double padding = 0.2
    )
    {
        DLIB_CASSERT(det.num_parts() == 68 || det.num_parts() == 5,
            "\t chip_details get_face_chip_details()"
            << "\n\t You have to give either a 5 point or 68 point face landmarking output to this function. "
            << "\n\t det.num_parts(): " << det.num_parts()
        );
        DLIB_CASSERT(padding >= 0 && size > 0,
            "\t chip_details get_face_chip_details()"
            << "\n\t Invalid inputs were given to this function."
            << "\n\t padding: " << padding 
            << "\n\t size:    " << size 
            );

        std::vector<dpoint> from_points, to_points;
        if (det.num_parts() == 5)
        {
            dpoint p0(0.8595674595992, 0.2134981538014);
            dpoint p1(0.6460604764104, 0.2289674387677);
            dpoint p2(0.1205750620789, 0.2137274526848);
            dpoint p3(0.3340850613712, 0.2290642403242);
            dpoint p4(0.4901123135679, 0.6277975316475);

            p0 = (padding+p0)/(2*padding+1);
            p1 = (padding+p1)/(2*padding+1);
            p2 = (padding+p2)/(2*padding+1);
            p3 = (padding+p3)/(2*padding+1);
            p4 = (padding+p4)/(2*padding+1);

            from_points.push_back(p0*size);
            to_points.push_back(det.part(0));

            from_points.push_back(p1*size);
            to_points.push_back(det.part(1));

            from_points.push_back(p2*size);
            to_points.push_back(det.part(2));

            from_points.push_back(p3*size);
            to_points.push_back(det.part(3));

            from_points.push_back(p4*size);
            to_points.push_back(det.part(4));
        }
        else
        {
            // Average positions of face points 17-67
            const double mean_face_shape_x[] = {
                0.000213256, 0.0752622, 0.18113, 0.29077, 0.393397, 0.586856, 0.689483, 0.799124,
                0.904991, 0.98004, 0.490127, 0.490127, 0.490127, 0.490127, 0.36688, 0.426036,
                0.490127, 0.554217, 0.613373, 0.121737, 0.187122, 0.265825, 0.334606, 0.260918,
                0.182743, 0.645647, 0.714428, 0.793132, 0.858516, 0.79751, 0.719335, 0.254149,
                0.340985, 0.428858, 0.490127, 0.551395, 0.639268, 0.726104, 0.642159, 0.556721,
                0.490127, 0.423532, 0.338094, 0.290379, 0.428096, 0.490127, 0.552157, 0.689874,
                0.553364, 0.490127, 0.42689
            };
            const double mean_face_shape_y[] = {
                0.106454, 0.038915, 0.0187482, 0.0344891, 0.0773906, 0.0773906, 0.0344891,
                0.0187482, 0.038915, 0.106454, 0.203352, 0.307009, 0.409805, 0.515625, 0.587326,
                0.609345, 0.628106, 0.609345, 0.587326, 0.216423, 0.178758, 0.179852, 0.231733,
                0.245099, 0.244077, 0.231733, 0.179852, 0.178758, 0.216423, 0.244077, 0.245099,
                0.780233, 0.745405, 0.727388, 0.742578, 0.727388, 0.745405, 0.780233, 0.864805,
                0.902192, 0.909281, 0.902192, 0.864805, 0.784792, 0.778746, 0.785343, 0.778746,
                0.784792, 0.824182, 0.831803, 0.824182
            };

            COMPILE_TIME_ASSERT(sizeof(mean_face_shape_x)/sizeof(double) == 68-17);

            for (unsigned long i = 17; i < det.num_parts(); ++i)
            {
                // Ignore the lower lip
                if ((55 <= i && i <= 59) || (65 <= i && i <= 67))
                    continue;
                // Ignore the eyebrows 
                if (17 <= i && i <= 26)
                    continue;

                dpoint p;
                p.x() = (padding+mean_face_shape_x[i-17])/(2*padding+1);
                p.y() = (padding+mean_face_shape_y[i-17])/(2*padding+1);
                from_points.push_back(p*size);
                to_points.push_back(det.part(i));
            }
        }

        return chip_details(from_points, to_points, chip_dims(size,size));
    }
dlib-issue-bot commented 5 years ago

Warning: this issue has been inactive for 35 days and will be automatically closed on 2019-07-05 if there is no further activity.

If you are waiting for a response but haven't received one it's possible your question is somehow inappropriate. E.g. it is off topic, you didn't follow the issue submission instructions, or your question is easily answerable by reading the FAQ, dlib's official compilation instructions, dlib's API documentation, or a Google search.