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

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

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


	
	
	


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了实现人脸检测与识别,我们可以使用Python中的OpenCV库和face_recognition库。下面是一个简单的代码示例,用于对一张图片进行人脸检测和识别: ```python import cv2 import face_recognition # 加载图片 image = cv2.imread("test.jpg") # 找到图片中的人脸位置 face_locations = face_recognition.face_locations(image) # 遍历每个人脸位置,进行识别 for face_location in face_locations: # 获取人脸位置信息 top, right, bottom, left = face_location # 在图片中标识人脸位置 cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2) # 对人脸进行识别 unknown_face_encoding = face_recognition.face_encodings(image, [face_location])[0] # 这里需要将已知人脸的编码信息保存在一个列表中,然后依次进行比对 # 如果已知人脸列表中有多个人脸,可以使用如下代码: # known_face_encodings = [encoding1, encoding2, encoding3, ...] # results = face_recognition.compare_faces(known_face_encodings, unknown_face_encoding) # 然后遍历results列表,找到匹配的人脸 # 在人脸位置上方添加文字标识 cv2.putText(image, "Unknown", (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 展示结果 cv2.imshow("Image", image) cv2.waitKey(0) ``` 在实际使用中,我们需要将已知人脸的编码信息保存在一个列表中,然后依次进行比对。如果已知人脸列表中有多个人脸,可以使用`face_recognition.compare_faces()`函数进行比对,然后遍历结果列表,找到匹配的人脸

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值