#include #include #include "opencv2/highgui/highgui.hpp" #include #include "opencv2/core/core.hpp" #include "IDCardRecogAux.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 #define ONNX 1 #define NCNN !ONNX #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); const char* GetIdInforRecongStatusString(IdInforRecongStatus status); int main() { void* hIDCardRecognition = CreateIDCardRecognition(); int* idCard = static_cast(hIDCardRecognition); std::locale::global(std::locale("zh_CN.UTF-8")); std::string imgpath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\test4.png"; cv::Mat srcimg = cv::imread(imgpath); int width = srcimg.cols; int height = srcimg.rows; IDCardRecogResult result; // 加载模型 #if PATH_LOADNET string netDir = "./Networks/onnx/"; LoadNetWorksDir(idCard, netDir.c_str()); #elif DATA_LOADNET #if ONNX std::string decPath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ch_PP-OCRv4_det_infer.onnx"; const void* detData = nullptr; size_t detLen = 0; ReadFileContent(decPath, detData, detLen); std::string recPath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ch_PP-OCRv4_rec_infer.onnx"; const void* recData = nullptr; size_t recLen = 0; ReadFileContent(recPath, recData, recLen); std::string recKeyPath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ppocr_keys_v1.txt"; const void* recKeyData = nullptr; size_t recKeyLen = 0; ReadFileContent(recKeyPath, recKeyData, recKeyLen); std::string clsPath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ch_ppocr_mobile_v2.0_cls_train.onnx"; const void* clsData = nullptr; size_t clsLen = 0; ReadFileContent(clsPath, clsData, clsLen); ModelDataArray modeBuffer; modeBuffer.modelCount = 4; modeBuffer.modelDatas = new ModelData[4]; modeBuffer.modelDatas[0].data = detData; modeBuffer.modelDatas[0].size = detLen; modeBuffer.modelDatas[0].identifier = "dectModelOnnx"; modeBuffer.modelDatas[1].data = recData; modeBuffer.modelDatas[1].size = recLen; modeBuffer.modelDatas[1].identifier = "recModelOnnx"; modeBuffer.modelDatas[2].data = recKeyData; modeBuffer.modelDatas[2].size = recKeyLen; modeBuffer.modelDatas[2].identifier = "keyTxt"; modeBuffer.modelDatas[3].data = clsData; modeBuffer.modelDatas[3].size = clsLen; modeBuffer.modelDatas[3].identifier = "clsModelOnnx"; LoadNetWorks(idCard, modeBuffer); //ModelDatasOnnx modeBuffer; //modeBuffer.detData = detData; //modeBuffer.detLen = detLen; //modeBuffer.recData = recData; //modeBuffer.recLen = recLen; //modeBuffer.recKeysData = recKeyData; //modeBuffer.recKeysLen = recKeyLen; //modeBuffer.clsData = clsData; //modeBuffer.clsLen = clsLen; //LoadNetWorksOnnx(idCard, modeBuffer); #else std::string filePath = ""; filePath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ch_PP-OCRv4_det_infer.bin"; const void* detBin = nullptr; size_t detBinLen = 0; ReadFileContent(filePath, detBin, detBinLen); filePath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ch_PP-OCRv4_det_infer.param"; const void* detParam = nullptr; size_t detParamLen = 0; ReadFileContent(filePath, detParam, detParamLen); filePath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ch_PP-OCRv3_rec.bin"; const void* recBin = nullptr; size_t recBinLen = 0; ReadFileContent(filePath, recBin, recBinLen); filePath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ch_PP-OCRv3_rec.param"; const void* recParam = nullptr; size_t recParamLen = 0; ReadFileContent(filePath, recParam, recParamLen); filePath = "H:\\Git\\Fork\\Lilith\\Vital\\IDCardRecogntion\\SourceCode\\Bin\\x64\\Release\\Networks\\ppocr_keys_v1.txt"; const void* keyTxt = nullptr; size_t keyTxtLen = 0; ReadFileContent(filePath, keyTxt, keyTxtLen); std::vector supportedIdentifiers = { "dectModelOnnx", "recModelOnnx", "clsModelOnnx", "decBinNcnn", "decParamNcnn", "recBinNcnn", "recParamNcnn", "keyTxt" }; ModelDataArray modeBuffer; modeBuffer.modelCount = 5; modeBuffer.modelDatas = new ModelData[5]; modeBuffer.modelDatas[0].data = detBin; modeBuffer.modelDatas[0].size = detBinLen; modeBuffer.modelDatas[0].identifier = "decBinNcnn"; modeBuffer.modelDatas[1].data = detParam; modeBuffer.modelDatas[1].size = detParamLen; modeBuffer.modelDatas[1].identifier = "decParamNcnn"; modeBuffer.modelDatas[2].data = recBin; modeBuffer.modelDatas[2].size = recBinLen; modeBuffer.modelDatas[2].identifier = "recBinNcnn"; modeBuffer.modelDatas[3].data = recParam; modeBuffer.modelDatas[3].size = recParamLen; modeBuffer.modelDatas[3].identifier = "recParamNcnn"; modeBuffer.modelDatas[4].data = keyTxt; modeBuffer.modelDatas[4].size = keyTxtLen; modeBuffer.modelDatas[4].identifier = "keyTxt"; LoadNetWorks(idCard, modeBuffer); //ModelDatasNcnn modeBuffer; //modeBuffer.detBinData = detBin; //modeBuffer.detBinLen = detBinLen; //modeBuffer.detParamData = detParam; //modeBuffer.detParamLen = detParamLen; //modeBuffer.recBinData = recBin; //modeBuffer.recBinLen = recBinLen; //modeBuffer.recParamData = recParam; //modeBuffer.recParamLen = recParamLen; //modeBuffer.recKeysData = keyTxt; //modeBuffer.recKeysLen = keyTxtLen; //LoadNetWorksNcnn(idCard, modeDataInforIn); #endif #endif ImageInfo imgInfo; imgInfo.Width = width; imgInfo.Height = height; imgInfo.ColorType = Rgb; imgInfo.DataBuffer = srcimg.data; // 推理图像 #if EVA_MORE_IMG // 循环推理单张图 可以一直调用 EvaluateOneImage //for (int i = 0; i < 10; ++i) { // bool success = EvaluateOneImage(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); if (success) { std::cout << "Evaluation successful!" << std::endl; std::cout << "NumerStatus:" << GetIdInforRecongStatusString(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; std::cout << "TimeSpan:" << result.TimeSpan << std::endl; } else { std::cerr << "Evaluation failed" << std::endl; } #endif return 0; } 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(); } // 将枚举值映射到字符串的函数 const char* GetIdInforRecongStatusString(IdInforRecongStatus status) { switch (status) { case IDINFOR_SUCCESSED_All: return "IDINFOR_SUCCESSED_All"; case IDINFOR_SUCCESSED_PART: return "IDINFOR_SUCCESSED_PART"; case IDINFOR_SUCCESSED_ID: return "IDINFOR_SUCCESSED_ID"; case IDINFOR_FAILED: return "IDINFOR_FAILED"; default: return "Unknown Status"; } }