人脸识别性能测试部分代码

描述:

检测出来的人脸按人数分类加矩形实线框、保存在新目录下。

#include <time.h>

#include<string.h>
#include<sstream>
#include <iostream>
#include <iomanip>  
#include <fstream>  
#include<direct.h>    //头文件  
#include<opencv2/opencv.hpp>  
#include<opencv2/highgui/highgui.hpp>  
#include<opencv2/imgproc/imgproc.hpp>  
#include "FaceDetection.h"
/*整型转为字符串型*/
string num2str(int i);
/*双浮点点型转为字符串型*/
string num2str(double i);
/*检测单张人脸图像*/
void getface(string filename, string savepath);
/*调用检测人脸程序*/
void get_face_detected();
/*总的人脸数*/
int face_number = 0;
vector<ObjectStruct>vframe;
/*输出结果文件*/
ofstream out("F:\\save_detected_face\\result.txt");
/*绘制矩形*/
void find_all_point(Point start, Point end, vector<Point> &save);
void full_rotated_rect(Mat &image, const RotatedRect &rect, const Scalar &color);


int  main()
{




// Mat img_masked;
// image.copyTo(img_masked, mask);



get_face_detected();
getchar();
return 0;
}
/*创建文件夹*/
/*调用检测人脸程序*/
void get_face_detected(){
/*frameImg 读入图像变量 frameImg1读入图像副本变量*/
std::vector<cv::String> filenames; // notice here that we are using the Opencv's embedded "String" class
// cv::String folder = "F:\\qq_friend_down_pic\\test\\*.jpg"; // again we are using the Opencv's embedded "String" class
cv::String folder = "F:\\qq_friend_down_pic\\test\\*.jpg";
cv::glob(folder, filenames); // new function that does the job ;-);
for (size_t i = 0; i < filenames.size(); ++i){
cout << "the face number is" << i << " ";
string savepath = "F:\\qq_friend_down_pic\\test1\\test2\\";
int start = filenames[i].find(".jpg");//找到符号“+”,作为字符数组的开始位置
savepath = filenames[i].substr(start - 13, start-4);
getface(filenames[i], savepath);
}


out << "total =  " << face_number << "\n";
cout << "结束!!!!" << "\n";
out.close();
}
string num2str(int i){
stringstream ss;
ss << i;
return ss.str();
}
string num2str(double i) {
stringstream ss;
ss << i;
return ss.str();
}
void getface(string filename, string savepath){
cv::Mat frameImg1, frameImg;
frameImg = imread(filename);
frameImg.copyTo(frameImg1);
int weight = frameImg.cols;
int height = frameImg.rows;


int mind = 0;


if (weight < height) mind = weight;
else mind = height;
double scale1 = 540 / double(mind);
cout << scale1 << endl;
resize(frameImg1, frameImg1, Size(), scale1, scale1);
cout << frameImg1.rows << " " << frameImg1.cols << endl;
double scale = 0.5;
mtcnn facedetect(frameImg1.rows*scale, frameImg1.cols * scale);
if (!frameImg1.empty()){
clock_t start;
start = clock();
facedetect.findFace(frameImg1, scale, vframe);
clock_t play_time = clock() - start;
/*保存到TXT中的内容*/
out << "path=  " << savepath << "\n";
out << "time=  " << play_time / 10e3 << "\n";
out << "face=  " << vframe.size() << "\n";
string face = to_string(vframe.size());
/*保存图像地址*/
string file_path = "F:\\save_detected_face\\classified\\" +face+"\\";
_mkdir(file_path.c_str());
savepath = file_path + savepath;
cout << "savepath =  " << savepath << "\n";



/*计算总人脸数*/
face_number = vframe.size() + face_number;
for (int i = 0; i<vframe.size(); i++){
if (vframe[i].pos[4]>0.9){


for (int j = 0; j < 5; j++){
circle(frameImg1, Point(vframe[i].fps[j * 2 + 1], vframe[i].fps[j * 2 + 0]), 3, Scalar(0, 255, 255), -1);
rectangle(frameImg1, Point(vframe[i].pos[0], vframe[i].pos[1]), Point(vframe[i].pos[0] + vframe[i].pos[2], vframe[i].pos[1] + vframe[i].pos[3]), Scalar(0, 0, 255), 2, 8, 0);
RotatedRect rect = RotatedRect(Point2f(vframe[i].pos[0] + vframe[i].pos[2], vframe[i].pos[1] + vframe[i].pos[3]), Size2f(vframe[i].pos[2], vframe[i].pos[3]), 0);
full_rotated_rect(frameImg1, rect, Scalar(0, 0, 255));
// Rect rect(vframe[i].fps[j * 2 + 1], vframe[i].fps[j * 2 + 0]), ceil_width, ceil_height);
}
cv::putText(frameImg1, num2str(vframe[i].pos[4]), Point(vframe[i].pos[0], vframe[i].pos[1]), CV_FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 100, 0), 2, 2);
}
}

imwrite(savepath, frameImg1);


}
// 按下键盘上q键退出  
//if (char(waitKey(20)) == 'q'){ return; }
frameImg1.release();
frameImg.release();
}
void full_rotated_rect(Mat &image, const RotatedRect &rect, const Scalar &color)
 {
   CvPoint2D32f point[4];
   Point pt[4];
   vector<Point> center1, center2;

   /*画出外框*/
cvBoxPoints(rect, point);
   for (int i = 0; i < 4; i++)
{
       pt[i].x = (int)point[i].x;
       pt[i].y = (int)point[i].y;
}
    line(image, pt[0], pt[1], color, 1);
    line(image, pt[1], pt[2], color, 1);
    line(image, pt[2], pt[3], color, 1);
    line(image, pt[3], pt[0], color, 1);

    /*填充内部*/
find_all_point(pt[0], pt[1], center1);  /*找出两点间直线上的所有点*/
    find_all_point(pt[3], pt[2], center2);
    vector<Point>::iterator itor1 = center1.begin(), itor2 = center2.begin();
    while (itor1 != center1.end() && itor2 != center2.end())
{
        line(image, *itor1, *itor2, color, 1);  /*连接对应点*/
        itor1++;
        itor2++;
}

vector<Point>().swap(center1);
    vector<Point>().swap(center2);
//imwrite("F:\\qq_friend_down_pic\\test1\\1175294077_10.jpg", image);
}
void find_all_point(Point start, Point end, vector<Point> &save)
 {
   if (abs(start.x - end.x) <= 1 && abs(start.y - end.y) <= 1)
{
    save.push_back(start);
    return; /*点重复时返回*/
}


Point point_center;
   point_center.x = (start.x + end.x) / 2;
   point_center.y = (start.y + end.y) / 2;
   save.push_back(point_center);   /*储存中点*/
   find_all_point(start, point_center, save);  /*递归*/
   find_all_point(point_center, end, save);
}

猜你喜欢

转载自blog.csdn.net/qq_34568522/article/details/80564029