C++Opencv人脸识别案例

#include 
#include


using namespace std;
using namespace cv;


void face_detector_demo() {

	std::string root_dir = "D:/opencv4.6.0/opencv/sources/samples/dnn/face_detector/";
	dnn::Net net = dnn::readNetFromTensorflow(root_dir + "opencv_face_detector_uint8.pb", root_dir + "opencv_face_detector.pbtxt");
	//VideoCapture capture("test.mp4");
	VideoCapture capture(0);
	Mat frame;
	while (true) {
		capture.read(frame);
		flip(frame, frame, 1);  //镜像翻转
		if (frame.empty()) {
			break;
		}
		Mat blob = dnn::blobFromImage(frame, 1.0, Size(300, 300), Scalar(104, 177, 123), false, false);
		net.setInput(blob);				//NCHW
		Mat probs = net.forward();		// 
		Mat res(Size(probs.size[2], probs.size[3]), CV_32F, probs.ptr());
		// 解析结果
		for (int i = 0; i < res.rows; i++) {
			float confidence = res.at(i, 2);
			if (confidence > 0.5) {
				int x1 = static_cast(res.at(i, 3) * frame.cols);
				int y1 = static_cast(res.at(i, 4) * frame.rows);
				int x2 = static_cast(res.at(i, 5) * frame.cols);
				int y2 = static_cast(res.at(i, 6) * frame.rows);
				Rect box(x1, y1, x2 - x1, y2 - y1);
				rectangle(frame, box, Scalar(0, 255, 0), 2, 8, 0);
			}
		}
		Mat dst;
		imshow("人脸检测演示:", frame);
		int c = waitKey(10);
		if (c == 27) {
			break;
		}
	}
}


int main()
{
	face_detector_demo();
	system("pause");
}

你可能感兴趣的:(Opencv(C++),opencv,c++,计算机视觉)