#include #include #include "opencv2/highgui/highgui.hpp" #include #include "opencv2/core/core.hpp" #include "IDCardRecogResult.h" #include "IIDCardRecognition.h" #define DATA_LOADNET 1 #define PATH_LOADNET 0 && !DATA_LOADNET #define EVA_ONE_IMG 1 #define EVA_MORE_IMG 0 && !EVA_ONE_IMG #if _DEBUG #pragma comment(lib,"IDCardRecognitionLibsd.lib") #else #pragma comment(lib,"IDCardRecognitionLibs.lib") #endif using namespace cv; using namespace std; using namespace IDCardRecognitionLibs; void ReadFileContent(const std::string& filePath, const void*& detData, size_t& detLen); int main() { void* hIDCardRecognition = CreateIDCardRecognition(); int* idCard = static_cast(hIDCardRecognition); std::locale::global(std::locale("zh_CN.UTF-8")); std::string imgpath = "./data/test4.png"; cv::Mat srcimg = imread(imgpath); int width = srcimg.cols; int height = srcimg.rows; IDCardRecogResult result; // 加载模型 #if PATH_LOADNET string netDir = "./ValidModels/"; LoadNetWorksDir(idCard, netDir.c_str()); #elif DATA_LOADNET std::string filePath = "./ValidModels/ch_PP-OCRv4_det_infer.onnx"; const void* detData = nullptr; size_t detLen = 0; ReadFileContent(filePath, detData, detLen); std::string recPath = "./ValidModels/ch_PP-OCRv4_rec_infer.onnx"; const void* recData = nullptr; size_t recLen = 0; ReadFileContent(recPath, recData, recLen); std::string recKeyPath = "./ValidModels/ppocr_keys_v1.txt"; const void* recKeyData = nullptr; size_t recKeyLen = 0; ReadFileContent(recKeyPath, recKeyData, recKeyLen); std::string clsPath = "./ValidModels/ch_ppocr_mobile_v2.0_cls_train.onnx"; const void* clsData = nullptr; size_t clsLen = 0; ReadFileContent(clsPath, clsData, clsLen); ModelDataInfor modeDataInfor; modeDataInfor.detData = detData; modeDataInfor.detLen = detLen; modeDataInfor.recData = recData; modeDataInfor.recLen = recLen; modeDataInfor.recKeysData = recKeyData; modeDataInfor.recKeysLen = recKeyLen; modeDataInfor.clsData = clsData; modeDataInfor.clsLen = clsLen; bool loadNet = LoadNetWorks(idCard, modeDataInfor); #endif ImageInfo imgInfo; imgInfo.Width = width; imgInfo.Height = height; imgInfo.ColorType = Rgb; imgInfo.DataBuffer = srcimg.data; // 推理图像 #if EVA_MORE_IMG std::string imgpath = "E:/GIT/Branches/Judy/daily/IDCardRecognition/testImg/"; cv::Mat srcimgRead = imread(imgpath + img_19.png); // 循环推理单张图 可以一直调用 EvaluateOneImage for (int i = 0; i < 10; ++i) { bool success = EvaluateOneImageStruct(idCard, imgInfo, result); if (success) { std::cout << "Iteration " << i + 1 << ": Evaluation successful" << std::endl; } else { std::cout << "Iteration " << i + 1 << ": Evaluation failed" << std::endl; } } ReleaseIDCardRecognition(idCard); #elif EVA_ONE_IMG bool success = EvaluateOneImageStruct(idCard, imgInfo, result); std::cout << "Evaluation successful!" << std::endl; std::cout << "NumerStatus:" << result.Status << std::endl; std::cout << "Name:" << result.Name << std::endl; std::cout << "Gender:" << result.Gender << std::endl; std::cout << "Ntion:" << result.Nation << std::endl; std::cout << "IDNum:" << result.IdNumber << std::endl; std::cout << "Birthdate:" << result.Birthdate << std::endl; std::cout << "Address:" << result.Address << std::endl; #endif return 0; } // 读取模型byte[] void ReadFileContent(const std::string& filePath, const void*& detData, size_t& detLen) { // 创建文件输入流 std::ifstream file(filePath, std::ios::binary); // 检查文件是否成功打开 if (!file.is_open()) { std::cerr << "Error opening file!" << std::endl; return; } // 获取文件大小 file.seekg(0, std::ios::end); detLen = file.tellg(); file.seekg(0, std::ios::beg); // 分配足够的内存以容纳文件内容 char* data = new char[detLen]; detData = data; // 从文件读取内容到内存中 file.read(data, detLen); // 关闭文件 file.close(); }