300W数据集测试MTCNN的landmark效果代码

300W数据集测试MTCNN的landmark效果代码

300W数据集测试MTCNN的landmark效果,用提取其中afw数据集337张图片的预测关键点并写入到txt中,再用测试程序和标注landmark做对比。

处理得到的预测landmark格式如下:
1051618982(图片名)
1(landmark个数)
543 267 643 268 594 322 542 359 643 360
111076519
2
1095 624 1161 635 1125 668 1084 696 1146 706
1172 764 1238 767 1211 806 1168 830 1233 833
1130084326

程序如下:

#include "network.h"
#include "mtcnn.h"
#include <time.h>
#include <fstream>
#include <opencv2/opencv.hpp>
#pragma comment(lib, "libopenblas.dll.a")

using namespace cv;

std::vector<std::string> split(std::string& str, std::string& pattern);
void str2int(int &int_temp, const string &string_temp);

int main()
{
//因为执行目录被设置到openblas/x64下了,保证dll能正常载入,这时候图片路径就相对要提上去2级
//Mat im = imread("…/…/test10.jpg");
int i = 0;
string img_dir = “E:/face_alignment/data/300W_test_5points/afw/”;
string name, s="_";
ifstream infile;
ofstream outfile;
infile.open(“E:/face_alignment/data/300W_test_5points/afw_mtcnn_test_V2_2.txt”);
outfile.open(“E:/face_alignment/data/300W_test_5points/MTCNN_V2_2_test/afw_test.txt”);

while (infile)
{
	infile &gt;&gt; name;
	vector&lt;string&gt; result = split(name, s);
	cout &lt;&lt; i &lt;&lt; endl;
	i++;
	//cout &lt;&lt; "name: " &lt;&lt; result[0] &lt;&lt; " s: " &lt;&lt; result[1] &lt;&lt; endl;
	
	string shot_name = result[0];
	int decide;
	str2int(decide, result[1]);

	if (decide == 1)
	{
		string image_name = name + ".jpg";
		outfile &lt;&lt; shot_name  &lt;&lt; endl;
		string image_name_dir = img_dir + image_name;
		cout &lt;&lt; image_name_dir &lt;&lt; endl;

		Mat im = imread(image_name_dir);
		vector&lt;vector&lt;Point2f&gt;&gt; key_points;
		mtcnn find(im.cols, im.rows);
		vector&lt;Rect&gt; objs = find.detectObject(im, key_points);
		//outfile &lt;&lt; objs.size() &lt;&lt; endl;
		for (int i = 0; i &lt; objs.size(); ++i)
		{
			rectangle(im, objs[i], Scalar(0, 255), 2);
			//outfile &lt;&lt; objs[i].x &lt;&lt; " " &lt;&lt; objs[i].y &lt;&lt; " " &lt;&lt; objs[i].width &lt;&lt; " " &lt;&lt; objs[i].height &lt;&lt; endl;
		}
			

		//cout &lt;&lt; "num of key_points: " &lt;&lt; key_points.size() &lt;&lt; endl;
		outfile &lt;&lt; key_points.size() &lt;&lt; endl;
		for (int i = 0; i &lt; key_points.size(); i++)
		{
			for (int j = 0; j &lt; key_points[i].size(); j++)
			{
				cv::circle(im, key_points[i][j], 1, cv::Scalar(255, 255, 0), 2);
				outfile &lt;&lt; key_points[i][j].x &lt;&lt; " " &lt;&lt; key_points[i][j].y &lt;&lt; " ";
			}
			outfile &lt;&lt; endl;
		}
		
		string outdir = "E:/face_alignment/data/300W_test_5points/MTCNN_V2_2_test/afw/";
		string out_image = outdir + shot_name + ".jpg";
		imwrite(out_image, im);
		//imshow("demo", im);
		//waitKey(0);
	}

	
}

infile.close();
outfile.close();



return 0;

}

//字符串分割函数
std::vector<std::string> split(std::string& str, std::string& pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
//str += pattern;//扩展字符串以方便操作
int size = str.size();

pos = str.find(pattern, 0);
if (pos&lt;size) {
	std::string s1 = str.substr(0, pos);
	std::string s2 = str.substr(pos + 1, size - 1);
	result.push_back(s1);
	result.push_back(s2);
}

return result;

}

void str2int(int &int_temp, const string &string_temp)
{
stringstream stream(string_temp);
stream >> int_temp;
}



用我们自己得到的预测txt,与作者提供的标注pts文件进行计算。  算5个landmark的欧式距离之和,除以左上角和右下角欧式距离,除以5。

#include <iostream>  
#include <stdlib.h>  
#include <fstream>  
#include <sstream>  
#include <string>  
#include <vector>  
#include <opencv2/opencv.hpp>    
using namespace cv;
using namespace std;

std::vector<std::string> split(std::string& str, std::string& pattern);
void str2int(int &int_temp, const string &string_temp);
float computer_error(vector<float> pts_gt, vector<vector<float>> pts_pre);

