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

tool_box.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
  1. #include "tool_box.hpp"
  2. std::vector<float> SetMean(const std::string& mean_file, int num_channels_)
  3. {
  4. caffe::BlobProto blob_proto;
  5. caffe::ReadProtoFromBinaryFileOrDie(mean_file, &blob_proto);
  6. /* Convert from BlobProto to Blob<float> */
  7. caffe::Blob<float> mean_blob;
  8. mean_blob.FromProto(blob_proto);
  9. /* The format of the mean file is planar 32-bit float BGR or grayscale. */
  10. std::vector<cv::Mat> channels;
  11. float* data = mean_blob.mutable_cpu_data();
  12. for (int i = 0; i < num_channels_; ++i) {
  13. /* Extract an individual channel. */
  14. cv::Mat channel(mean_blob.height(), mean_blob.width(), CV_32FC1, data);
  15. channels.push_back(channel);
  16. data += mean_blob.height() * mean_blob.width();
  17. }
  18. return std::vector<float>{channels[0].at<float>(0, 0),
  19. channels[1].at<float>(0, 0),
  20. channels[2].at<float>(0, 0)};
  21. }
  22. BoundingBox extend_face_to_whole_head(const BoundingBox& face,
  23. const int imageHeight, const int imageWidth)
  24. {
  25. float extend = MIN(MIN(face.getX(), face.getY()), MIN(imageWidth - 1 - face.getX() - face.getWidth(), imageHeight - 1 - face.getY() - face.getHeight()));
  26. extend = MIN(extend, face.getHeight() / 2);
  27. return BoundingBox(face.getX() - extend, face.getY() - extend,
  28. face.getWidth() + 2 * extend,
  29. face.getHeight() + 2 * extend, face.getProb());
  30. }
  31. std::vector<int> Argmax(const std::vector<float>& v, int N)
  32. {
  33. auto PairCompare = [](const std::pair<float, int>& lhs,
  34. const std::pair<float, int>& rhs)
  35. {
  36. return lhs.first > rhs.first;
  37. };
  38. std::vector<std::pair<float, int>> pairs;
  39. for (size_t i = 0; i < v.size(); ++i)
  40. pairs.push_back(std::make_pair(v[i], i));
  41. std::partial_sort(pairs.begin(), pairs.begin() + N, pairs.end(), PairCompare);
  42. std::vector<int> result;
  43. for (int i = 0; i < N; ++i)
  44. result.push_back(pairs[i].second);
  45. return result;
  46. }
Tip!

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

Comments

Loading...