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_recognization.cpp 1.6 kB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Created by Jack Yu on 23/10/2017.
  3. //
  4. #include "../include/CNNRecognizer.h"
  5. 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"};
  6. #include <opencv2/dnn.hpp>
  7. using namespace cv::dnn;
  8. void getMaxClass(cv::Mat &probBlob, int *classId, double *classProb)
  9. {
  10. // cv::Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix
  11. cv::Point classNumber;
  12. cv::minMaxLoc(probBlob, NULL, classProb, NULL, &classNumber);
  13. *classId = classNumber.x;
  14. }
  15. void TEST_RECOGNIZATION(){
  16. // pr::CNNRecognizer instance("model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel");
  17. Net net = cv::dnn::readNetFromCaffe("model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel");
  18. cv::Mat image = cv::imread("res/char1.png",cv::IMREAD_GRAYSCALE);
  19. cv::resize(image,image,cv::Size(14,30));
  20. cv::equalizeHist(image,image);
  21. cv::Mat inputBlob = cv::dnn::blobFromImage(image, 1/255.0, cv::Size(14,30), false);
  22. net.setInput(inputBlob,"data");
  23. cv::Mat res = net.forward();
  24. std::cout<<res<<std::endl;
  25. float *p = (float*)res.data;
  26. int maxid= 0;
  27. double prob = 0;
  28. getMaxClass(res,&maxid,&prob);
  29. std::cout<<chars[maxid]<<std::endl;
  30. };
  31. int main()
  32. {TEST_RECOGNIZATION();
  33. }