如何在mac中编译/运行cpp文件



我从GitHub下载了一个webcam_face_pose_ex.cpp文件,现在我想在我的mac上编译并运行它。

#include <dlib/opencv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <X11/Xlib.h>
using namespace dlib;
using namespace std;
int main()
{
try
{
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
return 1;
}
image_window win;
// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
// Grab and process frames until the main window is closed by the user.
while(!win.is_closed())
{
// Grab a frame
cv::Mat temp;
if (!cap.read(temp))
{
break;
}
// Turn OpenCV's Mat into something dlib can deal with.  Note that this just
// wraps the Mat object, it doesn't copy anything.  So cimg is only valid as
// long as temp is valid.  Also don't do anything to temp that would cause it
// to reallocate the memory which stores the image as that will make cimg
// contain dangling pointers.  This basically means you shouldn't modify temp
// while using cimg.
cv_image<bgr_pixel> cimg(temp);
// Detect faces 
std::vector<rectangle> faces = detector(cimg);
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
// Display it all on the screen
win.clear_overlay();
win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
}
}
catch(serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch(exception& e)
{
cout << e.what() << endl;
}
}

我尝试了g++webcam_face_pose_ex.cpp命令,但我得到了:

webcam_face_pose_ex.cpp:30:10: fatal error: 'dlib/opencv.h' file not found
#include <dlib/opencv.h>
^~~~~~~~~~~~~~~
1 error generated.

想知道我能做些什么来解决这个问题吗?

示例文件不打算使用g编译++

阅读以下内容以了解有关-I标志和#include语句的信息:

webcam_face_pose_ex.cpp是一个更大项目的一部分,您将无法单独编译它,因为它依赖于其他文件。#include指令指定,为了编译此程序,必须首先编译#include指定的文件中的代码。这意味着在编译webcam_face_pose_ex.cpp之前必须下载整个dlib。这个项目还需要opencv2,所以我们可以下载它,并将opencv2文件夹放在dlib项目文件夹中。

现在我们可以打开终端,将目录更改为dlib项目文件夹,并使用以下命令编译文件:

g++ -I. examples/webcam_face_pose_ex.cpp

请注意,我们使用-I参数作为-I.来指定查找#include指定的文件的目录,这意味着要在当前工作目录中搜索文件。在那里,它将找到dlib文件夹dlib/opencv.h

无论如何,这还不够。当您执行该命令时,您将遇到一个错误opencv2/opencv_modules.hpp: No such file or directory

解决方案

dlib项目文档指出,应该使用cmake构建示例。确保使用cmake编译示例。

最新更新