Skip to content

Commit 2cab657

Browse files
author
Mariya Sinitsina
committed
fixes
1 parent e05c63a commit 2cab657

File tree

2 files changed

+86
-116
lines changed

2 files changed

+86
-116
lines changed

modules/wechat_qrcode/perf/perf_main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,20 @@
33
// of this distribution and at http://opencv.org/license.html.
44
#include "perf_precomp.hpp"
55

6+
static
7+
void initTests()
8+
{
9+
#ifdef HAVE_OPENCV_DNN
10+
const char* extraTestDataPath =
11+
#ifdef WINRT
12+
NULL;
13+
#else
14+
getenv("OPENCV_DNN_TEST_DATA_PATH");
15+
#endif
16+
if (extraTestDataPath)
17+
cvtest::addDataSearchPath(extraTestDataPath);
18+
#endif // HAVE_OPENCV_DNN
19+
}
20+
21+
CV_TEST_MAIN("cv", initTests())
622
CV_PERF_TEST_MAIN(wechat_qrcode)

modules/wechat_qrcode/perf/perf_wechat_qrcode_pipeline.cpp

Lines changed: 70 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// of this distribution and at http://opencv.org/license.html.
44

55
#include "perf_precomp.hpp"
6-
76
namespace opencv_test
87
{
98
namespace
109
{
10+
std::string qrcode_model_path[] = {"", "dnn/wechat_2021-01"};
1111

1212
std::string qrcode_images_name[] = {
1313
"version_1_top.jpg",
@@ -20,183 +20,137 @@ std::string qrcode_images_name[] = {
2020
std::string qrcode_images_multiple[] = {"2_qrcodes.png", "3_qrcodes.png", "3_close_qrcodes.png",
2121
"4_qrcodes.png", "5_qrcodes.png", "7_qrcodes.png"};
2222

23-
bool Find_Models_Files(std::vector<std::string>& models) {
24-
string path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel;
25-
string model_version = "_2021-01";
26-
path_detect_prototxt = findDataFile("dnn/wechat"+model_version+"/detect.prototxt", false);
27-
path_detect_caffemodel = findDataFile("dnn/wechat"+model_version+"/detect.caffemodel", false);
28-
path_sr_prototxt = findDataFile("dnn/wechat"+model_version+"/sr.prototxt", false);
29-
path_sr_caffemodel = findDataFile("dnn/wechat"+model_version+"/sr.caffemodel", false);
30-
models = {path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel};
31-
return true;
32-
}
33-
34-
typedef ::perf::TestBaseWithParam< std::string > Perf_Objdetect_QRCode;
35-
36-
PERF_TEST_P_(Perf_Objdetect_QRCode, detect_and_decode_without_nn)
23+
WeChatQRCode createQRDetectorWithDNN(std::string& model_path)
3724
{
38-
const std::string name_current_image = GetParam();
39-
const std::string root = "cv/qrcode/";
40-
41-
std::string image_path = findDataFile(root + name_current_image);
42-
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
43-
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
44-
45-
std::vector< Mat > corners;
46-
auto detector = wechat_qrcode::WeChatQRCode();
47-
48-
TEST_CYCLE()
49-
{
50-
auto decoded_info = detector.detectAndDecode(src, corners);
51-
ASSERT_FALSE(decoded_info[0].empty());
25+
string path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel;
26+
if (!model_path.empty())
27+
{
28+
path_detect_prototxt = findDataFile(model_path + "/detect.prototxt", false);
29+
path_detect_caffemodel = findDataFile(model_path + "/detect.caffemodel", false);
30+
path_sr_prototxt = findDataFile(model_path + "/sr.prototxt", false);
31+
path_sr_caffemodel = findDataFile(model_path + "/sr.caffemodel", false);
5232
}
53-
SANITY_CHECK_NOTHING();
33+
return WeChatQRCode(path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel);
5434
}
5535

36+
typedef ::perf::TestBaseWithParam< tuple< std::string,std::string > > Perf_Objdetect_QRCode;
37+
5638
PERF_TEST_P_(Perf_Objdetect_QRCode, detect_and_decode)
5739
{
58-
const std::string name_current_image = GetParam();
40+
std::string model_path = get<0>(GetParam());
41+
std::string name_current_image = get<1>(GetParam());
5942
const std::string root = "cv/qrcode/";
6043

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

6548
std::vector< Mat > corners;
66-
std::vector<std::string> models;
67-
ASSERT_TRUE(Find_Models_Files(models));
68-
auto detector = wechat_qrcode::WeChatQRCode(models[0], models[1], models[2], models[3]);
49+
std::vector< String > decoded_info;
50+
auto detector = createQRDetectorWithDNN(model_path);
51+
// warmup
52+
if (!model_path.empty())
53+
{
54+
decoded_info = detector.detectAndDecode(src, corners);
55+
}
6956
TEST_CYCLE()
7057
{
71-
auto decoded_info = detector.detectAndDecode(src, corners);
58+
decoded_info = detector.detectAndDecode(src, corners);
7259
ASSERT_FALSE(decoded_info[0].empty());
7360
}
7461
SANITY_CHECK_NOTHING();
7562
}
7663

77-
typedef ::perf::TestBaseWithParam< std::string > Perf_Objdetect_QRCode_Multi;
64+
typedef ::perf::TestBaseWithParam< tuple< std::string,std::string > > Perf_Objdetect_QRCode_Multi;
7865

79-
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detect_and_decode_without_nn)
66+
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detect_and_decode)
8067
{
81-
const std::string name_current_image = GetParam();
68+
std::string model_path = get<0>(GetParam());
69+
std::string name_current_image = get<1>(GetParam());
8270
const std::string root = "cv/qrcode/multiple/";
8371

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

8876
std::vector< Mat > corners;
89-
auto detector = wechat_qrcode::WeChatQRCode();
90-
91-
TEST_CYCLE()
92-
{
93-
auto decoded_info = detector.detectAndDecode(src, corners);
94-
ASSERT_TRUE(decoded_info.size());
95-
for(size_t i = 0; i < decoded_info.size(); i++)
96-
{
97-
ASSERT_FALSE(decoded_info[i].empty());
98-
}
77+
std::vector< String > decoded_info;
78+
auto detector = createQRDetectorWithDNN(model_path);
79+
// warmup
80+
if (!model_path.empty())
81+
{
82+
decoded_info = detector.detectAndDecode(src, corners);
9983
}
100-
SANITY_CHECK_NOTHING();
101-
}
102-
103-
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detect_and_decode)
104-
{
105-
const std::string name_current_image = GetParam();
106-
const std::string root = "cv/qrcode/multiple/";
107-
108-
std::string image_path = findDataFile(root + name_current_image);
109-
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
110-
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
111-
112-
std::vector<std::string> models;
113-
ASSERT_TRUE(Find_Models_Files(models));
114-
auto detector = wechat_qrcode::WeChatQRCode(models[0], models[1], models[2], models[3]);
115-
std::vector< Mat > corners;
11684
TEST_CYCLE()
117-
{
118-
auto decoded_info = detector.detectAndDecode(src, corners);
85+
{
86+
decoded_info = detector.detectAndDecode(src, corners);
11987
ASSERT_TRUE(decoded_info.size());
120-
for(size_t i = 0; i < decoded_info.size(); i++)
121-
{
122-
ASSERT_FALSE(decoded_info[i].empty());
123-
}
88+
}
89+
for(size_t i = 0; i < decoded_info.size(); i++)
90+
{
91+
ASSERT_FALSE(decoded_info[i].empty());
12492
}
12593
SANITY_CHECK_NOTHING();
12694
}
12795

128-
typedef ::perf::TestBaseWithParam< tuple< std::string, Size > > Perf_Objdetect_Not_QRCode;
96+
typedef ::perf::TestBaseWithParam< tuple<std::string, std::string, Size> >Perf_Objdetect_Not_QRCode;
12997

130-
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect_and_decode_without_nn)
98+
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect_and_decode)
13199
{
132-
std::string type_gen = get<0>(GetParam());
133-
Size resolution = get<1>(GetParam());
100+
std::string model_path = get<0>(GetParam());
101+
std::string type_gen = get<1>(GetParam());
102+
Size resolution = get<2>(GetParam());
134103
Mat not_qr_code(resolution, CV_8UC1, Scalar(0));
135104
if (type_gen == "random")
136105
{
137106
RNG rng;
138107
rng.fill(not_qr_code, RNG::UNIFORM, Scalar(0), Scalar(1));
139108
}
140-
if (type_gen == "chessboard")
109+
else if (type_gen == "chessboard")
141110
{
142-
uint8_t next_pixel = 0;
143-
for (int r = 0; r < not_qr_code.rows * not_qr_code.cols; r++)
111+
uint8_t next_pixel = 255;
112+
for (int j = 0; j < not_qr_code.cols; j++)
144113
{
145-
int i = r / not_qr_code.cols;
146-
int j = r % not_qr_code.cols;
147-
not_qr_code.ptr<uchar>(i)[j] = next_pixel;
114+
not_qr_code.ptr<uchar>(0)[j] = next_pixel;
148115
next_pixel = 255 - next_pixel;
149116
}
150-
}
151-
std::vector< Mat > corners;
152-
auto detector = wechat_qrcode::WeChatQRCode();
153-
154-
TEST_CYCLE()
155-
{
156-
auto decoded_info = detector.detectAndDecode(not_qr_code, corners);
157-
ASSERT_FALSE(decoded_info.size());
158-
}
159-
SANITY_CHECK_NOTHING();
160-
}
161-
162-
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect_and_decode)
163-
{
164-
std::string type_gen = get<0>(GetParam());
165-
Size resolution = get<1>(GetParam());
166-
Mat not_qr_code(resolution, CV_8UC1, Scalar(0));
167-
if (type_gen == "random")
168-
{
169-
RNG rng;
170-
rng.fill(not_qr_code, RNG::UNIFORM, Scalar(0), Scalar(1));
171-
}
172-
if (type_gen == "chessboard")
173-
{
174-
uint8_t next_pixel = 0;
175-
for (int r = 0; r < not_qr_code.rows * not_qr_code.cols; r++)
117+
for (int r = not_qr_code.cols; r < not_qr_code.rows * not_qr_code.cols; r++)
176118
{
177119
int i = r / not_qr_code.cols;
178120
int j = r % not_qr_code.cols;
179-
not_qr_code.ptr<uchar>(i)[j] = next_pixel;
180-
next_pixel = 255 - next_pixel;
121+
not_qr_code.ptr<uchar>(i)[j] = 255 - not_qr_code.ptr<uchar>(i-1)[j];
181122
}
182123
}
183124
std::vector< Mat > corners;
184-
std::vector<std::string> models;
185-
ASSERT_TRUE(Find_Models_Files(models));
186-
auto detector = wechat_qrcode::WeChatQRCode(models[0], models[1], models[2], models[3]);
187-
188-
TEST_CYCLE()
125+
std::vector< String > decoded_info;
126+
auto detector = createQRDetectorWithDNN(model_path);
127+
// warmup
128+
if (!model_path.empty())
189129
{
190-
auto decoded_info = detector.detectAndDecode(not_qr_code, corners);
130+
decoded_info = detector.detectAndDecode(not_qr_code, corners);
131+
}
132+
TEST_CYCLE()
133+
{
134+
decoded_info = detector.detectAndDecode(not_qr_code, corners);
191135
ASSERT_FALSE(decoded_info.size());
192136
}
193137
SANITY_CHECK_NOTHING();
194138
}
195139

196-
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
197-
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode_Multi, testing::ValuesIn(qrcode_images_multiple));
140+
141+
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode,
142+
::testing::Combine(
143+
::testing::ValuesIn(qrcode_model_path),
144+
::testing::ValuesIn(qrcode_images_name)
145+
));
146+
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode_Multi,
147+
::testing::Combine(
148+
::testing::ValuesIn(qrcode_model_path),
149+
::testing::ValuesIn(qrcode_images_multiple)
150+
));
198151
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_Not_QRCode,
199152
::testing::Combine(
153+
::testing::ValuesIn(qrcode_model_path),
200154
::testing::Values("zero", "random", "chessboard"),
201155
::testing::Values(Size(640, 480), Size(1280, 720))
202156
));

0 commit comments

Comments
 (0)