首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mac中编译/运行cpp文件

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

Stack Overflow用户
提问于 2020-03-23 22:38:53
回答 1查看 581关注 0票数 1

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

代码语言:javascript
复制
#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命令,但是我得到了:

代码语言:javascript
复制
webcam_face_pose_ex.cpp:30:10: fatal error: 'dlib/opencv.h' file not found

#include <dlib/opencv.h>
         ^~~~~~~~~~~~~~~
1 error generated.

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-23 23:05:19

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

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

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

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

代码语言:javascript
复制
g++ -I. examples/webcam_face_pose_ex.cpp

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

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

解决方案

dlib项目文档声明应该使用cmake构建示例。确保使用cmake编译示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60822507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档