qrcode_example.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <iostream>
  2. #include <opencv2/core.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. using namespace std;
  6. using namespace cv;
  7. #include <opencv2/wechat_qrcode.hpp>
  8. int main(int argc, char* argv[]) {
  9. cout << endl << argv[0] << endl << endl;
  10. cout << "A demo program of WeChat QRCode Detector: " << endl;
  11. Mat img;
  12. int camIdx = -1;
  13. if (argc > 1) {
  14. bool live = strcmp(argv[1], "-camera") == 0;
  15. if (live) {
  16. camIdx = argc > 2 ? atoi(argv[2]) : 0;
  17. } else {
  18. img = imread(argv[1]);
  19. }
  20. } else {
  21. cout << " Usage: " << argv[0] << " <input_image>" << endl;
  22. return 0;
  23. }
  24. // The model is downloaded to ${CMAKE_BINARY_DIR}/downloads/wechat_qrcode if cmake runs without warnings,
  25. // otherwise you can download them from https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode.
  26. Ptr<wechat_qrcode::WeChatQRCode> detector;
  27. try {
  28. detector = makePtr<wechat_qrcode::WeChatQRCode>("detect.prototxt", "detect.caffemodel",
  29. "sr.prototxt", "sr.caffemodel");
  30. } catch (const std::exception& e) {
  31. cout <<
  32. "\n---------------------------------------------------------------\n"
  33. "Failed to initialize WeChatQRCode.\n"
  34. "Please, download 'detector.*' and 'sr.*' from\n"
  35. "https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode\n"
  36. "and put them into the current directory.\n"
  37. "---------------------------------------------------------------\n";
  38. cout << e.what() << endl;
  39. return 0;
  40. }
  41. string prevstr = "";
  42. vector<Mat> points;
  43. if (camIdx < 0) {
  44. auto res = detector->detectAndDecode(img, points);
  45. for (const auto& t : res) cout << t << endl;
  46. } else {
  47. VideoCapture cap(camIdx);
  48. for(;;) {
  49. cap >> img;
  50. if (img.empty())
  51. break;
  52. auto res = detector->detectAndDecode(img, points);
  53. for (const auto& t : res) {
  54. if (t != prevstr)
  55. cout << t << endl;
  56. }
  57. if (!res.empty())
  58. prevstr = res.back();
  59. imshow("image", img);
  60. if (waitKey(30) >= 0)
  61. break;
  62. }
  63. }
  64. return 0;
  65. }