davisking/dlib

[Bug]: having issues converting opencv mat to iplimage

hermanumrao opened this issue · 4 comments

What Operating System(s) are you seeing this problem on?

Linux (x86-64)

dlib version

19.24

Python version

No response

Compiler

g++ gnu 11

Expected Behavior

I wish to use opencv to read frames using a webcam
and then proess them using DLIB

Current Behavior

I am trying to run the below code:

#include <dlib/gui_widgets.h>
#include <dlib/image_processing.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>

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 frame;
      if (!cap.read(frame)) {
        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 frame is valid.  Also don't do anything to frame 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 frame while using cimg.

      // dlib::array2d<bgr_pixel> cimg;
      // dlib::assign_image(cimg, dlib::cv_image<bgr_pixel>(frame));
      cv_image<bgr_pixel> cimg(frame);

      // 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;
  }
}

but I am getting this error

root@raspberrypi:~/emotion-check# nvim Makefile 
root@raspberrypi:~/emotion-check# make
g++ -std=c++11 `pkg-config --cflags opencv4 dlib-1` -c main.cpp -o main.o
In file included from /usr/local/include/dlib/opencv.h:10,
                 from main.cpp:7:
/usr/local/include/dlib/opencv/cv_image.h: In instantiation of ‘dlib::cv_image<pixel_type>::cv_image(cv::Mat) [with pixel_type = dlib::bgr_pixel]’:
main.cpp:42:49:   required from here
/usr/local/include/dlib/opencv/cv_image.h:37:29: error: conversion from ‘const cv::Mat’ to non-scalar type ‘IplImage’ requested
   37 |             IplImage temp = img;
      |                             ^~~
make: *** [Makefile:106: main.o] Error 1

Steps to Reproduce

you can use the below makefile:

# Compiler and flags
CXX = g++
CXXFLAGS = -std=c++11  `pkg-config --cflags opencv4 dlib-1`
LDFLAGS = `pkg-config --libs opencv4 dlib-1`

# Target executable
TARGET = face_landmark_detection

# Source files
SRCS = try1.cpp

# Object files
OBJS = $(SRCS:.cpp=.o)

# Default target
all: $(TARGET)

# Rule to link the target executable
$(TARGET): $(OBJS)
	$(CXX) $(OBJS) -o $@ $(LDFLAGS)

# Rule to compile source files into object files
%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

# Rule to clean up generated files
clean:
	rm -f $(OBJS) $(TARGET)

.PHONY: all clean

Anything else?

It might be possible that my way of compiling or the code written is wrong
if so please help me out

What version of opencv are you using?

Your build errors also don't match your makefile. E.g. the build errors talk about main.cpp, but the makefile doesn't. So something doesn't add up. Maybe that's part of what's going on.

Warning: this issue has been inactive for 35 days and will be automatically closed on 2024-08-13 if there is no further activity.

If you are waiting for a response but haven't received one it's possible your question is somehow inappropriate. E.g. it is off topic, you didn't follow the issue submission instructions, or your question is easily answerable by reading the FAQ, dlib's official compilation instructions, dlib's API documentation, or a Google search.

Warning: this issue has been inactive for 42 days and will be automatically closed on 2024-08-13 if there is no further activity.

If you are waiting for a response but haven't received one it's possible your question is somehow inappropriate. E.g. it is off topic, you didn't follow the issue submission instructions, or your question is easily answerable by reading the FAQ, dlib's official compilation instructions, dlib's API documentation, or a Google search.

Notice: this issue has been closed because it has been inactive for 45 days. You may reopen this issue if it has been closed in error.