0

我在 Ubuntu 20.04 上使用 OpenCV 4.4.0,并安装了最新的opencv_contrib额外模块。为了检测面部标志(基于教程),我使用以下与额外面部模块相关的#include 和命名空间部分:

#include <opencv2/face.hpp>
using namespace cv::face;

检测到 face.hpp 文件(因此我假设正确安装了opencv_contrib模块),但例如该行

    Ptr<facemark> facemark = FacemarkLBF::create();

抛出错误

error: ‘facemark’ was not declared in this scope

我已经尝试使用 cmake-gui 和 cmake 终端命令安装额外的模块。结果是一样的。我假设存在与命名空间 cv::face相关的错误。关于我在这里犯了什么样的错误有什么想法吗?

最小的代码在这里:

#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
#include "drawLandmarks.hpp"


using namespace std;
using namespace cv;
using namespace cv::face;


int main(int argc,char** argv)
{
    // Load Face Detector
    CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");

    // Create an instance of Facemark
    Ptr<facemark> facemark = FacemarkLBF::create();

    // Load landmark detector
    facemark->loadModel("lbfmodel.yaml");

    // Set up webcam for video capture
    VideoCapture cam(0);
    
    // Variable to store a video frame and its grayscale 
    Mat frame, gray;
    
    // Read a frame
    while(cam.read(frame))
    {
      
      // Find face
      vector<rect> faces;
      // Convert frame to grayscale because
      // faceDetector requires grayscale image.
      cvtColor(frame, gray, COLOR_BGR2GRAY);

      // Detect faces
      faceDetector.detectMultiScale(gray, faces);
      
      // Variable for landmarks. 
      // Landmarks for one face is a vector of points
      // There can be more than one face in the image. Hence, we 
      // use a vector of vector of points. 
      vector< vector<point2f> > landmarks;
      
      // Run landmark detector
      bool success = facemark->fit(frame,faces,landmarks);
      
      if(success)
      {
        // If successful, render the landmarks on the face
        for(int i = 0; i < landmarks.size(); i++)
        {
          drawLandmarks(frame, landmarks[i]);
        }
      }

      // Display results 
      imshow("Facial Landmark Detection", frame);
      // Exit loop if ESC is pressed
      if (waitKey(1) == 27) break;
      
    }
    return 0;
}
4

1 回答 1

0

正如@john 和@idclev 463035818 所建议的那样,创建 Facemark 实例的方法是

Ptr<FacemarkLBF> facemark = FacemarkLBF::create();

例如,如果我想称它为facemark。

于 2020-07-06T08:54:53.797 回答