一次性检测出一个文件夹下的所有人脸

一次性检测出一个文件夹下的所有人脸——人脸检测。

#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <opencv2/objdetect/objdetect.hpp>
#include "opencv2/imgproc/imgproc.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/opencv.hpp"  
#include <opencv2/core/core.hpp>
#include<iostream>   
#include<fstream>
#include <opencv.hpp>
//#include<iomanip.h>
#include "facedetect-dll.h"
#pragma comment(lib,"libfacedetect.lib")

using namespace cv;
using namespace std;

int main()
{
	int sum = 0;
	//boundingbox_test.txt
	ofstream f("E:\\boundingbox2.txt");//打开文件用于写,若文件不存在就创建它
	if (!f)//打开文件失败则结束运行
		return -1;
	for (int i = 0; i < 5; i++){//训练集中共有281个检测不到任何脸;测试集中共有189个
		string image_name = "E:\\ESR test\\test\\";//加载训练数据
		image_name = image_name + to_string(i + 1) + ".jpg";//to_string将整型转换为字符串型
		Mat srcImage = imread(image_name, 0);
		if (srcImage.empty())
		{
			fprintf(stderr, "Can not load the image file.\n");
			return -1;
		}

		int * pResults = NULL;
		pResults = facedetect_multiview((unsigned char*)(srcImage.ptr(0)), srcImage.cols, srcImage.rows, srcImage.step,1.2f, 5, 24);
		printf("%d faces detected.\n", (pResults ? *pResults : 0));

		if (*pResults!= 0){
			for (int i = 0; i < (pResults ? *pResults : 0); i++)		//print the detection results
			{
				short * p = ((short*)(pResults + 1)) + 6 * i;
				int x = p[0];
				int y = p[1];
				int w = p[2];
				int h = p[3];
				int neighbors = p[4];
				int angle = p[5];
				f<< x <<" "<< y <<" "<< w <<" "<< h << endl;//使用插入运算符写文件内容
				//f<< setw(20) << "家庭地址:"<< endl;
			
				//printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x, y, w, h, neighbors, angle);
			}
		}
		else{
			f << "0   0    0    0" << endl;//使用插入运算符写文件内容
			//cout << i + 1 << endl;
			sum++;
			}
	}
	//cout << "sum=" << sum << endl;
	f.close();//关闭文件
	waitKey(0);
	return 0;
}


	
	
	


发布了185 篇原创文章 · 获赞 873 · 访问量 127万+

猜你喜欢

转载自blog.csdn.net/duan19920101/article/details/51781398