|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +#include "perf_precomp.hpp" |
| 6 | +namespace opencv_test |
| 7 | +{ |
| 8 | +namespace |
| 9 | +{ |
| 10 | +std::string qrcode_model_path[] = {"", "dnn/wechat_2021-01"}; |
| 11 | + |
| 12 | +std::string qrcode_images_name[] = { |
| 13 | + "version_1_top.jpg", |
| 14 | + "version_2_left.jpg", "version_2_up.jpg", "version_2_top.jpg", |
| 15 | + "version_3_down.jpg", "version_3_top.jpg", |
| 16 | + "version_4_top.jpg", |
| 17 | + "version_5_down.jpg", "version_5_left.jpg", "version_5_up.jpg", "version_5_top.jpg", |
| 18 | + "russian.jpg", "kanji.jpg", "link_wiki_cv.jpg"}; |
| 19 | + |
| 20 | +std::string qrcode_images_multiple[] = {"2_qrcodes.png", "3_qrcodes.png", "3_close_qrcodes.png", |
| 21 | + "4_qrcodes.png", "5_qrcodes.png", "7_qrcodes.png"}; |
| 22 | + |
| 23 | +WeChatQRCode createQRDetectorWithDNN(std::string& model_path) |
| 24 | +{ |
| 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); |
| 32 | + } |
| 33 | + return WeChatQRCode(path_detect_prototxt, path_detect_caffemodel, path_sr_prototxt, path_sr_caffemodel); |
| 34 | +} |
| 35 | + |
| 36 | +typedef ::perf::TestBaseWithParam< tuple< std::string,std::string > > Perf_Objdetect_QRCode; |
| 37 | + |
| 38 | +PERF_TEST_P_(Perf_Objdetect_QRCode, detect_and_decode) |
| 39 | +{ |
| 40 | + std::string model_path = get<0>(GetParam()); |
| 41 | + std::string name_current_image = get<1>(GetParam()); |
| 42 | + const std::string root = "cv/qrcode/"; |
| 43 | + |
| 44 | + std::string image_path = findDataFile(root + name_current_image); |
| 45 | + Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode; |
| 46 | + ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path; |
| 47 | + |
| 48 | + std::vector< Mat > corners; |
| 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 | + } |
| 56 | + TEST_CYCLE() |
| 57 | + { |
| 58 | + decoded_info = detector.detectAndDecode(src, corners); |
| 59 | + ASSERT_FALSE(decoded_info[0].empty()); |
| 60 | + } |
| 61 | + SANITY_CHECK_NOTHING(); |
| 62 | +} |
| 63 | + |
| 64 | +typedef ::perf::TestBaseWithParam< tuple< std::string,std::string > > Perf_Objdetect_QRCode_Multi; |
| 65 | + |
| 66 | +PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detect_and_decode) |
| 67 | +{ |
| 68 | + std::string model_path = get<0>(GetParam()); |
| 69 | + std::string name_current_image = get<1>(GetParam()); |
| 70 | + const std::string root = "cv/qrcode/multiple/"; |
| 71 | + |
| 72 | + std::string image_path = findDataFile(root + name_current_image); |
| 73 | + Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode; |
| 74 | + ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path; |
| 75 | + |
| 76 | + std::vector< Mat > corners; |
| 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); |
| 83 | + } |
| 84 | + TEST_CYCLE() |
| 85 | + { |
| 86 | + decoded_info = detector.detectAndDecode(src, corners); |
| 87 | + ASSERT_TRUE(decoded_info.size()); |
| 88 | + } |
| 89 | + for(size_t i = 0; i < decoded_info.size(); i++) |
| 90 | + { |
| 91 | + ASSERT_FALSE(decoded_info[i].empty()); |
| 92 | + } |
| 93 | + SANITY_CHECK_NOTHING(); |
| 94 | +} |
| 95 | + |
| 96 | +typedef ::perf::TestBaseWithParam< tuple<std::string, std::string, Size> >Perf_Objdetect_Not_QRCode; |
| 97 | + |
| 98 | +PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect_and_decode) |
| 99 | +{ |
| 100 | + std::string model_path = get<0>(GetParam()); |
| 101 | + std::string type_gen = get<1>(GetParam()); |
| 102 | + Size resolution = get<2>(GetParam()); |
| 103 | + Mat not_qr_code(resolution, CV_8UC1, Scalar(0)); |
| 104 | + if (type_gen == "random") |
| 105 | + { |
| 106 | + RNG rng; |
| 107 | + rng.fill(not_qr_code, RNG::UNIFORM, Scalar(0), Scalar(1)); |
| 108 | + } |
| 109 | + else if (type_gen == "chessboard") |
| 110 | + { |
| 111 | + uint8_t next_pixel = 255; |
| 112 | + for (int j = 0; j < not_qr_code.cols; j++) |
| 113 | + { |
| 114 | + not_qr_code.ptr<uchar>(0)[j] = next_pixel; |
| 115 | + next_pixel = 255 - next_pixel; |
| 116 | + } |
| 117 | + for (int r = not_qr_code.cols; r < not_qr_code.rows * not_qr_code.cols; r++) |
| 118 | + { |
| 119 | + int i = r / not_qr_code.cols; |
| 120 | + int j = r % not_qr_code.cols; |
| 121 | + not_qr_code.ptr<uchar>(i)[j] = 255 - not_qr_code.ptr<uchar>(i-1)[j]; |
| 122 | + } |
| 123 | + } |
| 124 | + std::vector< Mat > corners; |
| 125 | + std::vector< String > decoded_info; |
| 126 | + auto detector = createQRDetectorWithDNN(model_path); |
| 127 | + // warmup |
| 128 | + if (!model_path.empty()) |
| 129 | + { |
| 130 | + decoded_info = detector.detectAndDecode(not_qr_code, corners); |
| 131 | + } |
| 132 | + TEST_CYCLE() |
| 133 | + { |
| 134 | + decoded_info = detector.detectAndDecode(not_qr_code, corners); |
| 135 | + ASSERT_FALSE(decoded_info.size()); |
| 136 | + } |
| 137 | + SANITY_CHECK_NOTHING(); |
| 138 | +} |
| 139 | + |
| 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 | + )); |
| 151 | +INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_Not_QRCode, |
| 152 | + ::testing::Combine( |
| 153 | + ::testing::ValuesIn(qrcode_model_path), |
| 154 | + ::testing::Values("zero", "random", "chessboard"), |
| 155 | + ::testing::Values(Size(640, 480), Size(1280, 720)) |
| 156 | + )); |
| 157 | + |
| 158 | +} // namespace |
| 159 | +} // namespace |
0 commit comments