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

问题描述 投票:1回答:1

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

想知道我该怎么做才能解决此问题?

c++
1个回答
0
投票

file you downloaded是一个较大项目的一部分,您将无法单独编译它,因为它依赖于其他文件。 #include指令指定为了编译此程序,必须首先编译#include指定的文件中的代码。在编译dlib之前,请确保已下载整个webcam_face_pose_ex.cpp,然后将目录更改为dlib文件夹并使用以下命令编译该文件:

g++ -I. examples/webcam_face_pose_ex.cpp

注意,我们使用-I参数将[include]指定为#include指定的文件所在目录,这意味着在当前工作目录中搜索文件。它将在其中找到dlib文件夹。

© www.soinside.com 2019 - 2024. All rights reserved.