tuple<Mat, vector<vector<int>>> getFaceBox(Net net, Mat& frame, double conf_threshold)
{
Mat frameOpenCVDNN = frame.clone();
int frameHeight = frameOpenCVDNN.rows;
int frameWidth = frameOpenCVDNN.cols;
double inScaleFactor = 1.0;
Size size = Size(300, 300);
// std::vector<int> meanVal = {104, 117, 123};
Scalar meanVal = Scalar(104, 117, 123);
cv::Mat inputBlob;
inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, size, meanVal, true, false);
net.setInput(inputBlob, "data");
cv::Mat detection = net.forward("detection_out");
cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
vector<vector<int>> bboxes;
for (int i = 0; i < detectionMat.rows; i++) {
float confidence = detectionMat.at<float>(i, 2);
if (confidence > conf_threshold) {
int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frameWidth);
int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frameHeight);
int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frameWidth);
int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frameHeight);
vector<int> box = { x1, y1, x2, y2 };
bboxes.push_back(box);
cv::rectangle(frameOpenCVDNN, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 0, 255));
}
}
return make_tuple(frameOpenCVDNN, bboxes);
}
int main()
{
clock_t startTime, endTime;
cv::Mat frame;
int padding = 20;
Scalar MODEL_MEAN_VALUES = Scalar(78.4263377603, 87.7689143744, 114.895847746);
vector<string> ageList = { "(0-2)","(4-6)","(8-12)","(15-20)","(25-32)","(38-43)","(48-53)","(60-100)" };
vector<string> genderList = { "Male","Female" };
std::string netRootPath = "D://";
std::string imagepath = "D://sunsuyuan//project//data//doublefusion//results//FittingSmpl//color.png";
//frame = cv::imread(netRootPath + "sample3.jpg", 1);
frame = cv::imread(netRootPath+"3.png", 1);
// frame = cv::imread(imagepath, 1);
cv::cvtColor(frame, frame, CV_BGRA2BGR);
if (frame.empty()) {
fprintf(stderr, "cv::imread %s failed\n", netRootPath + "sample3.jpg");
return -1;
}
// Load Network
Net ageNet = readNet(netRootPath+"age_net.caffemodel", netRootPath+"age_deploy.prototxt");
Net genderNet = readNet(netRootPath+"gender_net.caffemodel", netRootPath+"gender_deploy.prototxt");
Net faceNet = readNet(netRootPath+"opencv_face_detector_uint8.pb", netRootPath+"opencv_face_detector.pbtxt");
vector<vector<int>> bboxes;
Mat frameFace;
tie(frameFace, bboxes) = getFaceBox(faceNet, frame, 0.7);
if (bboxes.size() == 0) {
cout << "No face detected, checking next frame." << endl;
}
else {
startTime = clock();
for (auto it = begin(bboxes); it != end(bboxes); ++it) {
Rect rec(it->at(0) - padding, it->at(1) - padding, it->at(2) - it->at(0) + 2 * padding, it->at(3) - it->at(1) + 2 * padding);
Mat face = frame(rec); // take the ROI of box on the frame
Mat blob = blobFromImage(face, 1, Size(227, 227), MODEL_MEAN_VALUES, false);
genderNet.setInput(blob);
vector<float> genderPreds = genderNet.forward();
// find max element index (distance function does the argmax() work in C++)
int max_index_gender = std::distance(genderPreds.begin(), max_element(genderPreds.begin(), genderPreds.end()));
string gender = genderList[max_index_gender];
ageNet.setInput(blob);
vector<float> agePreds = ageNet.forward();
// finding maximum indicd in the age_preds vector
int max_indice_age = std::distance(agePreds.begin(), max_element(agePreds.begin(), agePreds.end()));
string age = ageList[max_indice_age];
cout << "Gender: " << gender << " Age: " << age << endl;
string label = gender + ", " + age; // label
cv::putText(frameFace, label, Point(it->at(0), it->at(1) - 15), cv::FONT_HERSHEY_SIMPLEX, 0.7, Scalar(0, 255, 255));
}
endTime = clock();//计时结束
std::cout << "The run time is: " << (double)1000*(endTime - startTime) / CLOCKS_PER_SEC << "ms" <<std::endl;
imwrite(netRootPath+"out1.jpg", frameFace);
//imshow("out",frameFace);
cv::waitKey(0);
}
}
c++实现人脸、性别及年龄检测
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. 创建应用,获取API Key和API Secret 首先进入习悦开发者平台,注册或登录成功后,点击创建应用,...
- 虹软产品地址:http://ai.arcsoft.com.cn/product/arcface.html虹软Arc...
- 今天青石的票圈出镜率最高的,莫过于张艺谋的新片终于定档了。 一张满溢着水墨风的海报一次次的出现在票圈里,也就是老谋...