我在 Ubuntu 20.04 上使用 OpenCV 4.4.0,并安装了最新的 opencv_contrib 额外模块。为了检测面部标志(基于 this 教程),我使用以下与额外 face 模块相关的 #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 终端命令安装额外的模块。结果是一样的。我假设存在与 namespace 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;
}
正如@john和@idclev 463035818所建议的,创建Facemark实例的方法是
Ptr<FacemarkLBF> facemark = FacemarkLBF::create();
例如如果我想称其为facemark。