You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_pipeline.cpp 6.0 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Created by Jack Yu on 23/10/2017.
  3. //
  4. #include "../include/Pipeline.h"
  5. using namespace std;
  6. template<class T>
  7. static unsigned int levenshtein_distance(const T &s1, const T &s2) {
  8. const size_t len1 = s1.size(), len2 = s2.size();
  9. std::vector<unsigned int> col(len2 + 1), prevCol(len2 + 1);
  10. for (unsigned int i = 0; i < prevCol.size(); i++) prevCol[i] = i;
  11. for (unsigned int i = 0; i < len1; i++) {
  12. col[0] = i + 1;
  13. for (unsigned int j = 0; j < len2; j++)
  14. col[j + 1] = min(
  15. min(prevCol[1 + j] + 1, col[j] + 1),
  16. prevCol[j] + (s1[i] == s2[j] ? 0 : 1));
  17. col.swap(prevCol);
  18. }
  19. return prevCol[len2];
  20. }
  21. void TEST_ACC(){
  22. pr::PipelinePR prc("model/cascade.xml",
  23. "model/HorizonalFinemapping.prototxt","model/HorizonalFinemapping.caffemodel",
  24. "model/Segmentation.prototxt","model/Segmentation.caffemodel",
  25. "model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel",
  26. "model/SegmenationFree-Inception.prototxt","model/SegmenationFree-Inception.caffemodel"
  27. );
  28. ifstream file;
  29. string imagename;
  30. int n = 0,correct = 0,j = 0,sum = 0;
  31. char filename[] = "/Users/yujinke/Downloads/general_test/1.txt";
  32. string pathh = "/Users/yujinke/Downloads/general_test/";
  33. file.open(filename, ios::in);
  34. while (!file.eof())
  35. {
  36. file >> imagename;
  37. string imgpath = pathh + imagename;
  38. std::cout << "------------------------------------------------" << endl;
  39. cout << "图片名:" << imagename << endl;
  40. cv::Mat image = cv::imread(imgpath);
  41. // cv::imshow("image", image);
  42. // cv::waitKey(0);
  43. std::vector<pr::PlateInfo> res = prc.RunPiplineAsImage(image,pr::SEGMENTATION_FREE_METHOD);
  44. float conf = 0;
  45. vector<float> con ;
  46. vector<string> name;
  47. for (auto st : res) {
  48. if (st.confidence > 0.1) {
  49. //std::cout << st.getPlateName() << " " << st.confidence << std::endl;
  50. con.push_back(st.confidence);
  51. name.push_back(st.getPlateName());
  52. //conf += st.confidence;
  53. }
  54. else
  55. cout << "no string" << endl;
  56. }
  57. // std::cout << conf << std::endl;
  58. int num = con.size();
  59. float max = 0;
  60. string platestr, chpr, ch;
  61. int diff = 0,dif = 0;
  62. for (int i = 0; i < num; i++) {
  63. if (con.at(i) > max)
  64. {
  65. max = con.at(i);
  66. platestr = name.at(i);
  67. }
  68. }
  69. // cout << "max:"<<max << endl;
  70. cout << "string:" << platestr << endl;
  71. chpr = platestr.substr(0, 2);
  72. ch = imagename.substr(0, 2);
  73. diff = levenshtein_distance(imagename, platestr);
  74. dif = diff - 4;
  75. cout << "差距:" <<dif << endl;
  76. sum += dif;
  77. if (ch != chpr) n++;
  78. if (diff == 0) correct++;
  79. j++;
  80. }
  81. float cha = 1 - float(n) / float(j);
  82. std::cout << "------------------------------------------------" << endl;
  83. cout << "车牌总数:" << j << endl;
  84. cout << "汉字识别准确率:"<<cha << endl;
  85. float chaccuracy = 1 - float(sum - n * 2) /float(j * 8);
  86. cout << "字符识别准确率:" << chaccuracy << endl;
  87. }
  88. void TEST_PIPELINE(){
  89. pr::PipelinePR prc("model/cascade.xml",
  90. "model/HorizonalFinemapping.prototxt","model/HorizonalFinemapping.caffemodel",
  91. "model/Segmentation.prototxt","model/Segmentation.caffemodel",
  92. "model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel",
  93. "model/SegmenationFree-Inception.prototxt","model/SegmenationFree-Inception.caffemodel"
  94. );
  95. cv::Mat image = cv::imread("/Users/yujinke/ClionProjects/cpp_ocr_demo/test.png");
  96. std::vector<pr::PlateInfo> res = prc.RunPiplineAsImage(image,pr::SEGMENTATION_FREE_METHOD);
  97. for(auto st:res) {
  98. if(st.confidence>0.75) {
  99. std::cout << st.getPlateName() << " " << st.confidence << std::endl;
  100. cv::Rect region = st.getPlateRect();
  101. cv::rectangle(image,cv::Point(region.x,region.y),cv::Point(region.x+region.width,region.y+region.height),cv::Scalar(255,255,0),2);
  102. }
  103. }
  104. cv::imshow("image",image);
  105. cv::waitKey(0);
  106. }
  107. void TEST_CAM()
  108. {
  109. cv::VideoCapture capture("test1.mp4");
  110. cv::Mat frame;
  111. pr::PipelinePR prc("model/cascade.xml",
  112. "model/HorizonalFinemapping.prototxt","model/HorizonalFinemapping.caffemodel",
  113. "model/Segmentation.prototxt","model/Segmentation.caffemodel",
  114. "model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel",
  115. "model/SegmentationFree.prototxt","model/SegmentationFree.caffemodel"
  116. );
  117. while(1) {
  118. //读取下一帧
  119. if (!capture.read(frame)) {
  120. std::cout << "读取视频失败" << std::endl;
  121. exit(1);
  122. }
  123. //
  124. // cv::transpose(frame,frame);
  125. // cv::flip(frame,frame,2);
  126. // cv::resize(frame,frame,cv::Size(frame.cols/2,frame.rows/2));
  127. std::vector<pr::PlateInfo> res = prc.RunPiplineAsImage(frame,pr::SEGMENTATION_FREE_METHOD);
  128. for(auto st:res) {
  129. if(st.confidence>0.75) {
  130. std::cout << st.getPlateName() << " " << st.confidence << std::endl;
  131. cv::Rect region = st.getPlateRect();
  132. cv::rectangle(frame,cv::Point(region.x,region.y),cv::Point(region.x+region.width,region.y+region.height),cv::Scalar(255,255,0),2);
  133. }
  134. }
  135. cv::imshow("image",frame);
  136. cv::waitKey(1);
  137. }
  138. }
  139. int main()
  140. {
  141. TEST_ACC();
  142. // TEST_CAM();
  143. // TEST_PIPELINE();
  144. return 0 ;
  145. }