diff --git a/src/facenet.cpp b/src/facenet.cpp index 4aee9c4..5e840f8 100755 --- a/src/facenet.cpp +++ b/src/facenet.cpp @@ -4,7 +4,11 @@ #include "facenet.h" - +/** + * stem网络 + * @param image 输入图片 + * @param output 输出featuremap 指针形式 + */ void facenet::Stem(Mat &image, pBox *output) { pBox *rgb = new pBox; pBox *conv1_out = new pBox; @@ -161,6 +165,13 @@ void facenet::Stem(Mat &image, pBox *output) { freeBN(conv6_beta); } +/** + * Inception_resnet_A网络 + * @param input 输入featuremap + * @param output 输出featuremap + * @param filepath 模型文件路径 + * @param scale 比例系数 + */ void facenet::Inception_resnet_A(pBox *input, pBox *output, string filepath, float scale) { pBox *conv1_out = new pBox; pBox *conv2_out = new pBox; @@ -327,6 +338,11 @@ void facenet::Inception_resnet_A(pBox *input, pBox *output, string filepath, flo freeBN(conv6_beta); } +/** + * Reduction_A + * @param input 输入featuremap + * @param output 输出featuremap + */ void facenet::Reduction_A(pBox *input, pBox *output) { pBox *conv1_out = new pBox; pBox *conv2_out = new pBox; @@ -429,6 +445,13 @@ void facenet::Reduction_A(pBox *input, pBox *output) { freeBN(conv4_beta); } +/** + * Inception_resnet_B网络 + * @param input 输入featuremap + * @param output 输出featuremap + * @param filepath 模型文件路径 + * @param scale 比例系数 + */ void facenet::Inception_resnet_B(pBox *input, pBox *output, string filepath, float scale) { pBox *conv1_out = new pBox; pBox *conv2_out = new pBox; @@ -550,6 +573,11 @@ void facenet::Inception_resnet_B(pBox *input, pBox *output, string filepath, flo freeBN(conv4_beta); } +/** + * Reduction_B + * @param input 输入featuremap + * @param output 输出featuremap + */ void facenet::Reduction_B(pBox *input, pBox *output) { pBox *conv1_out = new pBox; pBox *conv2_out = new pBox; @@ -714,6 +742,13 @@ void facenet::Reduction_B(pBox *input, pBox *output) { freeBN(conv7_beta); } +/** + * Inception_resnet_C网络 + * @param input 输入featuremap + * @param output 输出featuremap + * @param filepath 模型文件路径 + * @param scale 比例系数 + */ void facenet::Inception_resnet_C(pBox *input, pBox *output, string filepath, float scale) { pBox *conv1_out = new pBox; pBox *conv2_out = new pBox; @@ -836,6 +871,13 @@ void facenet::Inception_resnet_C(pBox *input, pBox *output, string filepath, flo freeBN(conv4_beta); } +/** + * Inception_resnet_C网络 最后无激活函数 + * @param input 输入featuremap + * @param output 输出featuremap + * @param filepath 模型文件路径 + * @param scale 比例系数 + */ void facenet::Inception_resnet_C_None(pBox *input, pBox *output, string filepath) { pBox *conv1_out = new pBox; pBox *conv2_out = new pBox; @@ -950,6 +992,11 @@ void facenet::Inception_resnet_C_None(pBox *input, pBox *output, string filepath freeBN(conv4_beta); } +/** + * 平均池化 + * @param input 输入featuremap + * @param output 输出featuremap + */ void facenet::AveragePooling(pBox *input, pBox *output) { // cout << "size:" << input->height << endl; avePoolingInit(input, output, input->height, 2); @@ -965,6 +1012,12 @@ void facenet::Flatten(pBox *input, pBox *output) { memcpy(output->pdata, input->pdata, output->channel * output->width * output->height * sizeof(mydataFmt)); } +/** + * 全连接网络 + * @param input 输入featuremap + * @param output 输出featuremap + * @param filepath 网络模型参数文件路径 + */ //参数还未设置 void facenet::fully_connect(pBox *input, pBox *output, string filepath) { struct Weight *conv1_wb = new Weight; @@ -1014,8 +1067,6 @@ void facenet::run(Mat &image, vector &o, int count) { pBox *output = new pBox; pBox *input; Stem(image, output); -// printData(output); -// return; cout << "Stem Finally" << endl; input = output; output = new pBox; @@ -1030,7 +1081,6 @@ void facenet::run(Mat &image, vector &o, int count) { Reduction_A(input, output); cout << "Reduction_A Finally" << endl; input = output; -// freepBox(output); output = new pBox; for (int j = 0; j < 10; ++j) { // model_128/block17_1_list.txt @@ -1048,10 +1098,8 @@ void facenet::run(Mat &image, vector &o, int count) { for (int k = 0; k < 5; ++k) { // model_128/block8_1_list.txt string filepath = "../model_" + to_string(Num) + "/block8_" + to_string((k + 1)) + "_list.txt"; -// cout << filepath << endl; Inception_resnet_C(input, output, filepath, 0.2); input = output; -// freepBox(output); output = new pBox; } cout << "Inception_resnet_C Finally" << endl; @@ -1070,6 +1118,10 @@ void facenet::run(Mat &image, vector &o, int count) { output = new pBox; fully_connect(input, output, "../model_" + to_string(Num) + "/Bottleneck_list.txt"); cout << "Fully_Connect Finally" << endl; + + /** + * L2归一化 + */ mydataFmt sq = 0, sum = 0; for (int i = 0; i < Num; ++i) { sq = pow(output->pdata[i], 2); diff --git a/src/pikaqiu.cpp b/src/pikaqiu.cpp index 21bd19e..7c6d977 100755 --- a/src/pikaqiu.cpp +++ b/src/pikaqiu.cpp @@ -3,6 +3,34 @@ #include "facenet.h" #include +/** + * 图片缩小 + * @param src 输入图片 + * @return 返回图片 + */ +Mat RS(Mat &src) { + int w = src.cols; + int h = src.rows; + int wtemp, htemp; + Mat dst; + cout << w << "\t" << h << endl; + float threshold = 300.0; + if (h > threshold) { + wtemp = (int) (threshold / h * w); + htemp = threshold; + dst = Mat::zeros(htemp, wtemp, CV_8UC3); //我要转化为htemp*wtemp大小的 + resize(src, dst, dst.size()); + } + cout << wtemp << "\t" << htemp << endl; + cout << "-------------------" << endl; + return dst; +} + +/** + * 加载csv中的emb + * @param num csv的个数 + * @param vecVec 将emb参数存在二维vector中,外层定位某个人,内层定位某个emb值 + */ void load_emb_csv(int num, vector> &vecVec) { for (int i = 0; i < num; ++i) { ifstream inFile("../emb_csv/" + to_string(i) + ".csv", ios::in); @@ -28,6 +56,11 @@ void load_emb_csv(int num, vector> &vecVec) { } } +/** + * 预处理,保存emb值到csv中 + * @param o 保存一个人的emb值 + * @param num 第几个人,确定文件名 + */ void write_emb_csv(vector &o, int num) { ofstream outFile; outFile.open("../emb_csv/" + to_string(num) + ".csv", ios::out); // 打开模式可省略 @@ -44,6 +77,12 @@ void write_emb_csv(vector &o, int num) { cout << "write over!" << endl; } +/** + * 对比两个人的emb值,计算空间欧氏距离 + * @param lineArray0 第一个人的emb值 + * @param lineArray1 第二个人的emb值 + * @return + */ float compare(vector &lineArray0, vector &lineArray1) { mydataFmt sum = 0; for (int i = 0; i < Num; ++i) { @@ -56,6 +95,12 @@ float compare(vector &lineArray0, vector &lineArray1) { return result; } +/** + * 执行mtcnn网络 + * @param image 图片 + * @param vecRect 获取人脸框 + * @param vecPoint 获取人脸五个点 + */ void run_mtcnn(Mat &image, vector &vecRect, vector &vecPoint) { // vector vecPoint; mtcnn find(image.rows, image.cols); @@ -68,6 +113,12 @@ void run_mtcnn(Mat &image, vector &vecRect, vector &vecPoint) { // } } +/** + * 执行facenet网络(一张图与数据库里的多个人脸做比对) + * @param image 图片 + * @param vecRect 人脸框 + * @param csv_num csv数量 缺省 0 写csv模式 + */ void run_facenet(Mat &image, vector &vecRect, int csv_num = 0) { for (int i = 0; i < vecRect.size(); ++i) { Mat fourthImage; @@ -99,28 +150,11 @@ void run_facenet(Mat &image, vector &vecRect, int csv_num = 0) { } } -float test_compare(vector &lineArray0, vector &lineArray1) { - mydataFmt sum = 0; - for (int i = 0; i < Num; ++i) { -// cout << lineArray0[i] << "===" << lineArray1[i] << endl; - mydataFmt sub = lineArray0[i] - lineArray1[i]; - mydataFmt square = pow(sub, 2); - sum += square; - } - mydataFmt result = sqrt(sum); - return result; -} - -void test_facenet(Mat &image, vector &vecRect, vector &n) { - Mat fourthImage; - resize(image(vecRect[0]), fourthImage, Size(160, 160), 0, 0, cv::INTER_LINEAR); - facenet ggg; -// mydataFmt *o = new mydataFmt[Num]; -// vector n; - vector> o; - ggg.run(fourthImage, n, 0); -} - +/** + * 执行run_mtcnn和run_facenet + * 一张图,通过run_mtcnn找到n个人脸 + * n个人脸通过run_facenet与数据库里的m个人脸数据做匹配,看看这n个人分别是谁 + */ void run() { int b = 0; if (b == 0) { @@ -182,9 +216,30 @@ void run() { } } +/** + * 执行单次单人的facenet网络 + * @param image 输入图片 + * @param vecRect 人脸框 + * @param n emb值 + */ +void test_facenet(Mat &image, vector &vecRect, vector &n) { + Mat fourthImage; + resize(image(vecRect[0]), fourthImage, Size(160, 160), 0, 0, cv::INTER_LINEAR); + facenet ggg; +// mydataFmt *o = new mydataFmt[Num]; +// vector n; +// vector> o; + ggg.run(fourthImage, n, 0); +} + +/** + * 对比两张图两个人的emb + */ void test() { Mat image0 = imread("../hejiong1.jpeg"); Mat image1 = imread("../hejiong0.jpeg"); + image0 = RS(image0); + image1 = RS(image1); clock_t start; start = clock(); @@ -209,7 +264,7 @@ void test() { -1); } - float result = test_compare(n0, n1); + float result = compare(n0, n1); cout << "-------------------" << endl; cout << result << endl; if (result < 0.45) @@ -218,20 +273,26 @@ void test() { cout << "Probably not the same person" << endl; imshow("result0", image0); +// resizeWindow("result0", w0, h0); //创建一个固定值大小的窗口 imwrite("../result0.jpg", image0); imshow("result1", image1); imwrite("../result1.jpg", image1); start = clock() - start; // cout<<"time is "<