test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <iostream>
  2. #include <vector>
  3. #include "opencv2/highgui/highgui.hpp"
  4. #include <opencv2/opencv.hpp>
  5. #include "opencv2/core/core.hpp"
  6. #include "IDCardRecogResult.h"
  7. #include "IIDCardRecognition.h"
  8. #define DATA_LOADNET 1
  9. #define PATH_LOADNET 0 && !DATA_LOADNET
  10. #define EVA_ONE_IMG 1
  11. #define EVA_MORE_IMG 0 && !EVA_ONE_IMG
  12. #if _DEBUG
  13. #pragma comment(lib,"IDCardRecognitionLibsd.lib")
  14. #else
  15. #pragma comment(lib,"IDCardRecognitionLibs.lib")
  16. #endif
  17. using namespace cv;
  18. using namespace std;
  19. using namespace IDCardRecognitionLibs;
  20. void ReadFileContent(const std::string& filePath, const void*& detData, size_t& detLen);
  21. int main()
  22. {
  23. void* hIDCardRecognition = CreateIDCardRecognition();
  24. int* idCard = static_cast<int*>(hIDCardRecognition);
  25. std::locale::global(std::locale("zh_CN.UTF-8"));
  26. std::string imgpath = "./data/test4.png";
  27. cv::Mat srcimg = imread(imgpath);
  28. int width = srcimg.cols;
  29. int height = srcimg.rows;
  30. IDCardRecogResult result;
  31. // 加载模型
  32. #if PATH_LOADNET
  33. string netDir = "./ValidModels/";
  34. LoadNetWorksDir(idCard, netDir.c_str());
  35. #elif DATA_LOADNET
  36. std::string filePath = "./ValidModels/ch_PP-OCRv4_det_infer.onnx";
  37. const void* detData = nullptr;
  38. size_t detLen = 0;
  39. ReadFileContent(filePath, detData, detLen);
  40. std::string recPath = "./ValidModels/ch_PP-OCRv4_rec_infer.onnx";
  41. const void* recData = nullptr;
  42. size_t recLen = 0;
  43. ReadFileContent(recPath, recData, recLen);
  44. std::string recKeyPath = "./ValidModels/ppocr_keys_v1.txt";
  45. const void* recKeyData = nullptr;
  46. size_t recKeyLen = 0;
  47. ReadFileContent(recKeyPath, recKeyData, recKeyLen);
  48. std::string clsPath = "./ValidModels/ch_ppocr_mobile_v2.0_cls_train.onnx";
  49. const void* clsData = nullptr;
  50. size_t clsLen = 0;
  51. ReadFileContent(clsPath, clsData, clsLen);
  52. ModelDataInfor modeDataInfor;
  53. modeDataInfor.detData = detData;
  54. modeDataInfor.detLen = detLen;
  55. modeDataInfor.recData = recData;
  56. modeDataInfor.recLen = recLen;
  57. modeDataInfor.recKeysData = recKeyData;
  58. modeDataInfor.recKeysLen = recKeyLen;
  59. modeDataInfor.clsData = clsData;
  60. modeDataInfor.clsLen = clsLen;
  61. bool loadNet = LoadNetWorks(idCard, modeDataInfor);
  62. #endif
  63. ImageInfo imgInfo;
  64. imgInfo.Width = width;
  65. imgInfo.Height = height;
  66. imgInfo.ColorType = Rgb;
  67. imgInfo.DataBuffer = srcimg.data;
  68. // 推理图像
  69. #if EVA_MORE_IMG
  70. std::string imgpath = "E:/GIT/Branches/Judy/daily/IDCardRecognition/testImg/";
  71. cv::Mat srcimgRead = imread(imgpath + img_19.png);
  72. // 循环推理单张图 可以一直调用 EvaluateOneImage
  73. for (int i = 0; i < 10; ++i) {
  74. bool success = EvaluateOneImageStruct(idCard, imgInfo, result);
  75. if (success) {
  76. std::cout << "Iteration " << i + 1 << ": Evaluation successful" << std::endl;
  77. }
  78. else {
  79. std::cout << "Iteration " << i + 1 << ": Evaluation failed" << std::endl;
  80. }
  81. }
  82. ReleaseIDCardRecognition(idCard);
  83. #elif EVA_ONE_IMG
  84. bool success = EvaluateOneImageStruct(idCard, imgInfo, result);
  85. std::cout << "Evaluation successful!" << std::endl;
  86. std::cout << "NumerStatus:" << result.Status << std::endl;
  87. std::cout << "Name:" << result.Name << std::endl;
  88. std::cout << "Gender:" << result.Gender << std::endl;
  89. std::cout << "Ntion:" << result.Nation << std::endl;
  90. std::cout << "IDNum:" << result.IdNumber << std::endl;
  91. std::cout << "Birthdate:" << result.Birthdate << std::endl;
  92. std::cout << "Address:" << result.Address << std::endl;
  93. #endif
  94. return 0;
  95. }
  96. // 读取模型byte[]
  97. void ReadFileContent(const std::string& filePath, const void*& detData, size_t& detLen) {
  98. // 创建文件输入流
  99. std::ifstream file(filePath, std::ios::binary);
  100. // 检查文件是否成功打开
  101. if (!file.is_open()) {
  102. std::cerr << "Error opening file!" << std::endl;
  103. return;
  104. }
  105. // 获取文件大小
  106. file.seekg(0, std::ios::end);
  107. detLen = file.tellg();
  108. file.seekg(0, std::ios::beg);
  109. // 分配足够的内存以容纳文件内容
  110. char* data = new char[detLen];
  111. detData = data;
  112. // 从文件读取内容到内存中
  113. file.read(data, detLen);
  114. // 关闭文件
  115. file.close();
  116. }