File: perf_wechat_qrcode_pipeline.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 (160 lines) | stat: -rw-r--r-- 5,740 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

#include "perf_precomp.hpp"
namespace opencv_test
{
namespace
{
std::string qrcode_model_path[] = {"", "dnn/wechat_2021-01"};

std::string qrcode_images_name[] = {
    "version_1_top.jpg",
    "version_2_left.jpg", "version_2_up.jpg", "version_2_top.jpg",
    "version_3_down.jpg", "version_3_top.jpg",
    "version_4_top.jpg",
    "version_5_down.jpg", "version_5_left.jpg", "version_5_up.jpg", "version_5_top.jpg",
    "russian.jpg", "kanji.jpg", "link_wiki_cv.jpg"};

// NB: exclude "2_qrcodes.png" as this image appears too difficult, so that this test fails on it
std::string qrcode_images_multiple[] = {/*"2_qrcodes.png",*/ "3_qrcodes.png", "3_close_qrcodes.png",
                                        "4_qrcodes.png", "5_qrcodes.png", "7_qrcodes.png"};

WeChatQRCode createQRDetectorWithDNN(std::string& model_path)
{
    string path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel;
    if (!model_path.empty())
    {
        path_detect_prototxt = findDataFile(model_path + "/detect.prototxt", false);
        path_detect_caffemodel = findDataFile(model_path + "/detect.caffemodel", false);
        path_sr_prototxt = findDataFile(model_path + "/sr.prototxt", false);
        path_sr_caffemodel = findDataFile(model_path + "/sr.caffemodel", false);
    }
    return WeChatQRCode(path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel);
}

typedef ::perf::TestBaseWithParam< tuple< std::string,std::string > > Perf_Objdetect_QRCode;

PERF_TEST_P_(Perf_Objdetect_QRCode, detect_and_decode)
{
    std::string model_path = get<0>(GetParam());
    std::string name_current_image = get<1>(GetParam());
    const std::string root = "cv/qrcode/";

    std::string image_path = findDataFile(root + name_current_image);
    Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
    ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;

    std::vector< Mat > corners;
    std::vector< String > decoded_info;
    auto detector = createQRDetectorWithDNN(model_path);
    // warmup
    if (!model_path.empty())
    {
        decoded_info = detector.detectAndDecode(src, corners);
    }
    TEST_CYCLE()
    {
        decoded_info = detector.detectAndDecode(src, corners);
        ASSERT_FALSE(decoded_info[0].empty());
    }
    SANITY_CHECK_NOTHING();
}

typedef ::perf::TestBaseWithParam< tuple< std::string,std::string > > Perf_Objdetect_QRCode_Multi;

PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detect_and_decode)
{
    std::string model_path = get<0>(GetParam());
    std::string name_current_image = get<1>(GetParam());
    const std::string root = "cv/qrcode/multiple/";

    std::string image_path = findDataFile(root + name_current_image);
    Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
    ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;

    std::vector< Mat > corners;
    std::vector< String > decoded_info;
    auto detector = createQRDetectorWithDNN(model_path);
    // warmup
    if (!model_path.empty())
    {
        decoded_info = detector.detectAndDecode(src, corners);
    }
    TEST_CYCLE()
    {
        decoded_info = detector.detectAndDecode(src, corners);
        ASSERT_TRUE(decoded_info.size());
    }
    for(size_t i = 0; i < decoded_info.size(); i++)
    {
        ASSERT_FALSE(decoded_info[i].empty());
    }
    SANITY_CHECK_NOTHING();
}

typedef ::perf::TestBaseWithParam< tuple<std::string, std::string, Size> >Perf_Objdetect_Not_QRCode;

PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect_and_decode)
{
    std::string model_path = get<0>(GetParam());
    std::string type_gen = get<1>(GetParam());
    Size resolution = get<2>(GetParam());
    Mat not_qr_code(resolution, CV_8UC1, Scalar(0));
    if (type_gen == "random")
    {
        RNG rng;
        rng.fill(not_qr_code, RNG::UNIFORM, Scalar(0), Scalar(1));
    }
    else if (type_gen == "chessboard")
    {
        uint8_t next_pixel = 255;
        for (int j = 0; j < not_qr_code.cols; j++)
        {
            not_qr_code.ptr<uchar>(0)[j] = next_pixel;
            next_pixel = 255 - next_pixel;
        }
        for (int r = not_qr_code.cols; r < not_qr_code.rows * not_qr_code.cols; r++)
        {
            int i = r / not_qr_code.cols;
            int j = r % not_qr_code.cols;
            not_qr_code.ptr<uchar>(i)[j] = 255 - not_qr_code.ptr<uchar>(i-1)[j];
        }
    }
    std::vector< Mat > corners;
    std::vector< String > decoded_info;
    auto detector = createQRDetectorWithDNN(model_path);
    // warmup
    if (!model_path.empty())
    {
        decoded_info = detector.detectAndDecode(not_qr_code, corners);
    }
    TEST_CYCLE()
    {
        decoded_info = detector.detectAndDecode(not_qr_code, corners);
        ASSERT_FALSE(decoded_info.size());
    }
    SANITY_CHECK_NOTHING();
}


INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode,
      ::testing::Combine(
            ::testing::ValuesIn(qrcode_model_path),
            ::testing::ValuesIn(qrcode_images_name)
      ));
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode_Multi,
      ::testing::Combine(
            ::testing::ValuesIn(qrcode_model_path),
            ::testing::ValuesIn(qrcode_images_multiple)
      ));
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_Not_QRCode,
      ::testing::Combine(
            ::testing::ValuesIn(qrcode_model_path),
            ::testing::Values("zero", "random", "chessboard"),
            ::testing::Values(Size(640, 480),   Size(1280, 720))
      ));

} // namespace
} // namespace