Files
ANSCORE/modules/ANSOCR/ANSONNXOCR/ONNXOCRDetector.h

82 lines
3.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include "ONNXOCRTypes.h"
#include "ONNXEngine.h"
#include <vector>
#include <mutex>
#include <atomic>
#include <chrono>
namespace ANSCENTER {
namespace onnxocr {
class ONNXOCRDetector : public BasicOrtHandler {
public:
explicit ONNXOCRDetector(const std::string& onnx_path, unsigned int num_threads = 1);
explicit ONNXOCRDetector(const std::string& onnx_path,
const OrtHandlerOptions& options,
unsigned int num_threads = 1);
~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);
// Pre-warm cuDNN/TRT at a canonical 320x320 input so the first real
// call doesn't pay the algorithm-selection tax. Idempotent.
void Warmup();
private:
bool _warmedUp = false;
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);
// 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);
std::mutex _mutex;
};
} // namespace onnxocr
} // namespace ANSCENTER