add auto binarythres fix logic 2

This commit is contained in:
cyy_mac
2026-03-26 06:59:01 +08:00
parent b826235fd7
commit f34cefe597

View File

@@ -675,14 +675,39 @@ void ArmorYoloDetectorNode::performBinaryThresCalibration(
FYT_INFO("armor_yolo_detect", "Calibration searching: thres={}, no detection yet", calib_current_thres_);
return;
} else {
// Detection succeeded! Switch to binary search mode
// Detection succeeded! Switch to stabilization phase
calib_searching_ = false;
calib_frame_count_ = 0;
FYT_INFO("armor_yolo_detect", "Calibration: detection found at thres={}, starting binary search", calib_current_thres_);
calib_frame_count_ = 1; // Start counting from 1
FYT_INFO("armor_yolo_detect", "Calibration: detection found at thres={}, waiting for stability...", calib_current_thres_);
return; // Wait for next frame to continue stability check
}
}
// Phase 2: Binary search to find optimal threshold
// Phase 2: Wait for stable detection (no frame limit)
// Check if detection still succeeds at current threshold
{
int original_thres = detector_->binary_thres;
detector_->binary_thres = calib_current_thres_;
auto armors = detector_->processROIs(img, gray_img, rois);
detector_->binary_thres = original_thres;
if (armors.empty()) {
// Detection lost, go back to searching phase
FYT_INFO("armor_yolo_detect", "Calibration: detection lost, searching again...");
calib_searching_ = true;
calib_current_thres_ = 250;
calib_frame_count_ = 0;
return;
}
}
// Detection is stable, increment counter
calib_frame_count_++;
FYT_INFO("armor_yolo_detect", "Calibration stability: {}/5 (thres={})", calib_frame_count_, calib_current_thres_);
// After stable for 5 frames, start binary search
if (calib_frame_count_ >= 5) {
// Phase 3: Binary search to find optimal threshold
int low = 30, high = calib_current_thres_, best_thres = calib_current_thres_;
double best_error = std::numeric_limits<double>::max();
constexpr int max_iterations = 5;
@@ -715,7 +740,7 @@ void ArmorYoloDetectorNode::performBinaryThresCalibration(
// Calculate error (difference between traditional and YOLO area)
double error = std::abs(trad_avg_area - yolo_avg_area);
FYT_INFO("armor_yolo_detect", "Calibration iter {}: thres={}, yolo_area={:.1f}, trad_area={:.1f}, error={:.1f}",
FYT_INFO("armor_yolo_detect", "Calibration binary search iter {}: thres={}, yolo_area={:.1f}, trad_area={:.1f}, error={:.1f}",
iter, mid, yolo_avg_area, trad_avg_area, error);
if (error < best_error) {
@@ -737,14 +762,7 @@ void ArmorYoloDetectorNode::performBinaryThresCalibration(
}
}
// Update threshold to best found
calib_current_thres_ = best_thres;
// Only count frames where we got valid detection
calib_frame_count_++;
FYT_INFO("armor_yolo_detect", "Calibration progress: {}/10 (thres={})", calib_frame_count_, best_thres);
if (calib_frame_count_ >= 10) {
// Calibration complete
calib_done_ = true;
detector_->binary_thres = best_thres;