File: qrcode_example_without_nn.cpp

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (67 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace std;
using namespace cv;

#include <opencv2/wechat_qrcode.hpp>

int main(int argc, char* argv[]) {
    cout << endl << argv[0] << endl << endl;
    cout << "A demo program of WeChat QRCode Detector: " << endl;

    Mat img;
    int camIdx = -1;
    if (argc > 1) {
        bool live = strcmp(argv[1], "-camera") == 0;
        if (live) {
            camIdx = argc > 2 ? atoi(argv[2]) : 0;
        } else {
            img = imread(argv[1]);
        }
    } else {
        cout << "    Usage: " << argv[0] << " <input_image>" << endl;
        return 0;
    }
    // The model is downloaded to ${CMAKE_BINARY_DIR}/downloads/wechat_qrcode if cmake runs without warnings,
    // otherwise you can download them from https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode.
    Ptr<wechat_qrcode::WeChatQRCode> detector;

    try {
        detector = makePtr<wechat_qrcode::WeChatQRCode>("", "", "", "");
    } catch (const std::exception& e) {
        cout <<
            "\n---------------------------------------------------------------\n"
            "Failed to initialize WeChatQRCode.\n"
            "---------------------------------------------------------------\n";
        cout << e.what() << endl;
        return 0;
    }
    string prevstr = "";
    vector<Mat> points;

    if (camIdx < 0) {
        auto res = detector->detectAndDecode(img, points);
        for (const auto& t : res) cout << t << endl;
    } else {
        VideoCapture cap(camIdx);
        for(;;) {
            cap >> img;
            if (img.empty())
                break;
            auto res = detector->detectAndDecode(img, points);
            for (const auto& t : res) {
                if (t != prevstr)
                    cout << t << endl;
            }
            if (!res.empty())
                prevstr = res.back();
            imshow("image", img);
            if (waitKey(30) >= 0)
                break;
        }
    }
    return 0;
}