add auto binarythres fix logic 2
This commit is contained in:
@@ -675,14 +675,39 @@ void ArmorYoloDetectorNode::performBinaryThresCalibration(
|
|||||||
FYT_INFO("armor_yolo_detect", "Calibration searching: thres={}, no detection yet", calib_current_thres_);
|
FYT_INFO("armor_yolo_detect", "Calibration searching: thres={}, no detection yet", calib_current_thres_);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
// Detection succeeded! Switch to binary search mode
|
// Detection succeeded! Switch to stabilization phase
|
||||||
calib_searching_ = false;
|
calib_searching_ = false;
|
||||||
calib_frame_count_ = 0;
|
calib_frame_count_ = 1; // Start counting from 1
|
||||||
FYT_INFO("armor_yolo_detect", "Calibration: detection found at thres={}, starting binary search", calib_current_thres_);
|
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_;
|
int low = 30, high = calib_current_thres_, best_thres = calib_current_thres_;
|
||||||
double best_error = std::numeric_limits<double>::max();
|
double best_error = std::numeric_limits<double>::max();
|
||||||
constexpr int max_iterations = 5;
|
constexpr int max_iterations = 5;
|
||||||
@@ -715,7 +740,7 @@ void ArmorYoloDetectorNode::performBinaryThresCalibration(
|
|||||||
// Calculate error (difference between traditional and YOLO area)
|
// Calculate error (difference between traditional and YOLO area)
|
||||||
double error = std::abs(trad_avg_area - yolo_avg_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);
|
iter, mid, yolo_avg_area, trad_avg_area, error);
|
||||||
|
|
||||||
if (error < best_error) {
|
if (error < best_error) {
|
||||||
@@ -737,14 +762,7 @@ void ArmorYoloDetectorNode::performBinaryThresCalibration(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update threshold to best found
|
// Calibration complete
|
||||||
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) {
|
|
||||||
calib_done_ = true;
|
calib_done_ = true;
|
||||||
detector_->binary_thres = best_thres;
|
detector_->binary_thres = best_thres;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user