1
0
mirror of https://github.com/opencv/opencv_contrib.git synced 2025-10-23 09:38:56 +08:00
This commit is contained in:
Mariya Sinitsina
2023-05-03 15:29:49 +03:00
parent e05c63acd2
commit 2cab657042
2 changed files with 90 additions and 120 deletions

View File

@@ -3,4 +3,20 @@
// of this distribution and at http://opencv.org/license.html. // of this distribution and at http://opencv.org/license.html.
#include "perf_precomp.hpp" #include "perf_precomp.hpp"
static
void initTests()
{
#ifdef HAVE_OPENCV_DNN
const char* extraTestDataPath =
#ifdef WINRT
NULL;
#else
getenv("OPENCV_DNN_TEST_DATA_PATH");
#endif
if (extraTestDataPath)
cvtest::addDataSearchPath(extraTestDataPath);
#endif // HAVE_OPENCV_DNN
}
CV_TEST_MAIN("cv", initTests())
CV_PERF_TEST_MAIN(wechat_qrcode) CV_PERF_TEST_MAIN(wechat_qrcode)

View File

@@ -3,11 +3,11 @@
// of this distribution and at http://opencv.org/license.html. // of this distribution and at http://opencv.org/license.html.
#include "perf_precomp.hpp" #include "perf_precomp.hpp"
namespace opencv_test namespace opencv_test
{ {
namespace namespace
{ {
std::string qrcode_model_path[] = {"", "dnn/wechat_2021-01"};
std::string qrcode_images_name[] = { std::string qrcode_images_name[] = {
"version_1_top.jpg", "version_1_top.jpg",
@@ -20,42 +20,25 @@ std::string qrcode_images_name[] = {
std::string qrcode_images_multiple[] = {"2_qrcodes.png", "3_qrcodes.png", "3_close_qrcodes.png", std::string qrcode_images_multiple[] = {"2_qrcodes.png", "3_qrcodes.png", "3_close_qrcodes.png",
"4_qrcodes.png", "5_qrcodes.png", "7_qrcodes.png"}; "4_qrcodes.png", "5_qrcodes.png", "7_qrcodes.png"};
bool Find_Models_Files(std::vector<std::string>& models) { WeChatQRCode createQRDetectorWithDNN(std::string& model_path)
string path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel;
string model_version = "_2021-01";
path_detect_prototxt = findDataFile("dnn/wechat"+model_version+"/detect.prototxt", false);
path_detect_caffemodel = findDataFile("dnn/wechat"+model_version+"/detect.caffemodel", false);
path_sr_prototxt = findDataFile("dnn/wechat"+model_version+"/sr.prototxt", false);
path_sr_caffemodel = findDataFile("dnn/wechat"+model_version+"/sr.caffemodel", false);
models = {path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel};
return true;
}
typedef ::perf::TestBaseWithParam< std::string > Perf_Objdetect_QRCode;
PERF_TEST_P_(Perf_Objdetect_QRCode, detect_and_decode_without_nn)
{ {
const std::string name_current_image = GetParam(); string path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel;
const std::string root = "cv/qrcode/"; if (!model_path.empty())
{
std::string image_path = findDataFile(root + name_current_image); path_detect_prototxt = findDataFile(model_path + "/detect.prototxt", false);
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode; path_detect_caffemodel = findDataFile(model_path + "/detect.caffemodel", false);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path; path_sr_prototxt = findDataFile(model_path + "/sr.prototxt", false);
path_sr_caffemodel = findDataFile(model_path + "/sr.caffemodel", false);
std::vector< Mat > corners;
auto detector = wechat_qrcode::WeChatQRCode();
TEST_CYCLE()
{
auto decoded_info = detector.detectAndDecode(src, corners);
ASSERT_FALSE(decoded_info[0].empty());
} }
SANITY_CHECK_NOTHING(); 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) PERF_TEST_P_(Perf_Objdetect_QRCode, detect_and_decode)
{ {
const std::string name_current_image = GetParam(); std::string model_path = get<0>(GetParam());
std::string name_current_image = get<1>(GetParam());
const std::string root = "cv/qrcode/"; const std::string root = "cv/qrcode/";
std::string image_path = findDataFile(root + name_current_image); std::string image_path = findDataFile(root + name_current_image);
@@ -63,140 +46,111 @@ PERF_TEST_P_(Perf_Objdetect_QRCode, detect_and_decode)
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path; ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector< Mat > corners; std::vector< Mat > corners;
std::vector<std::string> models; std::vector< String > decoded_info;
ASSERT_TRUE(Find_Models_Files(models)); auto detector = createQRDetectorWithDNN(model_path);
auto detector = wechat_qrcode::WeChatQRCode(models[0], models[1], models[2], models[3]); // warmup
if (!model_path.empty())
{
decoded_info = detector.detectAndDecode(src, corners);
}
TEST_CYCLE() TEST_CYCLE()
{ {
auto decoded_info = detector.detectAndDecode(src, corners); decoded_info = detector.detectAndDecode(src, corners);
ASSERT_FALSE(decoded_info[0].empty()); ASSERT_FALSE(decoded_info[0].empty());
} }
SANITY_CHECK_NOTHING(); SANITY_CHECK_NOTHING();
} }
typedef ::perf::TestBaseWithParam< std::string > Perf_Objdetect_QRCode_Multi; typedef ::perf::TestBaseWithParam< tuple< std::string,std::string > > Perf_Objdetect_QRCode_Multi;
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detect_and_decode_without_nn)
{
const std::string name_current_image = 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;
auto detector = wechat_qrcode::WeChatQRCode();
TEST_CYCLE()
{
auto 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();
}
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detect_and_decode) PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detect_and_decode)
{ {
const std::string name_current_image = GetParam(); std::string model_path = get<0>(GetParam());
std::string name_current_image = get<1>(GetParam());
const std::string root = "cv/qrcode/multiple/"; const std::string root = "cv/qrcode/multiple/";
std::string image_path = findDataFile(root + name_current_image); std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode; Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path; ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector<std::string> models;
ASSERT_TRUE(Find_Models_Files(models));
auto detector = wechat_qrcode::WeChatQRCode(models[0], models[1], models[2], models[3]);
std::vector< Mat > corners;
TEST_CYCLE()
{
auto 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, Size > > Perf_Objdetect_Not_QRCode;
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect_and_decode_without_nn)
{
std::string type_gen = get<0>(GetParam());
Size resolution = get<1>(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));
}
if (type_gen == "chessboard")
{
uint8_t next_pixel = 0;
for (int r = 0; 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] = next_pixel;
next_pixel = 255 - next_pixel;
}
}
std::vector< Mat > corners; std::vector< Mat > corners;
auto detector = wechat_qrcode::WeChatQRCode(); std::vector< String > decoded_info;
auto detector = createQRDetectorWithDNN(model_path);
TEST_CYCLE() // warmup
if (!model_path.empty())
{ {
auto decoded_info = detector.detectAndDecode(not_qr_code, corners); decoded_info = detector.detectAndDecode(src, corners);
ASSERT_FALSE(decoded_info.size()); }
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(); 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) PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect_and_decode)
{ {
std::string type_gen = get<0>(GetParam()); std::string model_path = get<0>(GetParam());
Size resolution = get<1>(GetParam()); std::string type_gen = get<1>(GetParam());
Size resolution = get<2>(GetParam());
Mat not_qr_code(resolution, CV_8UC1, Scalar(0)); Mat not_qr_code(resolution, CV_8UC1, Scalar(0));
if (type_gen == "random") if (type_gen == "random")
{ {
RNG rng; RNG rng;
rng.fill(not_qr_code, RNG::UNIFORM, Scalar(0), Scalar(1)); rng.fill(not_qr_code, RNG::UNIFORM, Scalar(0), Scalar(1));
} }
if (type_gen == "chessboard") else if (type_gen == "chessboard")
{ {
uint8_t next_pixel = 0; uint8_t next_pixel = 255;
for (int r = 0; r < not_qr_code.rows * not_qr_code.cols; r++) 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 i = r / not_qr_code.cols;
int j = r % not_qr_code.cols; int j = r % not_qr_code.cols;
not_qr_code.ptr<uchar>(i)[j] = next_pixel; not_qr_code.ptr<uchar>(i)[j] = 255 - not_qr_code.ptr<uchar>(i-1)[j];
next_pixel = 255 - next_pixel;
} }
} }
std::vector< Mat > corners; std::vector< Mat > corners;
std::vector<std::string> models; std::vector< String > decoded_info;
ASSERT_TRUE(Find_Models_Files(models)); auto detector = createQRDetectorWithDNN(model_path);
auto detector = wechat_qrcode::WeChatQRCode(models[0], models[1], models[2], models[3]); // warmup
if (!model_path.empty())
TEST_CYCLE()
{ {
auto decoded_info = detector.detectAndDecode(not_qr_code, corners); decoded_info = detector.detectAndDecode(not_qr_code, corners);
}
TEST_CYCLE()
{
decoded_info = detector.detectAndDecode(not_qr_code, corners);
ASSERT_FALSE(decoded_info.size()); ASSERT_FALSE(decoded_info.size());
} }
SANITY_CHECK_NOTHING(); SANITY_CHECK_NOTHING();
} }
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode_Multi, testing::ValuesIn(qrcode_images_multiple)); 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, INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_Not_QRCode,
::testing::Combine( ::testing::Combine(
::testing::ValuesIn(qrcode_model_path),
::testing::Values("zero", "random", "chessboard"), ::testing::Values("zero", "random", "chessboard"),
::testing::Values(Size(640, 480), Size(1280, 720)) ::testing::Values(Size(640, 480), Size(1280, 720))
)); ));