test_barcode.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "test_precomp.hpp"
  5. namespace opencv_test{namespace{
  6. //init validation map
  7. datasetType initValidation(std::string path)
  8. {
  9. const std::string valid_path = findDataFile(path);
  10. return buildDataSet(valid_path);
  11. }
  12. TEST(Barcode_BarcodeDetector_single, regression)
  13. {
  14. const std::string root = "barcode/single/";
  15. datasetType validation = initValidation(root + "result.csv");
  16. auto bardet = barcode::BarcodeDetector();
  17. datasetType::iterator iterator = validation.begin();
  18. while (iterator != validation.end())
  19. {
  20. std::string img_name = iterator->first;
  21. std::string result = iterator->second;
  22. std::string image_path = findDataFile(root + img_name);
  23. Mat img = imread(image_path);
  24. EXPECT_FALSE(img.empty()) << "Can't read image: " << image_path;
  25. std::vector<cv::Point2f> points;
  26. std::vector<std::string> infos;
  27. std::vector<cv::barcode::BarcodeType> formats;
  28. bardet.detectAndDecode(img, infos, formats, points);
  29. EXPECT_FALSE(points.empty()) << "Nothing detected: " << image_path;
  30. bool is_correct = false;
  31. for (const auto &ans : infos)
  32. {
  33. if (ans == result)
  34. {
  35. is_correct = true;
  36. break;
  37. }
  38. }
  39. EXPECT_TRUE(is_correct) << "No results for " << img_name;
  40. iterator++;
  41. }
  42. }
  43. TEST(Barcode_BarcodeDetector_detect_multi, detect_regression)
  44. {
  45. const std::string root = "barcode/multiple/";
  46. datasetType validation = initValidation(root + "result.csv");
  47. auto bardet = barcode::BarcodeDetector();
  48. datasetType::iterator iterator = validation.begin();
  49. while (iterator != validation.end())
  50. {
  51. std::string img = iterator->first;
  52. size_t expect_corners_size = std::stoi(iterator->second);
  53. std::string image_path = findDataFile(root + img);
  54. Mat src = imread(image_path);
  55. EXPECT_FALSE(src.empty()) << "Can't read image: " << image_path;
  56. std::vector<Point> corners;
  57. bardet.detect(src, corners);
  58. EXPECT_EQ(corners.size(), expect_corners_size) << "Can't detect all barcodes: " << img;
  59. iterator++;
  60. }
  61. }
  62. TEST(Barcode_BarcodeDetector_basic, not_found_barcode)
  63. {
  64. auto bardet = barcode::BarcodeDetector();
  65. std::vector<Point> corners;
  66. vector<cv::String> decoded_info;
  67. vector<barcode::BarcodeType> decoded_type;
  68. Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
  69. EXPECT_FALSE(bardet.detect(zero_image, corners));
  70. corners = std::vector<Point>(4);
  71. EXPECT_ANY_THROW(bardet.decode(zero_image, corners, decoded_info, decoded_type));
  72. }
  73. }} // namespace