You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.4 KiB
C++
82 lines
2.4 KiB
C++
// EasyOCR-CPP.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
|
|
//
|
|
#pragma warning(disable : 4996)
|
|
#include "ATW.h"
|
|
#include <iostream>
|
|
#include "EasyOCR_Detector.h"
|
|
#include "EasyOCR_Recognizer.h"
|
|
|
|
int main()
|
|
{
|
|
const std::wstring detModelPath = L".\\DetectionModel.onnx";
|
|
const std::wstring recModelPath = L".\\RecognitionModel-EN.onnx";
|
|
const std::wstring recModelWithChnPath = L".\\RecognitionModel_EN+CH_SIM.onnx";
|
|
const std::string imagePath = "..\\..\\vin.jpg";
|
|
|
|
//Init Config
|
|
uns::G_OCRConfig.SetGPUUsage(uns::OCRConfig::GPUUsage::CPUOnly);
|
|
uns::G_OCRConfig.SetDetectModelPath(detModelPath);
|
|
uns::G_OCRConfig.SetRecognizeModelPath(recModelPath);
|
|
uns::G_OCRConfig.SetLanguage(uns::OCRConfig::CharsetType::EN);
|
|
|
|
//Prepare
|
|
uns::EasyOCR_Detector detector;
|
|
uns::EasyOCR_Recognizer recognizer;
|
|
|
|
//Init Models
|
|
if (!detector.Init())
|
|
{
|
|
std::cout << "Detector Init Failure!" << std::endl;
|
|
return -1;
|
|
}
|
|
if (!recognizer.Init())
|
|
{
|
|
std::cout << "Recognizer Init Failure!" << std::endl;
|
|
return -2;
|
|
}
|
|
|
|
//Load Image
|
|
cv::Mat image = cv::imread(imagePath);
|
|
if (image.empty())
|
|
{
|
|
std::cerr << "Failed to load image: " << imagePath << std::endl;
|
|
return -3;
|
|
}
|
|
cv::blur(image, image, { 3,3 });
|
|
|
|
//WarmUP GPU
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
std::cout << "Warming UP GPU (" << i << ") ......" << std::endl;
|
|
clock_t start = clock();
|
|
auto rects = detector(image);
|
|
auto results = recognizer(image, rects);
|
|
clock_t end = clock();
|
|
std::cout << "Warm UP [" << i << "] Cost " << static_cast<double>(end - start) / static_cast<double>(CLOCKS_PER_SEC) << " Second(s)" << std::endl;
|
|
}
|
|
std::cout << "GPU Warm UP Finished" << std::endl;
|
|
|
|
//Begin OCR
|
|
clock_t start = clock();
|
|
auto rects = detector(image);
|
|
clock_t detect_finished = clock();
|
|
auto results = recognizer(image, rects);
|
|
clock_t recognize_finished = clock();
|
|
for (const auto& [index, info] : results)
|
|
{
|
|
const auto& [text, conf] = info;
|
|
std::cout << "Box " << index << ": \"" << WtoA(text) << "\" Confidence=" << conf << "\n";
|
|
}
|
|
|
|
//Output Time Cost
|
|
double detect_time_cost = static_cast<double>(detect_finished - start) / static_cast<double>(CLOCKS_PER_SEC);
|
|
double recognize_time_cost = static_cast<double>(recognize_finished - detect_finished) / static_cast<double>(CLOCKS_PER_SEC);
|
|
printf("Detect Cost: %.4lf Second(s)\nRecognize Cost: %.4lf Second(s)\n", detect_time_cost, recognize_time_cost);
|
|
|
|
//Cleanup
|
|
detector.UnInit();
|
|
recognizer.UnInit();
|
|
|
|
//Exit
|
|
return 0;
|
|
} |