【问题标题】:How to compile/run a cpp file in mac如何在mac中编译/运行cpp文件
【发布时间】:2020-07-04 10:39:54
【问题描述】:

我从 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.

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

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow!请首先发布您尝试编译的代码的minimal reproducible example,以便我们可以帮助解决与您相同的问题并告诉您如何解决它。
  • 您需要一个-I&lt;path to OpenCV include folder&gt; 并使用它,您还需要-L&lt;Path to OpenCV library&gt;-l&lt;Danged if I can remember the name of the Open CV library at the moment&gt;
  • 嘿@Alex 这将不得不进入你的问题。当您粘贴它时,突出显示它并单击 { } 代码按钮以使其格式化。
  • @CausingUnderflowsEverywhere 谢谢我用我认为导致错误的代码编辑了帖子。
  • 嘿@Alex 你为什么要编译那个特定的文件?

标签: c++


【解决方案1】:

示例文件不打算使用 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 project documentation 声明示例应使用cmake 构建。确保使用 cmake 编译示例。

【讨论】:

  • 你好,这样做给了我这个结果:dlib-master/examples/webcam_face_pose_ex.cpp:30:10: fatal error: 'dlib/opencv.h' file not found #include &lt;dlib/opencv.h&gt; ^~~~~~~~~~~~~~~ 1 error generated.
  • 我这样做了,但没有任何区别。主文件夹称为 dlib-master
  • 第二次,我们需要使用 -I 参数来指定包含文件的位置。
猜你喜欢
  • 2018-12-04
  • 2022-01-21
  • 2014-07-13
  • 2015-07-11
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
相关资源
最近更新 更多