C ++ Realsense openCV facedetection - доступ - PullRequest
0 голосов
/ 27 мая 2018

Я новичок в Realsense & C ++.Я знаю, что этот вопрос может быть легко.Тем не менее, я не могу решить эту проблему, даже если я искал полдня.

Я пытаюсь использовать Realsense с OpenCV для распознавания лиц (Haarcascade).Но когда я использую 'face_cascade, detectMultiScale', проект получает ошибку нарушения доступа.(как на картинке внизу)

enter image description here

, и мои коды таковы:

// 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;
string window_name = "Face detection";

void detectAndDisplay(Mat frame)
{
	std::vector<Rect> faces;
	Mat frame_gray;
	
	cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
	equalizeHist(frame_gray, frame_gray);
	if (frame.empty()) {
		printf("error, no data");
	}
	else {
		printf("no problem");
	}
	//face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
	face_cascade.detectMultiScale(frame_gray, faces , 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(500, 500));

	for (size_t i = 0; i < faces.size(); i++)
	{
		Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
		ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2),
			0, 0, 360, Scalar(0, 0, 255), 4, 8, 0);
	}
	
	imshow(window_name, frame);
}


int main(int argc, char * argv[]) try
{
	// Declare depth colorizer for pretty visualization of depth data
	rs2::colorizer color_map;

	// Declare RealSense pipeline, encapsulating the actual device and sensors
	rs2::pipeline pipe;
	
	face_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
	//if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); };
	

	// Start streaming with default recommended configuration
	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 depth = color_map(data.get_depth_frame());
		rs2::frame color = data.get_color_frame();

		// Query frame size (width and height)
		//const int w = depth.as<rs2::video_frame>().get_width();
		//const int h = depth.as<rs2::video_frame>().get_height();

		const int color_w = color.as<rs2::video_frame>().get_width();
		const int color_h = color.as<rs2::video_frame>().get_height();

		// Create OpenCV matrix of size (w,h) from the colorized depth data
		//Mat image(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);
		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;
}

введите код

...