两张图片的相似度主函数
在Linux下运行相似度函数,先输入test_face_verification.cpp的code再进行编译输出结果
#include
using namespace std;
#ifdef _WIN32
#pragma once
#include
#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) \
CVAUX_STR(CV_SUBMINOR_VERSION)
#ifdef _DEBUG
#define cvLIB(name) "opencv_" name CV_VERSION_ID "d"
#else
#define cvLIB(name) "opencv_" name CV_VERSION_ID
#endif //_DEBUG
#pragma comment( lib, cvLIB("core") )
#pragma comment( lib, cvLIB("imgproc") )
#pragma comment( lib, cvLIB("highgui") )
#endif //_WIN32
#if defined(__unix__) || defined(__APPLE__)
#ifndef fopen_s
#define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename),(mode)))==NULL
#endif //fopen_s
#endif //__unix
#include
#include
#include "face_identification.h"
#include "recognizer.h"
#include "../build/face_detection.h"
#include "../build/face_alignment.h"
#include "math_functions.h"
#include
#include
#include
#include
#include "common_net.h"
#include "bias_adder_net.h"
#include "bn_net.h"
#include "conv_net.h"
#include "eltwise_net.h"
#include "inner_product_net.h"
#include "max_pooling_net.h"
#include "spatial_transform_net.h"
#include "pad_net.h"
REGISTER_NET_CLASS(Common);
REGISTER_NET_CLASS(BiasAdder);
REGISTER_NET_CLASS(Bn);
REGISTER_NET_CLASS(Conv);
REGISTER_NET_CLASS(Eltwise);
REGISTER_NET_CLASS(InnerProduct);
REGISTER_NET_CLASS(MaxPooling);
REGISTER_NET_CLASS(SpatialTransform);
REGISTER_NET_CLASS(Pad);
using namespace seeta;
#define TEST(major, minor) major##_##minor##_Tester()
#define EXPECT_NE(a, b) if ((a) == (b)) std::cout << "ERROR: "
#define EXPECT_EQ(a, b) if ((a) != (b)) std::cout << "ERROR: "
#ifdef _WIN32
std::string DATA_DIR = "../../data/";
std::string MODEL_DIR = "../../model/";
#else
std::string DATA_DIR = "../data/";
std::string MODEL_DIR = "../model/";
#endif
int main(int argc, char* argv[]) {
// Initialize face detection model
seeta::FaceDetection detector("./seeta_fd_frontal_v1.0.bin");
detector.SetMinFaceSize(40);
detector.SetScoreThresh(2.f);
detector.SetImagePyramidScaleFactor(0.8f);
detector.SetWindowStep(4, 4);
// Initialize face alignment model
seeta::FaceAlignment point_detector("./seeta_fa_v1.1.bin");
// Initialize face Identification model
FaceIdentification face_recognizer((MODEL_DIR + "seeta_fr_v1.0.bin").c_str());
std::string test_dir = DATA_DIR + "test_face_recognizer/";
//load image
/*cv::Mat gallery_img_color = cv::imread(test_dir + "images/compare_im/30.jpg", 1);
cv::Mat gallery_img_gray;
cv::cvtColor(gallery_img_color, gallery_img_gray, CV_BGR2GRAY);
FILE *fp1=fopen("pic/pic30","wb");
if(fp1==0)
return 0;
fwrite(gallery_img_color.data, gallery_img_color.dataend - gallery_img_color.datastart + 3, 1, fp1);
fclose(fp1);
FILE *fp2=fopen("pic/pic30_g","wb");
if(fp2==0)
return 0;
fwrite(gallery_img_gray.data, gallery_img_gray.dataend - gallery_img_gray.datastart + 1, 1, fp2);
fclose(fp2);
cv::Mat probe_img_color = cv::imread(test_dir + "images/compare_im/31.jpg", 1);
cv::Mat probe_img_gray;
cv::cvtColor(probe_img_color, probe_img_gray, CV_BGR2GRAY);
FILE *fp3=fopen("pic/pic31","wb");
if(fp3==0)
return 0;
fwrite(probe_img_color.data, probe_img_color.dataend - probe_img_color.datastart + 3, 1, fp3);
fclose(fp3);
FILE *fp4=fopen("pic/pic31_g","wb");
if(fp4==0)
return 0;
fwrite(probe_img_gray.data, probe_img_gray.dataend - probe_img_gray.datastart + 1, 1, fp4);
fclose(fp4);
ImageData gallery_img_data_color(gallery_img_color.cols, gallery_img_color.rows, gallery_img_color.channels());
gallery_img_data_color.data = gallery_img_color.data;
ImageData gallery_img_data_gray(gallery_img_gray.cols, gallery_img_gray.rows, gallery_img_gray.channels());
gallery_img_data_gray.data = gallery_img_gray.data;
ImageData probe_img_data_color(probe_img_color.cols, probe_img_color.rows, probe_img_color.channels());
probe_img_data_color.data = probe_img_color.data;
ImageData probe_img_data_gray(probe_img_gray.cols, probe_img_gray.rows, probe_img_gray.channels());
probe_img_data_gray.data = probe_img_gray.data;
*/
cv::Mat srcImage=cv::imread("test.jpg");
if (srcImage.empty())
{
std::cout << "Please take an image!";
return 0;
}
cv::resize(srcImage,srcImage,cv::Size(250,250),0,0,CV_INTER_LINEAR);
imwrite("test.jpg", srcImage);
cv::Mat srcImage_gray;
cv::cvtColor(srcImage,srcImage_gray, CV_BGR2GRAY);
FILE *fp11=fopen("test","wb");
if(fp11==0)
return 0;
fwrite(srcImage.data, srcImage.dataend - srcImage.datastart + 3, 1, fp11);
fclose(fp11);
FILE *fp22=fopen("test_g","wb");
if(fp22==0)
return 0;
fwrite(srcImage_gray.data, srcImage_gray.dataend - srcImage_gray.datastart + 1, 1, fp22);
fclose(fp22);
string name[] = { "pic/pic01", "pic/pic01_g", "pic/pic02", "pic/pic02_g",
"pic/pic03", "pic/pic03_g","pic/pic04", "pic/pic04_g","pic/pic05", "pic/pic05_g",
"pic/pic06", "pic/pic06_g","pic/pic07", "pic/pic07_g","pic/pic08", "pic/pic08_g",
"pic/pic09", "pic/pic09_g","pic/pic10", "pic/pic10_g","pic/pic11", "pic/pic11_g",
"pic/pic12", "pic/pic12_g","pic/pic13", "pic/pic13_g","pic/pic14", "pic/pic14_g",
"pic/pic15", "pic/pic15_g","pic/pic16", "pic/pic16_g","pic/pic17", "pic/pic17_g",
"pic/pic18", "pic/pic18_g","pic/pic19", "pic/pic19_g","pic/pic20", "pic/pic20_g",
"pic/pic21", "pic/pic21_g","pic/pic22", "pic/pic22_g","pic/pic23", "pic/pic23_g",
"pic/pic24", "pic/pic24_g","pic/pic25", "pic/pic25_g","pic/pic26", "pic/pic26_g",
"pic/pic27", "pic/pic27_g","pic/pic28", "pic/pic28_g","pic/pic29", "pic/pic29_g",
"pic/pic30", "pic/pic30_g","pic/pic31", "pic/pic31_g"};
ImageData gallery_img_data_color(250, 250, 3);
FILE *fp1a=fopen("test","rb");
if(fp1a==0)
return 0;
gallery_img_data_color.data = new uint8_t[250 * 250 * 3 + 3];
fread(gallery_img_data_color.data, 250 * 250 * 3 + 3, 1, fp1a);
fclose(fp1a);
ImageData gallery_img_data_gray(250, 250, 1);
FILE *fp2a=fopen("test_g","rb");
if(fp2a==0)
return 0;
gallery_img_data_gray.data = new uint8_t[250 * 250 + 1];
fread(gallery_img_data_gray.data, 250 * 250 + 1, 1, fp2a);
fclose(fp2a);
//std::ifstream ifs;//pc
// std::string img_name;//pc
//ifs.open("test_file_list1.txt", std::ifstream::in);//pc
//int img_num = 0;
// while (ifs >> img_name) {
//img_num ++ ;
// read image
//cv::Mat src_img = cv::imread(img_name, 1);
// EXPECT_NE(src_img.data, nullptr) << "Load image error!";
//}
float mx=0;
int temp=0;
for (int i=0;i<3;i++ ){
ImageData probe_img_data_color(250, 250, 3);
FILE *fp3a=fopen(name[2*i].c_str(),"rb");
if(fp3a==0)
return 0;
probe_img_data_color.data = new uint8_t[250 * 250 * 3 + 3];
fread(probe_img_data_color.data, 250 * 250 * 3 + 3, 1, fp3a);
fclose(fp3a);
ImageData probe_img_data_gray(250, 250, 1);
FILE *fp4a=fopen(name[2*i+1].c_str(),"rb");
if(fp4a==0)
return 0;
probe_img_data_gray.data = new uint8_t[250 * 250 + 1];
fread(probe_img_data_gray.data, 250 * 250 + 1, 1, fp4a);
fclose(fp4a);
// Detect faces
std::vector gallery_faces = detector.Detect(gallery_img_data_gray);
int32_t gallery_face_num = static_cast(gallery_faces.size());
std::vector probe_faces = detector.Detect(probe_img_data_gray);
int32_t probe_face_num = static_cast(probe_faces.size());
if (gallery_face_num == 0 || probe_face_num==0)
{
std::cout << "Faces are not detected.";
return 0;
}
/*3-14for (int32_t i = 0; i < 1; i++) {
//face_rect.x = faces[i].bbox.x;
//face_rect.y = faces[i].bbox.y;
//face_rect.width = faces[i].bbox.width;
//face_rect.height = faces[i].bbox.height;
cout << "x =" << gallery_faces[i].bbox.x << endl;
cout << "y =" << gallery_faces[i].bbox.y << endl;
cout << "w =" << gallery_faces[i].bbox.width << endl;
cout << "h =" << gallery_faces[i].bbox.height << endl;
cout << "x =" << probe_faces[i].bbox.x << endl;
cout << "y =" << probe_faces[i].bbox.y << endl;
cout << "w =" << probe_faces[i].bbox.width << endl;
cout << "h =" << probe_faces[i].bbox.height << endl;
//cv::rectangle(img, face_rect, CV_RGB(0, 0, 255), 4, 8, 0);
}*/
// Detect 5 facial landmarks
seeta::FacialLandmark gallery_points[5];
point_detector.PointDetectLandmarks(gallery_img_data_gray, gallery_faces[0], gallery_points);
seeta::FacialLandmark probe_points[5];
point_detector.PointDetectLandmarks(probe_img_data_gray, probe_faces[0], probe_points);
/* for (int i = 0; i<5; i++)
{
cv::circle(gallery_img_color, cv::Point(gallery_points[i].x, gallery_points[i].y), 2,
CV_RGB(0, 255, 0));
cv::circle(probe_img_color, cv::Point(probe_points[i].x, probe_points[i].y), 2,
CV_RGB(0, 255, 0));
}*/
//cv::imwrite("gallery_point_result.jpg", gallery_img_color);
//cv::imwrite("probe_point_result.jpg", probe_img_color);
// Extract face identity feature
float gallery_fea[2048];
float probe_fea[2048];
/*3-14for(int i = 0; i<5; i++){
std::cout<
face_recognizer.ExtractFeatureWithCrop(gallery_img_data_color, gallery_points, gallery_fea);
face_recognizer.ExtractFeatureWithCrop(probe_img_data_color, probe_points, probe_fea);
// Caculate similarity of two faces
float sim = face_recognizer.CalcSimilarity(gallery_fea, probe_fea);
std::cout << sim << std::endl;
if(sim>mx)
{
mx=sim;
std::cout << mx << std::endl;
temp=i;
std::cout << temp << std::endl;
}
}
//std::cout << mx << std::endl;
if(mx>0.72)
{cout<<" the most likely pic is "<2 *temp]<else
cout<<" The image library does not have the person."<return 0;
}
cpan@admin0-PowerEdge-R730:~/workspace/SeetaFaceEngine-master/FaceIdentification/build2$ make -j8
cpan@admin0-PowerEdge-R730:~/workspace/SeetaFaceEngine-master/FaceIdentification/build2$ ./src/test/test_face_verification.bin
经过在Linux环境下测试运行通过将code复制到Xilinx公司的SDSOC SDK运行release生成SD_Card目录下的文件,copy到SD卡上运行,SDSOC上的代码code为:
#include
using namespace std;
//#include
//#include
#include "face_identification.h"
#include "recognizer.h"
#include "face_detection.h"
#include "face_alignment.h"
#include "math_functions.h"
#include
#include
#include
#include
#include "common_net.h"
#include "bias_adder_net.h"
#include "bn_net.h"
#include "conv_net.h"
#include "eltwise_net.h"
#include "inner_product_net.h"
#include "max_pooling_net.h"
#include "spatial_transform_net.h"
#include "pad_net.h"
REGISTER_NET_CLASS(Common);
REGISTER_NET_CLASS(BiasAdder);
REGISTER_NET_CLASS(Bn);
REGISTER_NET_CLASS(Conv);
REGISTER_NET_CLASS(Eltwise);
REGISTER_NET_CLASS(InnerProduct);
REGISTER_NET_CLASS(MaxPooling);
REGISTER_NET_CLASS(SpatialTransform);
REGISTER_NET_CLASS(Pad);
using namespace seeta;
#define TEST(major, minor) major##_##minor##_Tester()
#define EXPECT_NE(a, b) if ((a) == (b)) std::cout << "ERROR: "
#define EXPECT_EQ(a, b) if ((a) != (b)) std::cout << "ERROR: "
#ifdef _WIN32
std::string DATA_DIR = "../../data/";
std::string MODEL_DIR = "../../model/";
#else
std::string DATA_DIR = "../data/";
std::string MODEL_DIR = "../model/";
#endif
int main(int argc, char* argv[]) {
string name[] = { "pic/pic01", "pic/pic01_g", "pic/pic02", "pic/pic02_g",
"pic/pic03", "pic/pic03_g","pic/pic04", "pic/pic04_g","pic/pic05", "pic/pic05_g",
"pic/pic06", "pic/pic06_g","pic/pic07", "pic/pic07_g","pic/pic08", "pic/pic08_g",
"pic/pic09", "pic/pic09_g","pic/pic10", "pic/pic10_g","pic/pic11", "pic/pic11_g",
"pic/pic12", "pic/pic12_g","pic/pic13", "pic/pic13_g","pic/pic14", "pic/pic14_g",
"pic/pic15", "pic/pic15_g","pic/pic16", "pic/pic16_g","pic/pic17", "pic/pic17_g",
"pic/pic18", "pic/pic18_g","pic/pic19", "pic/pic19_g","pic/pic20", "pic/pic20_g",
"pic/pic21", "pic/pic21_g","pic/pic22", "pic/pic22_g","pic/pic23", "pic/pic23_g",
"pic/pic24", "pic/pic24_g","pic/pic25", "pic/pic25_g","pic/pic26", "pic/pic26_g",
"pic/pic27", "pic/pic27_g","pic/pic28", "pic/pic28_g","pic/pic29", "pic/pic29_g",
"pic/pic30", "pic/pic30_g","pic/pic31", "pic/pic31_g"};
//int res[10];
ImageData gallery_img_data_color(250, 250, 3);
FILE *fp1=fopen("test","rb");
//FILE *fp1=fopen("./mnt/pic1","rb");
if(fp1==0)
return 0;
gallery_img_data_color.data = new uint8_t[250 * 250 * 3 + 3];
fread(gallery_img_data_color.data, 250 * 250 * 3 + 3, 1, fp1);
fclose(fp1);
ImageData gallery_img_data_gray(250, 250, 1);
FILE *fp2=fopen("test_g","rb");
//FILE *fp2=fopen("./mnt/pic1_g","rb");
if(fp2==0)
return 0;
gallery_img_data_gray.data = new uint8_t[250 * 250 + 1];
fread(gallery_img_data_gray.data, 250 * 250 + 1, 1, fp2);
fclose(fp2);
float mx=0;
int temp=0;
for (int i=0;i<6;i++ ){
ImageData probe_img_data_color(250, 250, 3);
FILE *fp3=fopen(name[2*i].c_str(),"rb");//!!!!!!!!
//FILE *fp3=fopen("./mnt/pic2","rb");
if(fp3==0)
return 0;
probe_img_data_color.data = new uint8_t[250 * 250 * 3 + 3];
fread(probe_img_data_color.data, 250 * 250 * 3 + 3, 1, fp3);
fclose(fp3);
ImageData probe_img_data_gray(250, 250, 1);
FILE *fp4=fopen(name[2*i+1].c_str(),"rb");//!!!!!!
//FILE *fp4=fopen("./mnt/pic2_g","rb");
if(fp4==0)
return 0;
probe_img_data_gray.data = new uint8_t[250 * 250 + 1];
fread(probe_img_data_gray.data, 250 * 250 + 1, 1, fp4);
fclose(fp4);
perf_counter main_ctr;
main_ctr.start();
// Initialize face detection model
seeta::FaceDetection detector("./mnt/seeta_fd_frontal_v1.0.bin");
detector.SetMinFaceSize(40);
detector.SetScoreThresh(2.f);
detector.SetImagePyramidScaleFactor(0.8f);
detector.SetWindowStep(4, 4);
// Initialize face alignment model
seeta::FaceAlignment point_detector("./mnt/seeta_fa_v1.1.bin");
// Initialize face Identification model
FaceIdentification face_recognizer("./mnt/seeta_fr_v1.0.bin");
// Detect faces
std::vector gallery_faces = detector.Detect(gallery_img_data_gray);
int32_t gallery_face_num = static_cast(gallery_faces.size());
std::vector probe_faces = detector.Detect(probe_img_data_gray);
int32_t probe_face_num = static_cast(probe_faces.size());
if (gallery_face_num == 0 || probe_face_num==0)
{
std::cout << "Faces are not detected.";
return 0;
}
// Detect 5 facial landmarks
seeta::FacialLandmark gallery_points[5];
point_detector.PointDetectLandmarks(gallery_img_data_gray, gallery_faces[0], gallery_points);
seeta::FacialLandmark probe_points[5];
point_detector.PointDetectLandmarks(probe_img_data_gray, probe_faces[0], probe_points);
// Extract face identity feature
float gallery_fea[2048];
float probe_fea[2048];
face_recognizer.ExtractFeatureWithCrop(gallery_img_data_color, gallery_points, gallery_fea);
face_recognizer.ExtractFeatureWithCrop(probe_img_data_color, probe_points, probe_fea);
// Caculate similarity of two faces
float sim = face_recognizer.CalcSimilarity(gallery_fea, probe_fea);
std::cout << sim <if(sim>mx)
{
mx=sim;
std::cout << mx << std::endl;
temp=i;
std::cout << temp << std::endl;
}
main_ctr.stop();
std::cout << main_ctr.avg_cpu_cycles() <if(mx>0.7)
{cout<<" the most likely pic is "<2*temp]<else
cout<<" The image library does not have the person."<return 0;
}
程序在经过FPGA加速后运行时间减少一半
Tips:
1、SDSOC: FPGA加速时波特率设置100Mhz matrix_product_hz1
一般刚跑程序时,只需要勾选:Generate SD Card Image 与Estimate Performance
Generate SD Card Image这个将生成的网标文件elf和BIN文件拷贝到板子上运行
Estimate Performance:模拟综合,看资源消耗情况
需要加入FPGA加速的时候,勾选Generate Bitstream,配置FPGA部分,消耗时间长
2、将SDSOC下lab运行处的SD_card所有程序copy到mnt下
运行时将图像库pic放在主目录下,把test与test_g也放在主目录下
运行程序./mnt/SeetaFace.elf
3、由于不用opencv库,所以通过/home/cpan/workspace/SeetaFaceEnginemaster/FaceIdentification/build2下,
运行make -j8
生成test_face_verification.bin文件,在运行该文件
./src/test/test_face_verification.bin
这个是在Linux下跑的源程序,可以输入需要处理的图像,利用OpenCV生成test与test_g,当在板子上运行时,需要将测试的这两个文件拷贝到板子上去。