int main()
{
int count = 0, pos = 0;
float acc, thread=0.1;
string name_list, s = “_”;
string pts_dir = “E:/face_alignment/data/300W_test_5points/afw/”;
ifstream infile_list, infile_pre;
infile_list.open(“E:/face_alignment/data/300W_test_5points/afw_mtcnn_test_V1.txt”);
infile_pre.open(“E:/face_alignment/data/300W_test_5points/MTCNN_V1_test/afw_test.txt”);
infile_list >> name_list;

while (infile_pre)
{
	string name_pre;
	int num_pre;
	vector&lt;vector&lt;float&gt; &gt; pts_pre;
	infile_pre &gt;&gt; name_pre;
	infile_pre &gt;&gt; num_pre;
	pts_pre.resize(num_pre);

	for (int i = 0; i &lt; num_pre; i++)
	{
		pts_pre[i].resize(10);
	}

	for (int j = 0; j &lt; num_pre; j++)
	{
		infile_pre &gt;&gt; pts_pre[j][0] &gt;&gt; pts_pre[j][1] &gt;&gt; pts_pre[j][2] &gt;&gt; pts_pre[j][3] &gt;&gt; pts_pre[j][4] &gt;&gt; pts_pre[j][5] &gt;&gt; pts_pre[j][6] &gt;&gt; pts_pre[j][7] &gt;&gt; pts_pre[j][8] &gt;&gt; pts_pre[j][9];
		
	}


	//for (int i = 0; i &lt; num_pre; i++)
	//{
	//	for (int j = 0; j &lt; 10; j++)
	//	{
	//		cout &lt;&lt; pts_pre[i][j] &lt;&lt; " ";
	//	}
	//	cout &lt;&lt; endl;
	//}


	// read gt file
	while (infile_list)
	{
		
		vector&lt;string&gt; result = split(name_list, s);
		string name_gt = result[0];

		if (name_gt.compare(name_pre) == 0)
		{
			count++;
			cout &lt;&lt; count &lt;&lt; endl;

			vector&lt;float&gt; pts_gt;
			pts_gt.resize(10);
			string pts_dir_name = pts_dir + name_list + ".pts";
			ifstream infile_pts;
			infile_pts.open(pts_dir_name);

			string ss;
			int yy;
			infile_pts &gt;&gt; ss &gt;&gt; yy;
			infile_pts &gt;&gt; ss &gt;&gt; yy;
			infile_pts &gt;&gt; ss;
			for (int i = 0; i &lt; 5; i++)
			{
				infile_pts &gt;&gt; pts_gt[i*2] &gt;&gt; pts_gt[i*2+1];
			}
			
			infile_pts.close();
			
			
			float error = computer_error(pts_gt, pts_pre);
			error = error / 5.0;
			if (error &lt;= thread)
				pos++;
			

			cout &lt;&lt; error &lt;&lt; " " &lt;&lt; endl;
			infile_list &gt;&gt; name_list;
			//cout &lt;&lt; name_list &lt;&lt; endl;

		}

		else
			break;

	}	


}

acc = float(pos) / float(count);
cout &lt;&lt; "accury: " &lt;&lt; acc &lt;&lt; endl;

infile_list.close();
infile_pre.close();

}

// computer alinment loss
float computer_error(vector<float> pts_gt, vector<vector<float>> pts_pre)
{
if (pts_pre.size() == 0)
return 10;

float RMSE, d_outer, align_loss;
d_outer = sqrt((pts_gt[0] - pts_gt[8])*(pts_gt[0] - pts_gt[8]) + (pts_gt[1] - pts_gt[9])*(pts_gt[1] - pts_gt[9]));
for (int i = 0; i &lt; pts_pre.size(); i++)
{
	RMSE = 0;
	for (int j = 0; j &lt; 5; j++)
	{
		RMSE += sqrt((pts_gt[2 * j] - pts_pre[i][2 * j])*(pts_gt[2 * j] - pts_pre[i][2 * j]) + (pts_gt[2 * j + 1] - pts_pre[i][2 * j + 1])*(pts_gt[2 * j + 1] - pts_pre[i][2 * j + 1]));
	}
	RMSE = RMSE / d_outer;

	if (i == 0)
	{
		align_loss = RMSE;
	}
	else
	{
		if (align_loss &gt; RMSE)
			align_loss = RMSE;
	}
}
return align_loss;

}

//字符串分割函数
std::vector<std::string> split(std::string& str, std::string& pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
//str += pattern;//扩展字符串以方便操作
int size = str.size();

pos = str.find(pattern, 0);
if (pos&lt;size) {
	std::string s1 = str.substr(0, pos);
	std::string s2 = str.substr(pos + 1, size - 1);
	result.push_back(s1);
	result.push_back(s2);
}

return result;

}

void str2int(int &int_temp, const string &string_temp)
{
stringstream stream(string_temp);
stream >> int_temp;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值