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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include "mediapipe/framework/calculator_framework.h" #include "mediapipe/framework/formats/detection.pb.h" #include "tensorflow/lite/interpreter.h"
namespace mediapipe {
class BlazeFacePostprocessorCalculator : public CalculatorBase { public: static absl::Status GetContract(CalculatorContract* cc); absl::Status Open(CalculatorContext* cc) override; absl::Status Process(CalculatorContext* cc) override;
private: std::vector<std::pair<float, float>> GenerateAnchors(); std::vector<float> DecodeBox( const float* box_data, const std::pair<float, float>& anchor); std::vector<std::pair<float, float>> DecodeKeypoints( const float* keypoint_data, const std::pair<float, float>& anchor, const std::vector<float>& box); std::vector<int> NonMaxSuppression( const std::vector<std::vector<float>>& boxes, const std::vector<float>& scores, float nms_threshold); float score_threshold_ = 0.5f; float min_suppression_threshold_ = 0.3f; int num_keypoints_ = 6; int num_anchors_ = 896; std::vector<std::pair<float, float>> anchors_; };
absl::Status BlazeFacePostprocessorCalculator::Open(CalculatorContext* cc) { const auto& options = cc->Options<BlazeFaceOptions>(); score_threshold_ = options.score_threshold(); min_suppression_threshold_ = options.min_suppression_threshold(); num_keypoints_ = options.num_keypoints(); anchors_ = GenerateAnchors(); return absl::OkStatus(); }
absl::Status BlazeFacePostprocessorCalculator::Process(CalculatorContext* cc) { if (cc->Inputs().Tag("TENSORS").IsEmpty()) { return absl::OkStatus(); } const auto& tensors = cc->Inputs().Tag("TENSORS").Get<std::vector<TfLiteTensor>>(); const float* box_data = tensors[0].data.f; const float* score_data = tensors[1].data.f; const float* keypoint_data = tensors[2].data.f; std::vector<std::vector<float>> all_boxes; std::vector<float> all_scores; for (int i = 0; i < num_anchors_; ++i) { float score = score_data[i]; if (score < score_threshold_) { continue; } auto box = DecodeBox(box_data + i * 16, anchors_[i]); all_boxes.push_back(box); all_scores.push_back(score); } auto keep_indices = NonMaxSuppression( all_boxes, all_scores, min_suppression_threshold_); auto detections = absl::make_unique<std::vector<Detection>>(); for (int idx : keep_indices) { Detection detection; auto* bbox = detection.mutable_location_data()->mutable_relative_bounding_box(); bbox->set_xmin(all_boxes[idx][0]); bbox->set_ymin(all_boxes[idx][1]); bbox->set_width(all_boxes[idx][2] - all_boxes[idx][0]); bbox->set_height(all_boxes[idx][3] - all_boxes[idx][1]); detection.set_score(all_scores[idx]); detection.set_label_id(0); auto keypoints = DecodeKeypoints( keypoint_data + idx * num_keypoints_ * 2, anchors_[idx], all_boxes[idx]); for (const auto& kp : keypoints) { auto* keypoint = detection.mutable_location_data()->add_relative_keypoints(); keypoint->set_x(kp.first); keypoint->set_y(kp.second); } detections->push_back(detection); } cc->Outputs().Tag("DETECTIONS").Add(detections.release(), cc->InputTimestamp()); return absl::OkStatus(); }
std::vector<float> BlazeFacePostprocessorCalculator::DecodeBox( const float* box_data, const std::pair<float, float>& anchor) { float x_center = box_data[0] / 128.0f + anchor.first; float y_center = box_data[1] / 128.0f + anchor.second; float width = box_data[2]; float height = box_data[3]; float xmin = x_center - width / 2.0f; float ymin = y_center - height / 2.0f; float xmax = x_center + width / 2.0f; float ymax = y_center + height / 2.0f; return {xmin, ymin, xmax, ymax}; }
std::vector<int> BlazeFacePostprocessorCalculator::NonMaxSuppression( const std::vector<std::vector<float>>& boxes, const std::vector<float>& scores, float nms_threshold) { std::vector<int> indices; std::vector<bool> suppressed(boxes.size(), false); std::vector<int> order(scores.size()); std::iota(order.begin(), order.end(), 0); std::sort(order.begin(), order.end(), [&scores](int a, int b) { return scores[a] > scores[b]; }); for (int i : order) { if (suppressed[i]) continue; indices.push_back(i); for (int j : order) { if (suppressed[j]) continue; float iou = CalculateIoU(boxes[i], boxes[j]); if (iou > nms_threshold) { suppressed[j] = true; } } } return indices; }
REGISTER_CALCULATOR(BlazeFacePostprocessorCalculator);
}
|