整理代码结构

整理代码结构
This commit is contained in:
2019-12-25 15:25:46 +08:00
parent 9951985c6b
commit 11e7f54983
5 changed files with 173 additions and 137 deletions

View File

@@ -20,7 +20,7 @@ void facenet::printData(pBox *in) {
cout << "printData" << endl;
}
void facenet::run(Mat &image, mydataFmt *o, int count) {
void facenet::run(Mat &image, vector<mydataFmt> &o, int count) {
cout << "=====This is No." + to_string(count) + " Picture=====" << endl;
pBox *output = new pBox;
pBox *input;
@@ -94,7 +94,8 @@ void facenet::run(Mat &image, mydataFmt *o, int count) {
divisor = sqrt(sum);
}
for (int j = 0; j < Num; ++j) {
o[j] = output->pdata[j] / divisor;
// o[j] = output->pdata[j] / divisor;
o.push_back(output->pdata[j] / divisor);
}
// memcpy(o, output->pdata, Num * sizeof(mydataFmt));
freepBox(output);

View File

@@ -14,7 +14,7 @@ public:
~facenet();
void run(Mat &image, mydataFmt *o, int count = 1);
void run(Mat &image, vector<mydataFmt> &o, int count = 1);
void prewhiten(Mat &image);

View File

@@ -473,7 +473,7 @@ mtcnn::~mtcnn() {
delete[]simpleFace_;
}
void mtcnn::findFace(Mat &image) {
void mtcnn::findFace(Mat &image, vector<Rect> &vecRect, vector<Point> &vecPoint) {
struct orderScore order;
int count = 0;
if (image.empty())
@@ -568,13 +568,20 @@ void mtcnn::findFace(Mat &image) {
int saveflag = 2;//0 预处理 1 预测识别 2 单独运行
for (vector<struct Bbox>::iterator it = thirdBbox_.begin(); it != thirdBbox_.end(); it++) {
if ((*it).exist && ((*it).y1 < (*it).y2) && ((*it).x1 < (*it).x2)) {
Rect temp((*it).y1, (*it).x1, (*it).y2 - (*it).y1, (*it).x2 - (*it).x1);
vecPoint.push_back(Point((*it).y1, (*it).x1));
vecPoint.push_back(Point((*it).y2, (*it).x2));
for (int num = 0; num < 5; num++)
vecPoint.push_back(Point((int) *(it->ppoint + num), (int) *(it->ppoint + num + 5)));
vecRect.push_back(temp);
if (saveflag == 0) {
Rect temp((*it).y1, (*it).x1, (*it).y2 - (*it).y1, (*it).x2 - (*it).x1);
Mat fourthImage;
resize(image(temp), fourthImage, Size(299, 299), 0, 0, cv::INTER_LINEAR);
facenet ggg;
mydataFmt *o = new mydataFmt[Num];
ggg.run(fourthImage, o, num);
// ggg.run(fourthImage, o, num);
imshow("result", fourthImage);
imwrite("../emb_img/" + to_string(num) + ".jpg", fourthImage);
waitKey(3000);
@@ -595,58 +602,9 @@ void mtcnn::findFace(Mat &image) {
outFile << endl;
outFile.close();
delete o;
} else if (saveflag == 1) {
Rect temp((*it).y1, (*it).x1, (*it).y2 - (*it).y1, (*it).x2 - (*it).x1);
Mat fourthImage;
resize(image(temp), fourthImage, Size(299, 299), 0, 0, cv::INTER_LINEAR);
facenet ggg;
mydataFmt *o = new mydataFmt[Num];
ggg.run(fourthImage, o, num);
imshow("result", fourthImage);
waitKey(3000);
destroyWindow("result");
ifstream inFile("../emb_csv/" + to_string(num) + ".csv", ios::in);
string lineStr;
// vector<vector<mydataFmt>> strArray;
vector<mydataFmt> lineArray;
while (getline(inFile, lineStr)) {
// 打印整行字符串
// cout << lineStr << endl;
// 存成二维表结构
stringstream ss(lineStr);
string str;
// vector<mydataFmt> lineArray;
// 按照逗号分隔
// mydataFmt nnn = 0;
while (getline(ss, str, ',')) {
lineArray.push_back(atof(str.c_str()));
// cout << str << endl;
}
// strArray.push_back(lineArray);
}
mydataFmt sum = 0;
for (int i = 0; i < Num; ++i) {
cout << o[i] << "===" << lineArray[i] << endl;
mydataFmt sub = o[i] - lineArray[i];
mydataFmt square = pow(sub, 2);
sum += square;
}
mydataFmt result = sqrt(sum);
cout << result << endl;
if (result < 0.85)
cout << "it's me" << endl;
else
cout << "unknow" << endl;
delete o;
}
num++;
// num++;
// }
// if ((*it).exist) {
rectangle(image, Point((*it).y1, (*it).x1), Point((*it).y2, (*it).x2), Scalar(0, 0, 255), 2, 8, 0);
for (int num = 0; num < 5; num++)
circle(image, Point((int) *(it->ppoint + num), (int) *(it->ppoint + num + 5)), 2, Scalar(0, 255, 255),
-1);
}
}

View File

@@ -6,17 +6,17 @@
class Pnet {
public:
Pnet();
Pnet();
~Pnet();
~Pnet();
void run(Mat &image, mydataFmt scale);
void run(Mat &image, mydataFmt scale);
float nms_threshold;
mydataFmt Pthreshold;
bool firstFlag;
vector<struct Bbox> boundingBox_;
vector<orderScore> bboxScore_;
float nms_threshold;
mydataFmt Pthreshold;
bool firstFlag;
vector<struct Bbox> boundingBox_;
vector<orderScore> bboxScore_;
private:
//the image for mxnet conv
struct pBox *rgb;
@@ -32,33 +32,33 @@ private:
//the 4th layer's out out
struct pBox *location_;
//Weight
struct Weight *conv1_wb;
struct pRelu *prelu_gmma1;
struct Weight *conv2_wb;
struct pRelu *prelu_gmma2;
struct Weight *conv3_wb;
struct pRelu *prelu_gmma3;
struct Weight *conv4c1_wb;
struct Weight *conv4c2_wb;
//Weight
struct Weight *conv1_wb;
struct pRelu *prelu_gmma1;
struct Weight *conv2_wb;
struct pRelu *prelu_gmma2;
struct Weight *conv3_wb;
struct pRelu *prelu_gmma3;
struct Weight *conv4c1_wb;
struct Weight *conv4c2_wb;
void generateBbox(const struct pBox *score, const struct pBox *location, mydataFmt scale);
void generateBbox(const struct pBox *score, const struct pBox *location, mydataFmt scale);
};
class Rnet {
public:
Rnet();
Rnet();
~Rnet();
~Rnet();
float Rthreshold;
float Rthreshold;
void run(Mat &image);
void run(Mat &image);
struct pBox *score_;
struct pBox *location_;
struct pBox *score_;
struct pBox *location_;
private:
struct pBox *rgb;
struct pBox *rgb;
struct pBox *conv1_out;
struct pBox *pooling1_out;
@@ -68,35 +68,35 @@ private:
struct pBox *conv3_out;
struct pBox *fc4_out;
struct pBox *fc4_out;
//Weight
struct Weight *conv1_wb;
struct pRelu *prelu_gmma1;
struct Weight *conv2_wb;
struct pRelu *prelu_gmma2;
struct Weight *conv3_wb;
struct pRelu *prelu_gmma3;
struct Weight *fc4_wb;
struct pRelu *prelu_gmma4;
struct Weight *score_wb;
struct Weight *location_wb;
//Weight
struct Weight *conv1_wb;
struct pRelu *prelu_gmma1;
struct Weight *conv2_wb;
struct pRelu *prelu_gmma2;
struct Weight *conv3_wb;
struct pRelu *prelu_gmma3;
struct Weight *fc4_wb;
struct pRelu *prelu_gmma4;
struct Weight *score_wb;
struct Weight *location_wb;
void RnetImage2MatrixInit(struct pBox *pbox);
void RnetImage2MatrixInit(struct pBox *pbox);
};
class Onet {
public:
Onet();
Onet();
~Onet();
~Onet();
void run(Mat &image);
void run(Mat &image);
float Othreshold;
struct pBox *score_;
struct pBox *location_;
struct pBox *keyPoint_;
float Othreshold;
struct pBox *score_;
struct pBox *location_;
struct pBox *keyPoint_;
private:
struct pBox *rgb;
struct pBox *conv1_out;
@@ -110,47 +110,47 @@ private:
struct pBox *conv4_out;
struct pBox *fc5_out;
struct pBox *fc5_out;
//Weight
struct Weight *conv1_wb;
struct pRelu *prelu_gmma1;
struct Weight *conv2_wb;
struct pRelu *prelu_gmma2;
struct Weight *conv3_wb;
struct pRelu *prelu_gmma3;
struct Weight *conv4_wb;
struct pRelu *prelu_gmma4;
struct Weight *fc5_wb;
struct pRelu *prelu_gmma5;
struct Weight *score_wb;
struct Weight *location_wb;
struct Weight *keyPoint_wb;
//Weight
struct Weight *conv1_wb;
struct pRelu *prelu_gmma1;
struct Weight *conv2_wb;
struct pRelu *prelu_gmma2;
struct Weight *conv3_wb;
struct pRelu *prelu_gmma3;
struct Weight *conv4_wb;
struct pRelu *prelu_gmma4;
struct Weight *fc5_wb;
struct pRelu *prelu_gmma5;
struct Weight *score_wb;
struct Weight *location_wb;
struct Weight *keyPoint_wb;
void OnetImage2MatrixInit(struct pBox *pbox);
void OnetImage2MatrixInit(struct pBox *pbox);
};
class mtcnn {
public:
mtcnn(int row, int col);
mtcnn(int row, int col);
~mtcnn();
~mtcnn();
void findFace(Mat &image);
void findFace(Mat &image, vector<Rect> &vecRect, vector<Point> &vecPoint);
private:
Mat reImage;
Mat reImage;
mydataFmt nms_threshold[3];
vector<mydataFmt> scales_;
Pnet *simpleFace_;
vector<struct Bbox> firstBbox_;
vector<struct orderScore> firstOrderScore_;
Rnet refineNet;
vector<struct Bbox> secondBbox_;
vector<struct orderScore> secondBboxScore_;
Onet outNet;
vector<struct Bbox> thirdBbox_;
vector<struct orderScore> thirdBboxScore_;
vector<mydataFmt> scales_;
Pnet *simpleFace_;
vector<struct Bbox> firstBbox_;
vector<struct orderScore> firstOrderScore_;
Rnet refineNet;
vector<struct Bbox> secondBbox_;
vector<struct orderScore> secondBboxScore_;
Onet outNet;
vector<struct Bbox> thirdBbox_;
vector<struct orderScore> thirdBboxScore_;
};
#endif

View File

@@ -3,25 +3,100 @@
#include "facenet.h"
#include <time.h>
void load_emb_csv(int num, vector<vector<mydataFmt>> &vecVec) {
for (int i = 0; i < num; ++i) {
ifstream inFile("../emb_csv/" + to_string(i) + ".csv", ios::in);
string lineStr;
// vector<vector<mydataFmt>> strArray;
vector<mydataFmt> lineArray;
while (getline(inFile, lineStr)) {
// 打印整行字符串
// cout << lineStr << endl;
// 存成二维表结构
stringstream ss(lineStr);
string str;
// vector<mydataFmt> lineArray;
// 按照逗号分隔
// mydataFmt nnn = 0;
while (getline(ss, str, ',')) {
lineArray.push_back(atof(str.c_str()));
// cout << str << endl;
}
// strArray.push_back(lineArray);
}
vecVec.push_back(lineArray);
}
}
float compare(vector<mydataFmt> &lineArray0, vector<mydataFmt> &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 run_mtcnn(Mat &image, vector<Rect> &vecRect) {
vector<Point> vecPoint;
mtcnn find(image.rows, image.cols);
find.findFace(image, vecRect, vecPoint);
for (int i = 0; i < vecRect.size(); ++i) {
rectangle(image, vecPoint[7 * i + 0], vecPoint[7 * i + 1], Scalar(0, 0, 255), 2, 8, 0);
for (int num = 0; num < 5; num++)
circle(image, vecPoint[7 * i + num + 2], 2, Scalar(0, 255, 255),
-1);
}
}
void run_facenet(Mat &image, vector<Rect> &vecRect, int csv_num) {
for (int i = 0; i < vecRect.size(); ++i) {
Mat fourthImage;
resize(image(vecRect[i]), fourthImage, Size(299, 299), 0, 0, cv::INTER_LINEAR);
facenet ggg;
// mydataFmt *o = new mydataFmt[Num];
vector<mydataFmt> n;
vector<vector<mydataFmt>> o;
ggg.run(fourthImage, n, i);
load_emb_csv(csv_num, o);
for (int j = 0; j < o.size(); ++j) {
float result = compare(n, o[j]);
cout << result << endl;
if (result < 0.85)
cout << "it's me" << endl;
else
cout << "unknow" << endl;
}
}
}
void run() {
int b = 0;
if (b == 0) {
Mat image = imread("../40.jpg");
// Mat image = imread("../40.jpg");
// Mat image = imread("../1.jpeg");
// Mat image = imread("../Kong_Weiye.jpg");
// Mat image = imread("../Kong_Weiye1.jpg");
Mat image = imread("../Kong_Weiye1.jpg");
// Mat image = imread("../20.png");
// Mat image = imread("../emb_img/0.jpg");
mtcnn find(image.rows, image.cols);
clock_t start;
start = clock();
find.findFace(image);
vector<Rect> vecRect;
run_mtcnn(image, vecRect);
run_facenet(image, vecRect, 5);
imshow("result", image);
imwrite("../result.jpg", image);
start = clock() - start;
// cout<<"time is "<<start/10e3<<endl;
cout << "time is " << (double) start / CLOCKS_PER_SEC * 1000 << "ms" << endl;
waitKey(0);
waitKey(5000);
image.release();
} else if (b == 1) {
Mat image = imread("../10.jpg");
@@ -31,7 +106,7 @@ void run() {
resize(image, Image, Size(160, 160), 0, 0, cv::INTER_LINEAR);
facenet ggg;
mydataFmt *o = new mydataFmt[Num];
ggg.run(Image, o, 0);
// ggg.run(Image, o, 0);
// imshow("result", Image);
imwrite("../result.jpg", Image);
@@ -58,7 +133,9 @@ void run() {
while (true) {
start = clock();
cap >> image;
find.findFace(image);
vector<Rect> vecRect;
vector<Point> vecPoint;
find.findFace(image, vecRect, vecPoint);
imshow("result", image);
if (waitKey(1) >= 0) break;
start = clock() - start;