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();//計(jì)時(shí)結(jié)束
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++實(shí)現(xiàn)人臉刊殉、性別及年齡檢測
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來笨觅,“玉大人拦耐,你說我怎么就攤上這事〖#” “怎么了杀糯?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長苍苞。 經(jīng)常有香客問我固翰,道長狼纬,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任骂际,我火速辦了婚禮疗琉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘歉铝。我一直安慰自己盈简,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布太示。 她就那樣靜靜地躺著柠贤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪类缤。 梳的紋絲不亂的頭發(fā)上臼勉,一...
- 文/蒼蘭香墨 我猛地睜開眼降允,長吁一口氣:“原來是場噩夢啊……” “哼恩闻!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起剧董,我...
- 序言:老撾萬榮一對情侶失蹤幢尚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后翅楼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體尉剩,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年毅臊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了理茎。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站胎挎,受9級(jí)特大地震影響沟启,放射性物質(zhì)發(fā)生泄漏忆家。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一德迹、第九天 我趴在偏房一處隱蔽的房頂上張望芽卿。 院中可真熱鬧,春花似錦浦辨、人聲如沸蹬竖。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至列另,卻和暖如春芽腾,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背页衙。 一陣腳步聲響...
- 正文 我出身青樓艰躺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親眨八。 傳聞我的和親對象是個(gè)殘疾皇子腺兴,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 1.0 系統(tǒng)環(huán)境 操作系統(tǒng): Centos7.4dlib:19.15tensorflow:1.8 2.0 環(huán)境安裝...
- 1. 創(chuàng)建應(yīng)用页响,獲取API Key和API Secret 首先進(jìn)入習(xí)悅開發(fā)者平臺(tái),注冊或登錄成功后,點(diǎn)擊創(chuàng)建應(yīng)用段誊,...
- 注冊Face++申請key和secret,導(dǎo)入SDK,有不足缺陷的地方還望指出索赏。 public classMain...
- 虹軟產(chǎn)品地址:http://ai.arcsoft.com.cn/product/arcface.html虹軟Arc...
- 今天青石的票圈出鏡率最高的悯衬,莫過于張藝謀的新片終于定檔了弹沽。 一張滿溢著水墨風(fēng)的海報(bào)一次次的出現(xiàn)在票圈里,也就是老謀...