GStream cannot query video position

I am using a modified version of the dlib example of webcam_face_pose_ex.cpp and am receiving the above error. When using the original version of the program it works fine, but I need to do more than just track a face. I have not changes anything that had to do with accessing the camera.
Below is my code:

#include <dlib/opencv.h>
#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 <dlib/threads.h>
#include <dlib/ref.h>
#include <vector>
#include <fstream>
#include <cmath>

using namespace dlib;
using namespace std;

/// <summery>
/// This class defines the window that will be used to display resutls.
/// </summery>
class my_window : public drawable_window {...}

std::vector<std::vector<point>> stored_faces;
my_window data_win;

/// <summery>
/// This function copies the values from temp and stores them in stored_faces.
/// </summery>
void copy_vect(std::vector<full_object_detection>& vect_to_copy){
    std::vector<point> temp2;
    for(size_t i = 0; i < vect_to_copy.num_parts(); ++i){
        temp2.push_back(vect_to_copy.part(i));
    }
   stored_faces.push_back(temp2);
   data_win.update_text(temp2);
}

int main()
{
    try
    {
        cv::VideoCapture cap(0);
        thread_pool& global_pool = default_threaded_pool();
        if (!cap.isOpened())
        {
            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;
        size_t frame_count = 0;
        unsigned short btn_presses = 0;
        // 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]));
            if(data_win.btn_pressed)
            {
                 copy_vect(shapes);
                 ++btn_presses;
            }
            if(btn_presses == 1 && !data_win.btn_pressed){
                ++btn_presses;
                win.close_window();
                // final calculations
                break;
            }
            frame_count++;
            // Display it all on the screen
            win.clear_overlay();
            win.set_image(cimg);
            win.add_overlay(render_face_detections(shapes));
      }
  }
  catch(serialization_error& e)
  {
      return -5;
  }
  catch(exception& e)
  {
      return -6;
  }
}
  • a stream from a webcam does not really have a “position”
  • on linux, it should use v4l for webcams, not gstreamer
  • there is no code to query the position in your script

please try to set the OPENCV_VIDEOIO_DEBUG env var to 1, and rerun your script, this should result in logging output, show us !

also try to specify the backend explicitly, e.g. VideoCapture cap(0, CAP_V4L);

1 Like

I tried adding the CAP_V4L to cap but am receiving this error:

/home/cervpro/dlib-19.20/examples/tests/traking/webcam_face_pose_ex.cpp:123:29: error: ‘CAP_V4L’ was not declared in this scope
     cv::VideoCapture cap(0, CAP_V4L);
                             ^~~~~~~
/home/cervpro/dlib-19.20/examples/tests/traking/webcam_face_pose_ex.cpp:123:29: note: suggested alternative:
In file included from /usr/include/opencv4/opencv2/highgui.hpp:51:0,
                 from /usr/include/opencv4/opencv2/highgui/highgui.hpp:48,
                 from /home/cervpro/dlib-19.20/examples/tests/traking/webcam_face_pose_ex.cpp:5:
/usr/include/opencv4/opencv2/videoio.hpp:92:8: note:   ‘CAP_V4L’
        CAP_V4L          = 200,          //!< V4L/V4L2 capturing support
        ^~~~~~~

I added #include <opencv2/videoio.hpp> but am still getting this error.

it’s probably only missing the namespace: cv::CAP_V4L