-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathball_pre_classifier_upper_cam.cpp
78 lines (59 loc) · 2.6 KB
/
ball_pre_classifier_upper_cam.cpp
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
#include "ball_pre_classifier_upper_cam.h"
#include <easy/profiler.h>
#include <stl_ext.h>
#include <cstring>
#include <thread>
namespace htwk {
BallPreClassifierUpperCam::BallPreClassifierUpperCam(int8_t* lutCb, int8_t* lutCr,
BallFeatureExtractor* featureExtractor,
HtwkVisionConfig &config)
: BallDetector(lutCb, lutCr, config), featureExtractor(featureExtractor) {
tflite.loadModelFromFile(
config.tflitePath + "/uc-ball-small-classifier.tflite",
{config.hypothesisGeneratorMaxHypothesisCount + config.ucBallHypGeneratorConfig.hypothesisCount,
config.ballDetectorPatchSize, config.ballDetectorPatchSize, 1},
config.ballPreClassifierUpperCamThreads);
}
/*
* detects the ball (if visible) and outputs its position
*/
void BallPreClassifierUpperCam::proceed(const uint8_t* img, std::shared_ptr<FieldBorderDetector> fieldBorderDetector, std::vector<ObjectHypothesis>& hypoList) {
Timer t("BallPreClassifierUpperCam", 50);
EASY_FUNCTION(profiler::colors::Green);
ratedBallHypotheses.clear();
float maxBallProb = config.ballProbabilityThreshold;
EASY_BLOCK("Get Feature");
const auto& fieldBorder = fieldBorderDetector->getConvexFieldBorder();
float* curInputPoint = tflite.getInputTensor();
for (const ObjectHypothesis& hyp : hypoList) {
featureExtractor->getFeature(hyp, img, config.ballDetectorPatchSize, curInputPoint);
curInputPoint += config.ballDetectorPatchSize * config.ballDetectorPatchSize;
}
EASY_END_BLOCK;
EASY_BLOCK("TFlite Small BallDetector");
tflite.execute();
EASY_END_BLOCK;
const float* outputPosBall = tflite.getOutputTensor();
const int outputOffset = 2;
allHypothesesWithProb = hypoList;
bestBallHypothesis = std::nullopt;
for (size_t i = 0; i < hypoList.size(); i++) {
ObjectHypothesis& hypProb = allHypothesesWithProb[i];
const float ballProb = outputPosBall[1];
outputPosBall += outputOffset;
hypProb.prob = ballProb;
// Remove every hypotheses which is 10% above the field border
int x = clamp(hypProb.x, 0, width-1);
if (hypProb.y < fieldBorder[x] - (height * 0.1f)) {
hypProb.prob = 0;
}
if (ballProb > config.ballProbabilityThreshold) {
ratedBallHypotheses.push_back(hypProb);
if (ballProb > maxBallProb) {
maxBallProb = ballProb;
bestBallHypothesis = hypProb;
}
}
}
}
} // namespace htwk