Dlib在kurento opencv过滤器中未检测到人脸

时间:2018-12-18 08:51:53

标签: c++ opencv dlib kurento

我创建了一个opencv过滤器,它可以检测一个人是否对Kurento WebRTC框架眨眼。我的代码可在独立的opencv应用程序中使用。但是,一旦我转换为Kurento的opencv过滤器,它就会开始播放。当编译模块/过滤器时没有优化标志时,它将短暂地检测人脸并在眼睛周围绘制轮廓。但是,在使用优化标志编译模块/过滤器之后,性能有所提高,但是没有检测到人脸。这是过滤器中的代码:

 void BlinkDetectorOpenCVImpl::process(cv::Mat &mat) {

            std::vector <dlib::rectangle> faces;

            // Just resize input image if you want
            resize(mat, mat, Size(800, 450));

            cv_image <rgb_alpha_pixel> cimg(mat);
            dlib::array2d<unsigned char> img_gray;
            dlib::assign_image(img_gray, cimg);
            faces = detector(img_gray);
            std::cout << "XXXXXXXXXXXXXXXXXXXXX FACES: " << faces.size() << std::endl;
            std::vector <full_object_detection> shapes;
            for (unsigned long i = 0; i < faces.size(); ++i) {
                full_object_detection shape = pose_model(cimg, faces[i]);
                std::vector <Point> left_eye_points = get_points_for_eye(shape, LEFT_EYE_START, LEFT_EYE_END);
                std::vector <Point> right_eye_points = get_points_for_eye(shape, RIGHT_EYE_START, RIGHT_EYE_END);
                double left_eye_ear = get_eye_aspect_ratio(left_eye_points);
                double right_eye_ear = get_eye_aspect_ratio(right_eye_points);

                double ear = (left_eye_ear + right_eye_ear) / 2.0;

                // Draw left eye
                std::vector <std::vector<Point>> contours;
                contours.push_back(left_eye_points);
                std::vector <std::vector<Point>> hull(1);
                convexHull(contours[0], hull[0]);
                drawContours(mat, hull, -1, Scalar(0, 255, 0));

                // Draw right eye
                contours[0] = right_eye_points;
                convexHull(contours[0], hull[0]);
                drawContours(mat, hull, -1, Scalar(0, 255, 0));

                if (ear < EYE_AR_THRESH) {
                    counter++;
                } else {
                    if (counter >= EYE_AR_CONSEC_FRAMES) {
                        total++;
                        /*  std::string sJson = "{\"blink\": \"blink\"}";

                          try
                          {
                              onResult event(getSharedFromThis(), onResult::getName(), sJson);
                              signalonResult(event);
                          }
                          catch (std::bad_weak_ptr &e)
                          {
                          }*/
                    }

                    counter = 0;
                }

                cv::putText(mat, (boost::format{"Blinks: %d"} % total).str(), cv::Point(10, 30),
                            cv::FONT_HERSHEY_SIMPLEX,
                            0.7, Scalar(0, 0, 255), 2);
                cv::putText(mat, (boost::format{"EAR: %.2f"} % ear).str(), cv::Point(300, 30),
                            cv::FONT_HERSHEY_SIMPLEX,
                            0.7, Scalar(0, 0, 255), 2);
            }
        }


    } /* blinkdetector */

1 个答案:

答案 0 :(得分:0)

我能够解决自己的问题。我发现,与其将图像调整为任意分辨率,不如将其调整为实际图像分辨率的一半宽度和一半高度。将图像调整为较小尺寸可使Dlib人脸检测快速进行。因此,这是我为解决此问题所做的事情:

    Mat tmpMat = mat.clone();
    resize(tmpMat, tmpMat, Size(tmpMat.size().width / 2, tmpMat.size().height / 2));

我不得不将Kurento发送的图像克隆到我的方法中,因为由于某些奇怪的原因,原始Mat在用cv_image转换为Dlib图像时不显示轮廓。