opencv/modules/objdetect/test/test_barcode.cpp

148 lines
4.0 KiB
C++
Raw Normal View History

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "opencv2/objdetect/barcode.hpp"
2023-06-15 20:32:19 +08:00
#include <set>
using namespace std;
namespace opencv_test{namespace{
2023-06-15 20:32:19 +08:00
typedef std::set<string> StringSet;
2023-06-15 20:32:19 +08:00
// Convert ';'-separated strings to a set
inline static StringSet toSet(const string &line)
{
2023-06-15 20:32:19 +08:00
StringSet res;
string::size_type it = 0, ti;
while (true)
{
2023-06-15 20:32:19 +08:00
ti = line.find(';', it);
if (ti == string::npos)
{
2023-06-15 20:32:19 +08:00
res.insert(string(line, it, line.size() - it));
break;
}
2023-06-15 20:32:19 +08:00
res.insert(string(line, it, ti - it));
it = ti + 1;
}
2023-06-15 20:32:19 +08:00
return res;
}
2023-06-15 20:32:19 +08:00
// Convert vector of strings to a set
inline static StringSet toSet(const vector<string> &lines)
{
2023-06-15 20:32:19 +08:00
StringSet res;
for (const string & line : lines)
res.insert(line);
return res;
}
2023-06-15 20:32:19 +08:00
// Get all keys of a map in a vector
template<typename T, typename V>
inline static vector<T> getKeys(const map<T, V> &m)
{
2023-06-15 20:32:19 +08:00
vector<T> res;
for (const auto & it : m)
res.push_back(it.first);
return res;
}
2023-06-15 20:32:19 +08:00
struct BarcodeResult
{
string type;
string data;
};
2023-06-15 20:32:19 +08:00
map<string, BarcodeResult> testResults {
{ "single/book.jpg", {"EAN_13", "9787115279460"} },
{ "single/bottle_1.jpg", {"EAN_13", "6922255451427"} },
{ "single/bottle_2.jpg", {"EAN_13", "6921168509256"} },
{ "multiple/4_barcodes.jpg", {"EAN_13;EAN_13;EAN_13;EAN_13", "9787564350840;9783319200064;9787118081473;9787122276124"} }
};
typedef testing::TestWithParam< string > BarcodeDetector_main;
TEST_P(BarcodeDetector_main, interface)
{
2023-06-15 20:32:19 +08:00
const string fname = GetParam();
const string image_path = findDataFile(string("barcode/") + fname);
const StringSet expected_lines = toSet(testResults[fname].data);
const StringSet expected_types = toSet(testResults[fname].type);
const size_t expected_count = expected_lines.size(); // assume codes are unique
// TODO: verify points location
Mat img = imread(image_path);
ASSERT_FALSE(img.empty()) << "Can't read image: " << image_path;
barcode::BarcodeDetector det;
vector<Point2f> points;
vector<string> types;
vector<string> lines;
// common interface (single)
{
2023-06-15 20:32:19 +08:00
bool res = det.detect(img, points);
ASSERT_TRUE(res);
EXPECT_EQ(expected_count * 4, points.size());
}
{
2023-06-15 20:32:19 +08:00
string res = det.decode(img, points);
ASSERT_FALSE(res.empty());
EXPECT_EQ(1u, expected_lines.count(res));
}
{
string res = det.detectAndDecode(img, points);
ASSERT_FALSE(res.empty());
EXPECT_EQ(1u, expected_lines.count(res));
EXPECT_EQ(4u, points.size());
}
2023-06-15 20:32:19 +08:00
// common interface (multi)
{
bool res = det.detectMulti(img, points);
ASSERT_TRUE(res);
EXPECT_EQ(expected_count * 4, points.size());
}
{
bool res = det.decodeMulti(img, points, lines);
ASSERT_TRUE(res);
EXPECT_EQ(expected_lines, toSet(lines));
}
// specific interface
{
bool res = det.decodeWithType(img, points, lines, types);
ASSERT_TRUE(res);
EXPECT_EQ(expected_types, toSet(types));
EXPECT_EQ(expected_lines, toSet(lines));
}
{
bool res = det.detectAndDecodeWithType(img, lines, types, points);
ASSERT_TRUE(res);
EXPECT_EQ(expected_types, toSet(types));
EXPECT_EQ(expected_lines, toSet(lines));
}
}
2023-06-15 20:32:19 +08:00
INSTANTIATE_TEST_CASE_P(/**/, BarcodeDetector_main, testing::ValuesIn(getKeys(testResults)));
TEST(BarcodeDetector_base, invalid)
{
auto bardet = barcode::BarcodeDetector();
std::vector<Point> corners;
vector<cv::String> decoded_info;
Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
EXPECT_FALSE(bardet.detectMulti(zero_image, corners));
corners = std::vector<Point>(4);
EXPECT_ANY_THROW(bardet.decodeMulti(zero_image, corners, decoded_info));
}
}} // opencv_test::<anonymous>::