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.5 kB

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