Files

82 lines
3.6 KiB
C++
Raw Permalink Normal View History

2026-03-28 16:54:11 +11:00
#pragma once
#include "ONNXOCRTypes.h"
#include "ONNXEngine.h"
#include <vector>
#include <mutex>
2026-05-01 07:53:18 +10:00
#include <atomic>
#include <chrono>
2026-03-28 16:54:11 +11:00
namespace ANSCENTER {
namespace onnxocr {
class ONNXOCRDetector : public BasicOrtHandler {
public:
explicit ONNXOCRDetector(const std::string& onnx_path, unsigned int num_threads = 1);
2026-04-14 20:30:21 +10:00
explicit ONNXOCRDetector(const std::string& onnx_path,
const OrtHandlerOptions& options,
unsigned int num_threads = 1);
2026-03-28 16:54:11 +11:00
~ONNXOCRDetector() override = default;
// Run text detection on an image
std::vector<TextBox> Detect(const cv::Mat& srcImage,
int maxSideLen = kDetMaxSideLen,
float dbThresh = kDetDbThresh,
float boxThresh = kDetBoxThresh,
float unclipRatio = kDetUnclipRatio,
bool useDilation = false);
2026-04-14 20:30:21 +10:00
// Pre-warm cuDNN/TRT at a canonical 320x320 input so the first real
// call doesn't pay the algorithm-selection tax. Idempotent.
void Warmup();
2026-03-28 16:54:11 +11:00
private:
2026-04-14 20:30:21 +10:00
bool _warmedUp = false;
2026-03-28 16:54:11 +11:00
Ort::Value transform(const cv::Mat& mat) override;
Ort::Value transformBatch(const std::vector<cv::Mat>& images) override;
// Preprocessing
std::vector<float> Preprocess(const cv::Mat& srcImage, int maxSideLen,
int& resizeH, int& resizeW,
float& ratioH, float& ratioW);
// Postprocessing: threshold -> contours -> boxes (matches PaddleOCR official flow)
std::vector<TextBox> Postprocess(const float* outputData, int outH, int outW,
float ratioH, float ratioW, int srcH, int srcW,
float dbThresh, float boxThresh, float unclipRatio,
bool useDilation);
// Get ordered 4 corners [TL, TR, BR, BL] from rotated rect (matches PaddleOCR GetMiniBoxes)
std::array<cv::Point2f, 4> GetMiniBoxes(const cv::RotatedRect& rect);
// Compute mean score inside box polygon on the probability map
float BoxScoreFast(const cv::Mat& probMap, const std::array<cv::Point2f, 4>& box);
// Expand 4-point box using Clipper offset
std::vector<cv::Point2f> UnclipPolygon(const std::array<cv::Point2f, 4>& box, float unclipRatio);
2026-05-01 07:53:18 +10:00
// AMD/DirectML self-heal helper — see ONNXOCRRecognizer for full
// contract. On Run() failure: recreate session on the same EP first
// (transient hiccup recovery), CPU fallback as last resort. Resets
// _warmedUp so the new session gets re-warmed on next Detect call.
void HandleRunFailureAndRecreate(const char* context, const std::exception& e);
// AMD/DirectML latency-based self-heal (Path B) — see ONNXOCRRecognizer
// for the full rationale. Detector is observed to hit the same
// slow-degradation pattern as the recognizer after a sibling DML
// session takes a fatal: Run() doesn't throw, just gets slow.
// 2 s threshold catches degraded individual Run() (healthy is 50–500 ms);
// 60 s cooldown prevents thrash on permanently broken driver state.
static constexpr double kSlowRecreateThresholdMs = 2000.0;
static constexpr int64_t kRecreateCooldownMs = 60000;
std::atomic<bool> _slowRecreatePending{false};
std::chrono::steady_clock::time_point _lastRecreateAt{};
void RecordRunLatency(const char* context, double ms);
void ApplyPendingSlowRecreate(const char* context);
2026-03-28 16:54:11 +11:00
std::mutex _mutex;
};
} // namespace onnxocr
} // namespace ANSCENTER