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

gender_estimation.cpp 2.0 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
59
60
  1. #include "gender_estimation.hpp"
  2. std::vector<Gender> gender_estimation(const cv::Mat& Image, const std::vector<BoundingBox>& face_rois)
  3. {
  4. std::vector<Gender> genders;
  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_gender2.prototxt", caffe::Phase::TEST));
  7. net->CopyTrainedLayersFrom(project_root + "gender_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. double predictMan = *(predicts);
  29. ++predicts;
  30. double predictWoman = *(predicts);
  31. if (predictWoman > predictMan)
  32. {
  33. genders.push_back(Gender::Woman);
  34. }
  35. else if (predictMan > predictWoman)
  36. {
  37. genders.push_back(Gender::Man);
  38. }
  39. else
  40. {
  41. genders.push_back(Gender::Error);
  42. }
  43. }
  44. return std::move(genders);
  45. }
Tip!

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

Comments

Loading...