test_qrcode.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. // Tencent is pleased to support the open source community by making WeChat QRCode available.
  6. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  7. #include "test_precomp.hpp"
  8. namespace opencv_test {
  9. namespace {
  10. std::string qrcode_images_name[] = {
  11. "version_1_down.jpg", /*"version_1_left.jpg", "version_1_right.jpg", "version_1_up.jpg",*/
  12. "version_1_top.jpg",
  13. /*"version_2_down.jpg",*/ "version_2_left.jpg", /*"version_2_right.jpg",*/
  14. "version_2_up.jpg",
  15. "version_2_top.jpg",
  16. "version_3_down.jpg",
  17. "version_3_left.jpg",
  18. /*"version_3_right.jpg",*/ "version_3_up.jpg",
  19. "version_3_top.jpg",
  20. "version_4_down.jpg",
  21. "version_4_left.jpg",
  22. /*"version_4_right.jpg",*/ "version_4_up.jpg",
  23. "version_4_top.jpg",
  24. "version_5_down.jpg",
  25. "version_5_left.jpg",
  26. /*"version_5_right.jpg",*/ "version_5_up.jpg",
  27. "version_5_top.jpg",
  28. "russian.jpg",
  29. "kanji.jpg", /*"link_github_ocv.jpg",*/
  30. "link_ocv.jpg",
  31. "link_wiki_cv.jpg"};
  32. std::string qrcode_images_close[] = {/*"close_1.png",*/ "close_2.png", "close_3.png", "close_4.png",
  33. "close_5.png"};
  34. std::string qrcode_images_monitor[] = {"monitor_1.png", "monitor_2.png", "monitor_3.png",
  35. "monitor_4.png", "monitor_5.png"};
  36. std::string qrcode_images_curved[] = {"curved_1.jpg", /*"curved_2.jpg", "curved_3.jpg",
  37. "curved_4.jpg",*/
  38. "curved_5.jpg", "curved_6.jpg",
  39. /*"curved_7.jpg", "curved_8.jpg"*/};
  40. // std::string qrcode_images_multiple[] = {"2_qrcodes.png", "3_close_qrcodes.png", "3_qrcodes.png",
  41. // "4_qrcodes.png", "5_qrcodes.png", "6_qrcodes.png",
  42. // "7_qrcodes.png", "8_close_qrcodes.png"};
  43. typedef testing::TestWithParam<std::string> Objdetect_QRCode;
  44. TEST_P(Objdetect_QRCode, regression) {
  45. const std::string name_current_image = GetParam();
  46. const std::string root = "qrcode/";
  47. std::string image_path = findDataFile(root + name_current_image);
  48. Mat src = imread(image_path, IMREAD_GRAYSCALE);
  49. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  50. vector<Mat> points;
  51. // can not find the model file
  52. // so we temporarily comment it out
  53. // auto detector = wechat_qrcode::WeChatQRCode(
  54. // findDataFile("detect.prototxt", false), findDataFile("detect.caffemodel", false),
  55. // findDataFile("sr.prototxt", false), findDataFile("sr.caffemodel", false));
  56. auto detector = wechat_qrcode::WeChatQRCode();
  57. auto decoded_info = detector.detectAndDecode(src, points);
  58. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  59. FileStorage file_config(dataset_config, FileStorage::READ);
  60. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  61. {
  62. FileNode images_list = file_config["test_images"];
  63. size_t images_count = static_cast<size_t>(images_list.size());
  64. ASSERT_GT(images_count, 0u)
  65. << "Can't find validation data entries in 'test_images': " << dataset_config;
  66. for (size_t index = 0; index < images_count; index++) {
  67. FileNode config = images_list[(int)index];
  68. std::string name_test_image = config["image_name"];
  69. if (name_test_image == name_current_image) {
  70. std::string original_info = config["info"];
  71. string decoded_str;
  72. if (decoded_info.size()) {
  73. decoded_str = decoded_info[0];
  74. }
  75. EXPECT_EQ(decoded_str, original_info);
  76. return; // done
  77. }
  78. }
  79. std::cerr << "Not found results for '" << name_current_image
  80. << "' image in config file:" << dataset_config << std::endl
  81. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  82. << std::endl;
  83. }
  84. }
  85. typedef testing::TestWithParam<std::string> Objdetect_QRCode_Close;
  86. TEST_P(Objdetect_QRCode_Close, regression) {
  87. const std::string name_current_image = GetParam();
  88. const std::string root = "qrcode/close/";
  89. std::string image_path = findDataFile(root + name_current_image);
  90. Mat src = imread(image_path, IMREAD_GRAYSCALE);
  91. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  92. vector<Mat> points;
  93. // can not find the model file
  94. // so we temporarily comment it out
  95. // auto detector = wechat_qrcode::WeChatQRCode(
  96. // findDataFile("detect.prototxt", false), findDataFile("detect.caffemodel", false),
  97. // findDataFile("sr.prototxt", false), findDataFile("sr.caffemodel", false));
  98. auto detector = wechat_qrcode::WeChatQRCode();
  99. auto decoded_info = detector.detectAndDecode(src, points);
  100. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  101. FileStorage file_config(dataset_config, FileStorage::READ);
  102. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  103. {
  104. FileNode images_list = file_config["close_images"];
  105. size_t images_count = static_cast<size_t>(images_list.size());
  106. ASSERT_GT(images_count, 0u)
  107. << "Can't find validation data entries in 'close_images': " << dataset_config;
  108. for (size_t index = 0; index < images_count; index++) {
  109. FileNode config = images_list[(int)index];
  110. std::string name_test_image = config["image_name"];
  111. if (name_test_image == name_current_image) {
  112. std::string original_info = config["info"];
  113. string decoded_str;
  114. if (decoded_info.size()) {
  115. decoded_str = decoded_info[0];
  116. }
  117. EXPECT_EQ(decoded_str, original_info);
  118. return; // done
  119. }
  120. }
  121. std::cerr << "Not found results for '" << name_current_image
  122. << "' image in config file:" << dataset_config << std::endl
  123. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  124. << std::endl;
  125. }
  126. }
  127. typedef testing::TestWithParam<std::string> Objdetect_QRCode_Monitor;
  128. TEST_P(Objdetect_QRCode_Monitor, regression) {
  129. const std::string name_current_image = GetParam();
  130. const std::string root = "qrcode/monitor/";
  131. std::string image_path = findDataFile(root + name_current_image);
  132. Mat src = imread(image_path, IMREAD_GRAYSCALE);
  133. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  134. vector<Mat> points;
  135. // can not find the model file
  136. // so we temporarily comment it out
  137. // auto detector = wechat_qrcode::WeChatQRCode(
  138. // findDataFile("detect.prototxt", false), findDataFile("detect.caffemodel", false),
  139. // findDataFile("sr.prototxt", false), findDataFile("sr.caffemodel", false));
  140. auto detector = wechat_qrcode::WeChatQRCode();
  141. auto decoded_info = detector.detectAndDecode(src, points);
  142. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  143. FileStorage file_config(dataset_config, FileStorage::READ);
  144. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  145. {
  146. FileNode images_list = file_config["monitor_images"];
  147. size_t images_count = static_cast<size_t>(images_list.size());
  148. ASSERT_GT(images_count, 0u)
  149. << "Can't find validation data entries in 'monitor_images': " << dataset_config;
  150. for (size_t index = 0; index < images_count; index++) {
  151. FileNode config = images_list[(int)index];
  152. std::string name_test_image = config["image_name"];
  153. if (name_test_image == name_current_image) {
  154. std::string original_info = config["info"];
  155. string decoded_str;
  156. if (decoded_info.size()) {
  157. decoded_str = decoded_info[0];
  158. }
  159. EXPECT_EQ(decoded_str, original_info);
  160. return; // done
  161. }
  162. }
  163. std::cerr << "Not found results for '" << name_current_image
  164. << "' image in config file:" << dataset_config << std::endl
  165. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  166. << std::endl;
  167. }
  168. }
  169. typedef testing::TestWithParam<std::string> Objdetect_QRCode_Curved;
  170. TEST_P(Objdetect_QRCode_Curved, regression) {
  171. const std::string name_current_image = GetParam();
  172. const std::string root = "qrcode/curved/";
  173. std::string image_path = findDataFile(root + name_current_image);
  174. Mat src = imread(image_path, IMREAD_GRAYSCALE);
  175. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  176. vector<Mat> points;
  177. // can not find the model file
  178. // so we temporarily comment it out
  179. // auto detector = wechat_qrcode::WeChatQRCode(
  180. // findDataFile("detect.prototxt", false), findDataFile("detect.caffemodel", false),
  181. // findDataFile("sr.prototxt", false), findDataFile("sr.caffemodel", false));
  182. auto detector = wechat_qrcode::WeChatQRCode();
  183. auto decoded_info = detector.detectAndDecode(src, points);
  184. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  185. FileStorage file_config(dataset_config, FileStorage::READ);
  186. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  187. {
  188. FileNode images_list = file_config["test_images"];
  189. size_t images_count = static_cast<size_t>(images_list.size());
  190. ASSERT_GT(images_count, 0u)
  191. << "Can't find validation data entries in 'test_images': " << dataset_config;
  192. for (size_t index = 0; index < images_count; index++) {
  193. FileNode config = images_list[(int)index];
  194. std::string name_test_image = config["image_name"];
  195. if (name_test_image == name_current_image) {
  196. std::string original_info = config["info"];
  197. string decoded_str;
  198. if (decoded_info.size()) {
  199. decoded_str = decoded_info[0];
  200. }
  201. EXPECT_EQ(decoded_str, original_info);
  202. return; // done
  203. }
  204. }
  205. std::cerr << "Not found results for '" << name_current_image
  206. << "' image in config file:" << dataset_config << std::endl
  207. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  208. << std::endl;
  209. }
  210. }
  211. typedef testing::TestWithParam<std::string> Objdetect_QRCode_Multi;
  212. TEST_P(Objdetect_QRCode_Multi, regression) {
  213. const std::string name_current_image = GetParam();
  214. const std::string root = "qrcode/multiple/";
  215. std::string image_path = findDataFile(root + name_current_image);
  216. Mat src = imread(image_path);
  217. ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
  218. vector<Mat> points;
  219. // can not find the model file
  220. // so we temporarily comment it out
  221. // auto detector = wechat_qrcode::WeChatQRCode(
  222. // findDataFile("detect.prototxt", false), findDataFile("detect.caffemodel", false),
  223. // findDataFile("sr.prototxt", false), findDataFile("sr.caffemodel", false));
  224. auto detector = wechat_qrcode::WeChatQRCode();
  225. vector<string> decoded_info = detector.detectAndDecode(src, points);
  226. const std::string dataset_config = findDataFile(root + "dataset_config.json");
  227. FileStorage file_config(dataset_config, FileStorage::READ);
  228. ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
  229. {
  230. FileNode images_list = file_config["multiple_images"];
  231. size_t images_count = static_cast<size_t>(images_list.size());
  232. ASSERT_GT(images_count, 0u)
  233. << "Can't find validation data entries in 'test_images': " << dataset_config;
  234. for (size_t index = 0; index < images_count; index++) {
  235. FileNode config = images_list[(int)index];
  236. std::string name_test_image = config["image_name"];
  237. if (name_test_image == name_current_image) {
  238. size_t count_eq_info = 0;
  239. for (int i = 0; i < int(decoded_info.size()); i++) {
  240. for (int j = 0; j < int(config["info"].size()); j++) {
  241. std::string original_info = config["info"][j];
  242. if (original_info == decoded_info[i]) {
  243. count_eq_info++;
  244. break;
  245. }
  246. }
  247. }
  248. EXPECT_EQ(config["info"].size(), count_eq_info);
  249. return; // done
  250. }
  251. }
  252. std::cerr << "Not found results for '" << name_current_image
  253. << "' image in config file:" << dataset_config << std::endl
  254. << "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
  255. << std::endl;
  256. }
  257. }
  258. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
  259. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Close, testing::ValuesIn(qrcode_images_close));
  260. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Monitor, testing::ValuesIn(qrcode_images_monitor));
  261. INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Curved, testing::ValuesIn(qrcode_images_curved));
  262. // INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Multi, testing::ValuesIn(qrcode_images_multiple));
  263. } // namespace
  264. } // namespace opencv_test