4 Replies Latest reply on Jun 3, 2018 11:19 AM by justin_j

    Realsense D415 OpenCV face detection error

    justin_j

      Hi

      I am new to Realsense. I am trying to use Realsense with openCV by C++.

      I already tried it. However, Something does not work pretty well.

      By openCV I made Realsense streaming with gray scale. However, when the

      Debug reach to CascadeClassifier, which is just like

       

      face_cascade(frame_gray, faces,1.1,5,0|CASCADE_SCALE_IMAGE,Size(50,50));

       

      the memory violation came out and stop debugging.

      At first time, I assume that something is wrong with pointer.

      Therefore, I tried changing gray scale channel, but did not gone well.

       

      error shows like this :

      예외 발생(0x00007FFD00A14008, camera_test001.exe): Microsoft C++ 예외: std::exception, 메모리 위치 0x000000642ACFCF90. 0x784 스레드가 종료되었습니다(코드: 0 (0x0))

      (exception occur(0x00007FFD00A14008, camera_test001.exe): Microsoft C++ Exception: std::exception, memory location 0x000000642ACFCF90. 0x784 thread has been closed(code:0 (0x0))

      스크린샷 2018-05-31 오후 11.39.36.png

       

      and my complete code is this:

       

      // License: Apache 2.0.See LICENSE file in root directory.
      // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
      
      #include <rs.hpp> // Include RealSense Cross Platform API
      #include <opencv2/opencv.hpp>   // Include OpenCV API
      #include "opencv2/objdetect.hpp"
      #include "opencv2/videoio.hpp"
      #include "opencv2/highgui.hpp"
      #include "opencv2/imgproc.hpp"
      
      using namespace std;
      using namespace cv;
      
      //String face_cascade_name;
      CascadeClassifier face_cascade;
      
      void detectAndDisplay(Mat frame) {
        std::vector<cv::Rect> faces;
        Mat frame_gray;// (Size(640, 480));
        string window_name = "Test";
        //frame.convertTo(frame, CV_8UC3);
        cvtColor(frame, frame, COLOR_BGR2RGB);
        cvtColor(frame, frame_gray, COLOR_RGB2GRAY);
      
        equalizeHist(frame_gray, frame_gray);
        //frame_gray.convertTo(frame_gray, CV_8UC3);
        if (frame.empty() || frame_gray.empty()) {
        printf("error, no data\n");
        }
        else {
        printf("nothing\n");
        }
      
        face_cascade.detectMultiScale(frame_gray, faces , 1.1, 5, 0 | CASCADE_SCALE_IMAGE, Size(50, 50));
      
        imshow(window_name, frame_gray);
      }
      
      int main(int argc, char * argv[]) try
      {
        rs2::colorizer color_map;
        rs2::pipeline pipe;
      
        face_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml");
      
        pipe.start();
      
        const auto window_name = "Display Image";
        namedWindow(window_name, WINDOW_AUTOSIZE);
      
        while (waitKey(1) < 0 && cvGetWindowHandle(window_name))
        {
        rs2::frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
        rs2::frame color = data.get_color_frame();
      
        const int color_w = color.as<rs2::video_frame>().get_width(); //640 
        const int color_h = color.as<rs2::video_frame>().get_height(); //480
      
        Mat image(Size(color_w, color_h), CV_8UC3, (void*)color.get_data(), Mat::AUTO_STEP);
      
        // Update the window with new data
        //imshow(window_name, image);
      
        detectAndDisplay(image);
        }
      
        return EXIT_SUCCESS;
      }
      catch (const rs2::error & e)
      {
        std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
        return EXIT_FAILURE;
      }
      catch (const std::exception& e)
      {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
      }