Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

age_estimation.cpp 2.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  1. #include "age_estimation.hpp"
  2. std::vector<Age> age_estimation(const cv::Mat& Image, const std::vector<BoundingBox>& face_rois)
  3. {
  4. std::vector<Age> ages;
  5. std::vector<float> mean(SetMean(project_root + "mean.binaryproto", 3));
  6. std::shared_ptr<caffe::Net<double>> net(new caffe::Net<double>(project_root + "deploy_age2.prototxt", caffe::Phase::TEST));
  7. net->CopyTrainedLayersFrom(project_root + "age_net.caffemodel");
  8. for (const auto& face_roi: face_rois)
  9. {
  10. BoundingBox headRoi(extend_face_to_whole_head(face_roi, Image.rows, Image.cols));
  11. cv::Mat head;
  12. Image(cv::Rect(headRoi.getX(), headRoi.getY(), headRoi.getWidth(), headRoi.getHeight())).copyTo(head);
  13. cv::resize(head, head, cv::Size(cellsize, cellsize));
  14. std::vector<cv::Mat> headCVChannels;
  15. cv::split(head, headCVChannels);
  16. std::vector<Eigen::MatrixXf> matChannels;
  17. matChannels.emplace_back(OpenCV2Eigen(headCVChannels[0]));
  18. matChannels.emplace_back(OpenCV2Eigen(headCVChannels[1]));
  19. matChannels.emplace_back(OpenCV2Eigen(headCVChannels[2]));
  20. matChannels[0].array() -= mean[0];
  21. matChannels[1].array() -= mean[1];
  22. matChannels[2].array() -= mean[2];
  23. std::vector<std::vector<Eigen::MatrixXf>> face_matrix{matChannels};
  24. Eigen2Blob<double>(face_matrix, net);
  25. net->ForwardPrefilled();
  26. caffe::Blob<double>* output_layer = net->output_blobs()[0];
  27. double* predicts = const_cast<double*>(output_layer->cpu_data());
  28. std::vector<float> possibilities(predicts, predicts + 8);
  29. switch(Argmax(possibilities, 1)[0])
  30. {
  31. case 0: ages.push_back(Age::R0_2); break;
  32. case 1: ages.push_back(Age::R4_6); break;
  33. case 2: ages.push_back(Age::R8_13); break;
  34. case 3: ages.push_back(Age::R15_20); break;
  35. case 4: ages.push_back(Age::R25_32); break;
  36. case 5: ages.push_back(Age::R38_43); break;
  37. case 6: ages.push_back(Age::R48_53); break;
  38. case 7: ages.push_back(Age::R60_); break;
  39. default: ages.push_back(Age::Error); break;
  40. }
  41. }
  42. return std::move(ages);
  43. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...