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

face_detection.cpp 3.8 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  1. #include "face_detection.hpp"
  2. void face_detection(const cv::Mat& wildImage, std::vector<BoundingBox>& bdBoxes)
  3. {
  4. int min_scale = 0;
  5. int max_scale = 0;
  6. int factor_count = 0;
  7. float factor = 0.7937;
  8. float delim = 0;
  9. std::vector<float> scalings;
  10. max_scale = MAX(wildImage.cols, wildImage.rows);
  11. min_scale = MIN(wildImage.cols, wildImage.rows);
  12. delim = MIN(2500.0 / max_scale, 5);
  13. while (delim > 1 + 1e-4)
  14. {
  15. scalings.push_back(delim);
  16. delim *= factor;
  17. }
  18. while (min_scale >= 227)
  19. {
  20. scalings.push_back(pow(factor, factor_count));
  21. min_scale *= factor;
  22. }
  23. std::vector<BoundingBox> TmpBoundingBox;
  24. std::vector<cv::Mat> channels;
  25. cv::split(wildImage, channels);
  26. for (const auto& scaling: scalings)
  27. {
  28. const int scaledCols = wildImage.cols * scaling;
  29. const int scaledRows = wildImage.rows * scaling;
  30. modify_prototxt(scaledRows, scaledCols);
  31. cv::Mat redChannel, greenChannel, blueChannel;
  32. cv::resize(channels[0], blueChannel,
  33. cv::Size(scaledCols, scaledRows));
  34. cv::resize(channels[1], redChannel,
  35. cv::Size(scaledCols, scaledRows));
  36. cv::resize(channels[2], greenChannel,
  37. cv::Size(scaledCols, scaledRows));
  38. std::vector<Eigen::MatrixXf> rgbImage;
  39. rgbImage.emplace_back(OpenCV2Eigen(blueChannel));
  40. rgbImage.emplace_back(OpenCV2Eigen(greenChannel));
  41. rgbImage.emplace_back(OpenCV2Eigen(redChannel));
  42. rgbImage[0].array() -= red_channel_mean;
  43. rgbImage[1].array() -= green_channel_mean;
  44. rgbImage[2].array() -= blue_channel_mean;
  45. std::shared_ptr<caffe::Net<float>> net(new caffe::Net<float>(project_root + "face_full_conv2.prototxt", caffe::Phase::TEST));
  46. net->CopyTrainedLayersFrom(project_root + "face_full_conv.caffemodel");
  47. std::vector<std::vector<Eigen::MatrixXf>> rgbImages{std::move(rgbImage)};
  48. Eigen2Blob(rgbImages, net);
  49. net->ForwardPrefilled();
  50. caffe::Blob<float>* output_layer = net->output_blobs()[0];
  51. float* predict = const_cast<float*>(output_layer->cpu_data() + output_layer->shape(2) * output_layer->shape(3));
  52. Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> prob(predict, output_layer->shape(2), output_layer->shape(3));
  53. add_to_boundingbox(prob, scaling, TmpBoundingBox);
  54. }
  55. std::vector<BoundingBox> nms_averaged_bd;
  56. nms_average(TmpBoundingBox, nms_averaged_bd, 0.2);
  57. bdBoxes.clear();
  58. nms_max(nms_averaged_bd, bdBoxes, 0.3);
  59. }
  60. void modify_prototxt(const int rows, const int cols)
  61. {
  62. std::ifstream protoIn(project_root + "face_full_conv.prototxt", std::ios::in);
  63. std::ofstream protoOut(project_root + "face_full_conv2.prototxt", std::ios::out);
  64. auto index = 0;
  65. for (std::string line; std::getline(protoIn, line); index++)
  66. {
  67. if (index == 5)
  68. {
  69. protoOut << "input_dim: " << rows << '\n';
  70. }
  71. else if (index == 6)
  72. {
  73. protoOut << "input_dim: " << cols << '\n';
  74. }
  75. else
  76. {
  77. protoOut << line << '\n';
  78. }
  79. }
  80. protoOut.close();
  81. protoIn.close();
  82. }
  83. void add_to_boundingbox(const Eigen::MatrixXf& prob, const float scale, std::vector<BoundingBox>& boundingBox)
  84. {
  85. const int stride = 32;
  86. const int cell_size = 227;
  87. for (int h = 0; h < prob.rows(); ++h)
  88. {
  89. for (int w = 0; w < prob.cols(); ++w)
  90. {
  91. if (prob(h, w) >= 0.85)
  92. {
  93. boundingBox.emplace_back(BoundingBox(float(w * stride) / scale, float(h * stride) / scale, float(cell_size) / scale, float(cell_size) / scale, prob(h, w)));
  94. }
  95. }
  96. }
  97. }
Tip!

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

Comments

Loading...