Improve ALPR_OCR peformance
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <chrono>
|
||||
|
||||
namespace ANSCENTER {
|
||||
namespace onnxocr {
|
||||
@@ -12,6 +13,12 @@ ONNXOCRClassifier::ONNXOCRClassifier(const std::string& onnx_path, unsigned int
|
||||
: BasicOrtHandler(onnx_path, num_threads) {
|
||||
}
|
||||
|
||||
ONNXOCRClassifier::ONNXOCRClassifier(const std::string& onnx_path,
|
||||
const OrtHandlerOptions& options,
|
||||
unsigned int num_threads)
|
||||
: BasicOrtHandler(onnx_path, options, num_threads) {
|
||||
}
|
||||
|
||||
Ort::Value ONNXOCRClassifier::transform(const cv::Mat& mat) {
|
||||
cv::Mat resized;
|
||||
// Direct resize to 80x160 (PP-LCNet_x1_0_textline_ori)
|
||||
@@ -103,5 +110,38 @@ void ONNXOCRClassifier::Classify(std::vector<cv::Mat>& img_list,
|
||||
}
|
||||
}
|
||||
|
||||
void ONNXOCRClassifier::Warmup() {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (_warmedUp || !ort_session) return;
|
||||
|
||||
try {
|
||||
cv::Mat dummy(kClsImageH * 2, kClsImageW * 2, CV_8UC3, cv::Scalar(128, 128, 128));
|
||||
cv::Mat resized;
|
||||
cv::resize(dummy, resized, cv::Size(kClsImageW, kClsImageH));
|
||||
resized.convertTo(resized, CV_32FC3);
|
||||
auto inputData = NormalizeAndPermute(resized);
|
||||
|
||||
std::array<int64_t, 4> inputShape = { 1, 3, kClsImageH, kClsImageW };
|
||||
Ort::Value inputTensor = Ort::Value::CreateTensor<float>(
|
||||
*memory_info_handler, inputData.data(), inputData.size(),
|
||||
inputShape.data(), inputShape.size());
|
||||
|
||||
auto t0 = std::chrono::high_resolution_clock::now();
|
||||
(void)ort_session->Run(
|
||||
Ort::RunOptions{ nullptr },
|
||||
input_node_names.data(), &inputTensor, 1,
|
||||
output_node_names.data(), num_outputs);
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
double ms = std::chrono::duration<double, std::milli>(t1 - t0).count();
|
||||
std::cout << "[ONNXOCRClassifier] Warmup [1,3,"
|
||||
<< kClsImageH << "," << kClsImageW << "] "
|
||||
<< ms << " ms" << std::endl;
|
||||
}
|
||||
catch (const Ort::Exception& e) {
|
||||
std::cerr << "[ONNXOCRClassifier] Warmup failed: " << e.what() << std::endl;
|
||||
}
|
||||
_warmedUp = true;
|
||||
}
|
||||
|
||||
} // namespace onnxocr
|
||||
} // namespace ANSCENTER
|
||||
|
||||
Reference in New Issue
Block a user