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.

main.cpp 4.4 kB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Created by Coleflowers on 20/06/2018.
  3. //
  4. #include <phpcpp.h>
  5. #include <iostream>
  6. #include "PlateSegmentation.h"
  7. #include "CNNRecognizer.h"
  8. #include "Recognizer.h"
  9. #include "PlateDetection.h"
  10. #include "FastDeskew.h"
  11. #include "FineMapping.h"
  12. std::vector<std::string> chars{"京","沪","津","渝","冀","晋","蒙","辽","吉","黑","苏","浙","皖","闽","赣","鲁","豫","鄂","湘","粤","桂","琼","川","贵","云","藏","陕","甘","青","宁","新","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"};
  13. /**
  14. * 车牌图片识别
  15. * @param imgpath 图片的绝对路径
  16. * @return 车牌号 图片路径的图片不存在的话 返回空值
  17. */
  18. cv::String scan(const char *imgpath){
  19. // 读取图片获取车牌的粗略部分
  20. cv::Mat image = cv::imread(imgpath);
  21. if(!image.data) {
  22. printf("No img data!\n");
  23. return "";
  24. }
  25. pr::PlateDetection plateDetection("/usr/platescan/src/lpr/model/cascade.xml");
  26. std::vector<pr::PlateInfo> plates;
  27. plateDetection.plateDetectionRough(image,plates);
  28. // todo 处理发现的每一个车牌信息
  29. // 目前只处理了一组
  30. cv::Mat img;
  31. for(pr::PlateInfo platex:plates) {
  32. // 暂时不保存了
  33. //cv::imwrite("res/mmm.png",platex.getPlateImage());
  34. img = platex.getPlateImage();
  35. }
  36. // 车牌部分
  37. cv::Mat image_finemapping = pr::FineMapping::FineMappingVertical(img);
  38. pr::FineMapping finemapper = pr::FineMapping("/usr/platescan/src/lpr/model/HorizonalFinemapping.prototxt","/usr/platescan/src/lpr/model/HorizonalFinemapping.caffemodel");
  39. //
  40. image_finemapping = pr::fastdeskew(image_finemapping, 5);
  41. // image_finemapping = finemapper.FineMappingHorizon(image_finemapping,0,-3);
  42. // Android 代码里这个是
  43. // 改了之后部分图片数据精度发生变化 ,比如 字母精度有了,但是汉字又错了
  44. image_finemapping = finemapper.FineMappingHorizon(image_finemapping,2,5);
  45. // 暂时不保存了
  46. //cv::imwrite("res/m222222222.png",image_finemapping);
  47. // 设置尺寸 识别
  48. cv::Mat demo = image_finemapping;
  49. cv::resize(demo,demo,cv::Size(136,36));
  50. //cv::imwrite("res/m333333.png",demo);
  51. cv::Mat respones;
  52. pr::PlateSegmentation plateSegmentation("/usr/platescan/src/lpr/model/Segmentation.prototxt","/usr/platescan/src/lpr/model/Segmentation.caffemodel");
  53. pr::PlateInfo plate;
  54. plate.setPlateImage(demo);
  55. std::vector<cv::Rect> rects;
  56. plateSegmentation.segmentPlatePipline(plate,1,rects);
  57. plateSegmentation.ExtractRegions(plate,rects);
  58. pr::GeneralRecognizer *recognizer = new pr::CNNRecognizer("/usr/platescan/src/lpr/model/CharacterRecognization.prototxt","/usr/platescan/src/lpr/model/CharacterRecognization.caffemodel");
  59. recognizer->SegmentBasedSequenceRecognition(plate);
  60. //std::cout<<plate.decodePlateNormal(chars)<<std::endl;
  61. plate.decodePlateNormal(chars);
  62. cv::String txt = plate.getPlateName();
  63. // printf("res:%s\n", txt.c_str());
  64. //
  65. // 写入结果到文件
  66. // FILE *fp;
  67. // fp = fopen(resSaveFile, "wb+");
  68. // fprintf(fp, "%s", txt.c_str());
  69. // fclose(fp);
  70. //
  71. delete(recognizer);
  72. return txt;
  73. }
  74. Php::Value myFunction(Php::Parameters &params) {
  75. std::string str = params[0];
  76. const char *str_c = str.c_str();
  77. cv::String res = scan(str_c);
  78. return res.c_str();
  79. }
  80. /**
  81. * tell the compiler that the get_module is a pure C function
  82. */
  83. extern "C" {
  84. /**
  85. * Function that is called by PHP right after the PHP process
  86. * has started, and that returns an address of an internal PHP
  87. * strucure with all the details and features of your extension
  88. *
  89. * @return void* a pointer to an address that is understood by PHP
  90. */
  91. PHPCPP_EXPORT void *get_module() {
  92. // static(!) Php::Extension object that should stay in memory
  93. // for the entire duration of the process (that's why it's static)
  94. static Php::Extension extension("platescan", "1.0");
  95. // @todo add your own functions, classes, namespaces to the extension
  96. extension.add<myFunction>("platescan", {Php::ByRef("string", Php::Type::String)});
  97. // return the extension
  98. return extension;
  99. }
  100. }