1

I downloaded a webcam_face_pose_ex.cpp file from GitHub and now i want to compile and run it on my 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;
    }
}

I tried g++ webcam_face_pose_ex.cpp command but I get:

webcam_face_pose_ex.cpp:30:10: fatal error: 'dlib/opencv.h' file not found

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

Was Wondering what I could do to fix this?

Alex
  • 17
  • 7
  • Hey, welcome to Stack Overflow! Please begin by posting a [mcve] of the code you're trying to compile so that we can help get to the same problem as you and tell you how to fix it. – CausingUnderflowsEverywhere Mar 23 '20 at 22:41
  • You need a `-I` and to go with it you'll also need `-L` and `-l` – user4581301 Mar 23 '20 at 22:42
  • Hey @Alex that will have to go into your question. When you paste it in, highlight it and click the { } code button so that it formats. – CausingUnderflowsEverywhere Mar 23 '20 at 22:45
  • @CausingUnderflowsEverywhere thank you I edited the post with the code I think is causing the error. – Alex Mar 23 '20 at 22:46
  • Hey @Alex for what reason are you trying to compile that specific file? – CausingUnderflowsEverywhere Mar 23 '20 at 22:57
  • @Alex You should answer the question regarding why you want to compile just that specific file. I don't even have OpenCV installed on my machine and the makefile the library provides worked just fine (which compiles this particular example program as well). – nick Mar 23 '20 at 23:23
  • @nick from what i saw it was the only example in the library that used a webcam. Also I am not sure what a makefile is or how to use it. Can you please explain? Thank you – Alex Mar 23 '20 at 23:28
  • @Alex there is a file called [README.md](https://github.com/davisking/dlib/blob/master/README.md), this file explains how to use the library, including how to build it. They use `cmake` which is a program you need to install on your computer, it will build the library for you if you follow the instructions given in the `README.md` document. They also have a `how to compile` page on their website http://dlib.net/compile.html – nick Mar 23 '20 at 23:32
  • @nick would you be able to post an answer with an example of running cmake for the project? I'm sure that would help a lot of people. – CausingUnderflowsEverywhere Mar 23 '20 at 23:36
  • @CausingUnderflowsEverywhere I don't think that has anything to do with this question. There are questions here on SO regarding [cmake](https://stackoverflow.com/q/17525153/9176689) [use](https://stackoverflow.com/search?q=%5Bcmake%5D+how+to+compile) and the commands you use with cmake never really change. – nick Mar 23 '20 at 23:42
  • @nick I did the steps you listed and it worked on almost all of the examples. However it did not work on the one example that I wanted done. It gave me this message: ```OpenCV not found, so we won't build the webcam_face_pose_ex example.``` I looked in the dlib-master folder and OpenCV was there. – Alex Mar 23 '20 at 23:53
  • @Alex that means you need to install opencv, they, too, have a `README.md` document in their github that tells you how to install it. https://github.com/opencv/opencv/blob/master/README.md – nick Mar 23 '20 at 23:58
  • @nick I have OpenCV installed. I just reinstalled it to make sure. However it continues to give me that message. – Alex Mar 24 '20 at 00:21
  • @Alex then that's a new problem and I suggest you create a new question. I would suggest that you do some research beforehand though, because [opencv not found](https://stackoverflow.com/questions/32146862/opencv-not-found-when-using-cmake-on-debian-wheezy) is a [common problem](https://stackoverflow.com/search?q=opencv+not+found) which means your question might get closed and deleted quickly if you don't explain how it's different from the other 4946 questions – nick Mar 24 '20 at 00:24

1 Answers1

1

The Example File Is Not Meant to be Compiled Using g++

Read the following to learn a bit about the -I flag and #include statements:

The webcam_face_pose_ex.cpp is part of a larger project and you won't be able to compile it on its own because it depends on other files. The #include directive specifies that in order to compile this program, code from the file specified by #includemust be compiled first. This means the entire dlib must be downloaded before compiling webcam_face_pose_ex.cpp. This project also requires opencv2 so we can download it and place the opencv2 folder in the dlib project folder.

Now we can open terminal and change directory into the dlib project folder and compile the file using the following command:

g++ -I. examples/webcam_face_pose_ex.cpp

Note we're specifying the directory of where to find the files specified by #include using the -I parameter as -I. this means to search the current working directory for the files. There it will find the dlib folder and dlib/opencv.h.

How ever, this isn't enough. When you execute the command, you'll encounter an error opencv2/opencv_modules.hpp: No such file or directory.

Solution

The dlib project documentation states that the examples should be built using cmake. Make sure to use cmake to compile the examples.

  • Hello, doing that gave me this result: ```dlib-master/examples/webcam_face_pose_ex.cpp:30:10: fatal error: 'dlib/opencv.h' file not found #include ^~~~~~~~~~~~~~~ 1 error generated.``` – Alex Mar 23 '20 at 23:14
  • I did that but it made no difference. The main folder is called dlib-master – Alex Mar 23 '20 at 23:21
  • Once second, we need to use the -I parameter to specify where the included files are to be located. – CausingUnderflowsEverywhere Mar 23 '20 at 23:23