mirror of
https://github.com/opencv/opencv.git
synced 2025-06-11 11:45:30 +08:00
Merge pull request #15338 from rayonnant14:my_detect_and_decode_3.4
QR-Code detector : multiple detection * change in qr-codes detection * change in qr-codes detection * change in test * change in test * add multiple detection * multiple detection * multiple detect * add parallel implementation * add functional for performance tests * change in test * add perftest * returned implementation for 1 qr-code, added support for vector<Mat> and vector<vector<Point2f>> in MultipleDetectAndDecode * deleted all lambda expressions * changing in triangle sort * fixed warnings * fixed errors * add java and python tests * change in java tests * change in java and python tests * change in perf test * change in qrcode.cpp * add spaces * change in qrcode.cpp * change in qrcode.cpp * change in qrcode.cpp * change in java tests * change in java tests * solved problems * solved problems * change in java and python tests * change in python tests * change in python tests * change in python tests * change in methods name * deleted sample qrcode_multi, change in qrcode.cpp * change in perf tests * change in objdetect.hpp * deleted code duplication in sample qrcode.cpp * returned spaces * added spaces * deleted draw function * change in qrcode.cpp * change in qrcode.cpp * deleted all draw functions * objdetect(QR): extractVerticalLines * objdetect(QR): whitespaces * objdetect(QR): simplify operations, avoid duplicated code * change in interface, additional checks in java and python tests, added new key in sample for saving original image from camera * fix warnings and errors in python test * fix * write in file with space key * solved error with empty mat check in python test * correct path to test image * deleted spaces * solved error with check empty mat in python tests * added check of empty vector of points * samples: rework qrcode.cpp * objdetect(QR): fix API, input parameters must be first * objdetect(QR): test/fix points layout
This commit is contained in:
parent
282a15c9f9
commit
acc089ca64
@ -914,7 +914,10 @@ class JavaWrapperGenerator(object):
|
||||
c_epilogue.append("Mat* _retval_ = new Mat();")
|
||||
c_epilogue.append(fi.ctype+"_to_Mat(_ret_val_vector_, *_retval_);")
|
||||
else:
|
||||
c_epilogue.append("return " + fi.ctype + "_to_List(env, _ret_val_vector_);")
|
||||
if ret:
|
||||
c_epilogue.append("jobject _retval_ = " + fi.ctype + "_to_List(env, _ret_val_vector_);")
|
||||
else:
|
||||
c_epilogue.append("return " + fi.ctype + "_to_List(env, _ret_val_vector_);")
|
||||
if fi.classname:
|
||||
if not fi.ctype: # c-tor
|
||||
retval = fi.fullClass(isCPP=True) + "* _retval_ = "
|
||||
|
@ -694,8 +694,8 @@ public:
|
||||
CV_WRAP bool detect(InputArray img, OutputArray points) const;
|
||||
|
||||
/** @brief Decodes QR code in image once it's found by the detect() method.
|
||||
Returns UTF8-encoded output string or empty string if the code cannot be decoded.
|
||||
|
||||
Returns UTF8-encoded output string or empty string if the code cannot be decoded.
|
||||
@param img grayscale or color (BGR) image containing QR code.
|
||||
@param points Quadrangle vertices found by detect() method (or some other algorithm).
|
||||
@param straight_qrcode The optional output image containing rectified and binarized QR code
|
||||
@ -705,11 +705,44 @@ public:
|
||||
/** @brief Both detects and decodes QR code
|
||||
|
||||
@param img grayscale or color (BGR) image containing QR code.
|
||||
@param points opiotnal output array of vertices of the found QR code quadrangle. Will be empty if not found.
|
||||
@param points optional output array of vertices of the found QR code quadrangle. Will be empty if not found.
|
||||
@param straight_qrcode The optional output image containing rectified and binarized QR code
|
||||
*/
|
||||
CV_WRAP cv::String detectAndDecode(InputArray img, OutputArray points=noArray(),
|
||||
OutputArray straight_qrcode = noArray());
|
||||
/** @brief Detects QR codes in image and returns the vector of the quadrangles containing the codes.
|
||||
@param img grayscale or color (BGR) image containing (or not) QR codes.
|
||||
@param points Output vector of vector of vertices of the minimum-area quadrangle containing the codes.
|
||||
*/
|
||||
CV_WRAP
|
||||
bool detectMulti(InputArray img, OutputArray points) const;
|
||||
|
||||
/** @brief Decodes QR codes in image once it's found by the detect() method.
|
||||
@param img grayscale or color (BGR) image containing QR codes.
|
||||
@param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
|
||||
@param points vector of Quadrangle vertices found by detect() method (or some other algorithm).
|
||||
@param straight_qrcode The optional output vector of images containing rectified and binarized QR codes
|
||||
*/
|
||||
CV_WRAP
|
||||
bool decodeMulti(
|
||||
InputArray img, InputArray points,
|
||||
CV_OUT std::vector<cv::String>& decoded_info,
|
||||
OutputArrayOfArrays straight_qrcode = noArray()
|
||||
) const;
|
||||
|
||||
/** @brief Both detects and decodes QR codes
|
||||
@param img grayscale or color (BGR) image containing QR codes.
|
||||
@param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
|
||||
@param points optional output vector of vertices of the found QR code quadrangles. Will be empty if not found.
|
||||
@param straight_qrcode The optional output vector of images containing rectified and binarized QR codes
|
||||
*/
|
||||
CV_WRAP
|
||||
bool detectAndDecodeMulti(
|
||||
InputArray img, CV_OUT std::vector<cv::String>& decoded_info,
|
||||
OutputArray points = noArray(),
|
||||
OutputArrayOfArrays straight_qrcode = noArray()
|
||||
) const;
|
||||
|
||||
protected:
|
||||
struct Impl;
|
||||
Ptr<Impl> p;
|
||||
@ -731,6 +764,29 @@ CV_EXPORTS bool detectQRCode(InputArray in, std::vector<Point> &points, double e
|
||||
*/
|
||||
CV_EXPORTS bool decodeQRCode(InputArray in, InputArray points, std::string &decoded_info, OutputArray straight_qrcode = noArray());
|
||||
|
||||
/** @brief Detect QR codes in image and return vector of minimum area of quadrangle that describes QR codes.
|
||||
@param in Matrix of the type CV_8UC1 containing an image where QR codes are detected.
|
||||
@param points Output vector of vertices of quadrangles of minimal area that describes QR codes.
|
||||
@param eps_x Epsilon neighborhood, which allows you to determine the horizontal pattern of the scheme 1:1:3:1:1 according to QR code standard.
|
||||
@param eps_y Epsilon neighborhood, which allows you to determine the vertical pattern of the scheme 1:1:3:1:1 according to QR code standard.
|
||||
*/
|
||||
CV_EXPORTS
|
||||
bool detectQRCodeMulti(
|
||||
InputArray in, std::vector<Point> &points,
|
||||
double eps_x = 0.2, double eps_y = 0.1);
|
||||
|
||||
/** @brief Decode QR codes in image and return text that is encrypted in QR code.
|
||||
@param in Matrix of the type CV_8UC1 containing an image where QR code are detected.
|
||||
@param points Input vector of vertices of quadrangles of minimal area that describes QR codes.
|
||||
@param decoded_info vector of String information that is encrypted in QR codes.
|
||||
@param straight_qrcode vector of Matrixes of the type CV_8UC1 containing an binary straight QR codes.
|
||||
*/
|
||||
CV_EXPORTS
|
||||
bool decodeQRCodeMulti(
|
||||
InputArray in, InputArray points,
|
||||
CV_OUT std::vector<std::string> &decoded_info,
|
||||
OutputArrayOfArrays straight_qrcode = noArray());
|
||||
|
||||
//! @} objdetect
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
package org.opencv.test.objdetect;
|
||||
|
||||
import java.util.List;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.objdetect.QRCodeDetector;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.test.OpenCVTestCase;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class QRCodeDetectorTest extends OpenCVTestCase {
|
||||
|
||||
@ -21,9 +23,27 @@ public class QRCodeDetectorTest extends OpenCVTestCase {
|
||||
|
||||
public void testDetectAndDecode() {
|
||||
Mat img = Imgcodecs.imread(testDataPath + "/cv/qrcode/link_ocv.jpg");
|
||||
assertFalse(img.empty());
|
||||
QRCodeDetector detector = new QRCodeDetector();
|
||||
assertNotNull(detector);
|
||||
String output = detector.detectAndDecode(img);
|
||||
assertEquals(output, "https://opencv.org/");
|
||||
}
|
||||
|
||||
public void testDetectAndDecodeMulti() {
|
||||
Mat img = Imgcodecs.imread(testDataPath + "/cv/qrcode/multiple/6_qrcodes.png");
|
||||
assertFalse(img.empty());
|
||||
QRCodeDetector detector = new QRCodeDetector();
|
||||
assertNotNull(detector);
|
||||
List < String > output = new ArrayList< String >();
|
||||
boolean result = detector.detectAndDecodeMulti(img, output);
|
||||
assertTrue(result);
|
||||
assertEquals(output.size(), 6);
|
||||
assertEquals(output.get(0), "SKIP");
|
||||
assertEquals(output.get(1), "EXTRA");
|
||||
assertEquals(output.get(2), "TWO STEPS FORWARD");
|
||||
assertEquals(output.get(3), "STEP BACK");
|
||||
assertEquals(output.get(4), "QUESTION");
|
||||
assertEquals(output.get(5), "STEP FORWARD");
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,42 @@ import cv2 as cv
|
||||
from tests_common import NewOpenCVTests
|
||||
|
||||
class qrcode_detector_test(NewOpenCVTests):
|
||||
|
||||
def test_detect(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/link_ocv.jpg'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points = detector.detect(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(points.shape, (1, 4, 2))
|
||||
|
||||
def test_detect_and_decode(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/link_ocv.jpg'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points, straight_qrcode = detector.detectAndDecode(img)
|
||||
self.assertEqual(retval, "https://opencv.org/");
|
||||
self.assertEqual(retval, "https://opencv.org/")
|
||||
self.assertEqual(points.shape, (1, 4, 2))
|
||||
|
||||
def test_detect_multi(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/multiple/6_qrcodes.png'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, points = detector.detectMulti(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(points.shape, (6, 4, 2))
|
||||
|
||||
def test_detect_and_decode_multi(self):
|
||||
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/qrcode/multiple/6_qrcodes.png'))
|
||||
self.assertFalse(img is None)
|
||||
detector = cv.QRCodeDetector()
|
||||
retval, decoded_data, points, straight_qrcode = detector.detectAndDecodeMulti(img)
|
||||
self.assertTrue(retval)
|
||||
self.assertEqual(len(decoded_data), 6)
|
||||
self.assertEqual(decoded_data[0], "TWO STEPS FORWARD")
|
||||
self.assertEqual(decoded_data[1], "EXTRA")
|
||||
self.assertEqual(decoded_data[2], "SKIP")
|
||||
self.assertEqual(decoded_data[3], "STEP FORWARD")
|
||||
self.assertEqual(decoded_data[4], "STEP BACK")
|
||||
self.assertEqual(decoded_data[5], "QUESTION")
|
||||
self.assertEqual(points.shape, (6, 4, 2))
|
||||
|
@ -53,6 +53,56 @@ PERF_TEST_P_(Perf_Objdetect_QRCode, decode)
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef ::perf::TestBaseWithParam< std::string > Perf_Objdetect_QRCode_Multi;
|
||||
|
||||
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detectMulti)
|
||||
{
|
||||
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);
|
||||
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
|
||||
std::vector<Point2f> corners;
|
||||
QRCodeDetector qrcode;
|
||||
TEST_CYCLE() ASSERT_TRUE(qrcode.detectMulti(src, corners));
|
||||
SANITY_CHECK(corners);
|
||||
}
|
||||
|
||||
#ifdef HAVE_QUIRC
|
||||
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, decodeMulti)
|
||||
{
|
||||
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);
|
||||
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
|
||||
QRCodeDetector qrcode;
|
||||
std::vector<Point2f> corners;
|
||||
ASSERT_TRUE(qrcode.detectMulti(src, corners));
|
||||
std::vector<Mat> straight_barcode;
|
||||
std::vector< cv::String > decoded_info;
|
||||
TEST_CYCLE()
|
||||
{
|
||||
ASSERT_TRUE(qrcode.decodeMulti(src, corners, decoded_info, straight_barcode));
|
||||
for(size_t i = 0; i < decoded_info.size(); i++)
|
||||
{
|
||||
ASSERT_FALSE(decoded_info[i].empty());
|
||||
}
|
||||
}
|
||||
std::vector < std::vector< uint8_t > > decoded_info_uint8_t;
|
||||
for(size_t i = 0; i < decoded_info.size(); i++)
|
||||
{
|
||||
std::vector< uint8_t > tmp(decoded_info[i].begin(), decoded_info[i].end());
|
||||
decoded_info_uint8_t.push_back(tmp);
|
||||
}
|
||||
SANITY_CHECK(decoded_info_uint8_t);
|
||||
SANITY_CHECK(straight_barcode);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode,
|
||||
::testing::Values(
|
||||
"version_1_down.jpg", "version_1_left.jpg", "version_1_right.jpg", "version_1_up.jpg", "version_1_top.jpg",
|
||||
@ -61,6 +111,13 @@ INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode,
|
||||
)
|
||||
);
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode_Multi,
|
||||
::testing::Values(
|
||||
"2_qrcodes.png", "3_close_qrcodes.png", "3_qrcodes.png", "4_qrcodes.png",
|
||||
"5_qrcodes.png", "6_qrcodes.png", "7_qrcodes.png", "8_close_qrcodes.png"
|
||||
)
|
||||
);
|
||||
|
||||
typedef ::perf::TestBaseWithParam< tuple< std::string, Size > > Perf_Objdetect_Not_QRCode;
|
||||
|
||||
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect)
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -21,7 +21,11 @@ std::string qrcode_images_close[] = {
|
||||
std::string qrcode_images_monitor[] = {
|
||||
"monitor_1.png", "monitor_2.png", "monitor_3.png", "monitor_4.png", "monitor_5.png"
|
||||
};
|
||||
// #define UPDATE_QRCODE_TEST_DATA
|
||||
std::string qrcode_images_multiple[] = {
|
||||
"2_qrcodes.png", "3_close_qrcodes.png", "3_qrcodes.png", "4_qrcodes.png",
|
||||
"5_qrcodes.png", "6_qrcodes.png", "7_qrcodes.png", "8_close_qrcodes.png"
|
||||
};
|
||||
//#define UPDATE_QRCODE_TEST_DATA
|
||||
#ifdef UPDATE_QRCODE_TEST_DATA
|
||||
|
||||
TEST(Objdetect_QRCode, generate_test_data)
|
||||
@ -134,6 +138,66 @@ TEST(Objdetect_QRCode_Monitor, generate_test_data)
|
||||
file_config.release();
|
||||
}
|
||||
|
||||
|
||||
TEST(Objdetect_QRCode_Multi, generate_test_data)
|
||||
{
|
||||
const std::string root = "qrcode/multiple/";
|
||||
const std::string dataset_config = findDataFile(root + "dataset_config.json");
|
||||
FileStorage file_config(dataset_config, FileStorage::WRITE);
|
||||
|
||||
file_config << "multiple_images" << "[:";
|
||||
size_t multiple_count = sizeof(qrcode_images_multiple) / sizeof(qrcode_images_multiple[0]);
|
||||
for (size_t i = 0; i < multiple_count; i++)
|
||||
{
|
||||
file_config << "{:" << "image_name" << qrcode_images_multiple[i];
|
||||
std::string image_path = findDataFile(root + qrcode_images_multiple[i]);
|
||||
Mat src = imread(image_path);
|
||||
|
||||
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
|
||||
std::vector<Point> corners;
|
||||
EXPECT_TRUE(detectQRCodeMulti(src, corners));
|
||||
#ifdef HAVE_QUIRC
|
||||
std::vector<cv::String> decoded_info;
|
||||
std::vector<Mat> straight_barcode;
|
||||
EXPECT_TRUE(decodeQRCodeMulti(src, corners, decoded_info, straight_barcode));
|
||||
#endif
|
||||
file_config << "x" << "[:";
|
||||
for(size_t j = 0; j < corners.size(); j += 4)
|
||||
{
|
||||
file_config << "[:";
|
||||
for (size_t k = 0; k < 4; k++)
|
||||
{
|
||||
file_config << corners[j + k].x;
|
||||
}
|
||||
file_config << "]";
|
||||
}
|
||||
file_config << "]";
|
||||
file_config << "y" << "[:";
|
||||
for(size_t j = 0; j < corners.size(); j += 4)
|
||||
{
|
||||
file_config << "[:";
|
||||
for (size_t k = 0; k < 4; k++)
|
||||
{
|
||||
file_config << corners[j + k].y;
|
||||
}
|
||||
file_config << "]";
|
||||
}
|
||||
file_config << "]";
|
||||
file_config << "info";
|
||||
file_config << "[:";
|
||||
|
||||
for(size_t j = 0; j < decoded_info.size(); j++)
|
||||
{
|
||||
file_config << decoded_info[j];
|
||||
}
|
||||
file_config << "]";
|
||||
file_config << "}";
|
||||
}
|
||||
|
||||
file_config << "]";
|
||||
file_config.release();
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
typedef testing::TestWithParam< std::string > Objdetect_QRCode;
|
||||
@ -326,9 +390,96 @@ TEST_P(Objdetect_QRCode_Monitor, regression)
|
||||
}
|
||||
}
|
||||
|
||||
typedef testing::TestWithParam < std::string > Objdetect_QRCode_Multi;
|
||||
TEST_P(Objdetect_QRCode_Multi, regression)
|
||||
{
|
||||
const std::string name_current_image = GetParam();
|
||||
const std::string root = "qrcode/multiple/";
|
||||
const int pixels_error = 3;
|
||||
|
||||
std::string image_path = findDataFile(root + name_current_image);
|
||||
Mat src = imread(image_path);
|
||||
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
|
||||
QRCodeDetector qrcode;
|
||||
std::vector<Point> corners;
|
||||
#ifdef HAVE_QUIRC
|
||||
std::vector<cv::String> decoded_info;
|
||||
std::vector<Mat> straight_barcode;
|
||||
EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
|
||||
ASSERT_FALSE(corners.empty());
|
||||
ASSERT_FALSE(decoded_info.empty());
|
||||
#else
|
||||
ASSERT_TRUE(qrcode.detectMulti(src, corners));
|
||||
#endif
|
||||
|
||||
const std::string dataset_config = findDataFile(root + "dataset_config.json");
|
||||
FileStorage file_config(dataset_config, FileStorage::READ);
|
||||
ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
|
||||
{
|
||||
FileNode images_list = file_config["multiple_images"];
|
||||
size_t images_count = static_cast<size_t>(images_list.size());
|
||||
ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
|
||||
for (size_t index = 0; index < images_count; index++)
|
||||
{
|
||||
FileNode config = images_list[(int)index];
|
||||
std::string name_test_image = config["image_name"];
|
||||
if (name_test_image == name_current_image)
|
||||
{
|
||||
for(int j = 0; j < int(corners.size()); j += 4)
|
||||
{
|
||||
bool ok = false;
|
||||
for (int k = 0; k < int(corners.size() / 4); k++)
|
||||
{
|
||||
int count_eq_points = 0;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
int x = config["x"][k][i];
|
||||
int y = config["y"][k][i];
|
||||
if(((abs(corners[j + i].x - x)) <= pixels_error) && ((abs(corners[j + i].y - y)) <= pixels_error))
|
||||
count_eq_points++;
|
||||
}
|
||||
if (count_eq_points == 4)
|
||||
{
|
||||
ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
EXPECT_TRUE(ok);
|
||||
}
|
||||
|
||||
#ifdef HAVE_QUIRC
|
||||
size_t count_eq_info = 0;
|
||||
for(int i = 0; i < int(decoded_info.size()); i++)
|
||||
{
|
||||
for(int j = 0; j < int(decoded_info.size()); j++)
|
||||
{
|
||||
std::string original_info = config["info"][j];
|
||||
if(original_info == decoded_info[i])
|
||||
{
|
||||
count_eq_info++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
EXPECT_EQ(decoded_info.size(), count_eq_info);
|
||||
#endif
|
||||
|
||||
return; // done
|
||||
}
|
||||
}
|
||||
std::cerr
|
||||
<< "Not found results for '" << name_current_image
|
||||
<< "' image in config file:" << dataset_config << std::endl
|
||||
<< "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Close, testing::ValuesIn(qrcode_images_close));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Monitor, testing::ValuesIn(qrcode_images_monitor));
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Multi, testing::ValuesIn(qrcode_images_multiple));
|
||||
|
||||
TEST(Objdetect_QRCode_basic, not_found_qrcode)
|
||||
{
|
||||
|
@ -2,23 +2,45 @@
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include "opencv2/videoio.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
static void drawQRCodeContour(Mat &color_image, vector<Point> transform);
|
||||
static void drawFPS(Mat &color_image, double fps);
|
||||
static int liveQRCodeDetect(const string& out_file);
|
||||
static int imageQRCodeDetect(const string& in_file, const string& out_file);
|
||||
static int liveQRCodeDetect();
|
||||
static int imageQRCodeDetect(const string& in_file);
|
||||
|
||||
static bool g_modeMultiQR = false;
|
||||
static bool g_detectOnly = false;
|
||||
|
||||
static string g_out_file_name, g_out_file_ext;
|
||||
static int g_save_idx = 0;
|
||||
|
||||
static bool g_saveDetections = false;
|
||||
static bool g_saveAll = false;
|
||||
|
||||
static string getQRModeString()
|
||||
{
|
||||
std::ostringstream out;
|
||||
out << "QR"
|
||||
<< (g_modeMultiQR ? " multi" : "")
|
||||
<< (g_detectOnly ? " detector" : " decoder");
|
||||
return out.str();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
const string keys =
|
||||
"{h help ? | | print help messages }"
|
||||
"{i in | | input path to file for detect (with parameter - show image, otherwise - camera)}"
|
||||
"{o out | | output path to file (save image, work with -i parameter) }";
|
||||
"{i in | | input image path (also switches to image detection mode) }"
|
||||
"{detect | false | detect QR code only (skip decoding) }"
|
||||
"{m multi | | use detect for multiple qr-codes }"
|
||||
"{o out | qr_code.png | path to result file }"
|
||||
"{save_detections | false | save all QR detections (video mode only) }"
|
||||
"{save_all | false | save all processed frames (video mode only) }"
|
||||
;
|
||||
CommandLineParser cmd_parser(argc, argv, keys);
|
||||
|
||||
cmd_parser.about("This program detects the QR-codes from camera or images using the OpenCV library.");
|
||||
@ -28,32 +50,51 @@ int main(int argc, char *argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
string in_file_name = cmd_parser.get<string>("in"); // input path to image
|
||||
string out_file_name;
|
||||
if (cmd_parser.has("out"))
|
||||
out_file_name = cmd_parser.get<string>("out"); // output path to image
|
||||
string in_file_name = cmd_parser.get<string>("in"); // path to input image
|
||||
|
||||
if (cmd_parser.has("out"))
|
||||
{
|
||||
std::string fpath = cmd_parser.get<string>("out"); // path to output image
|
||||
std::string::size_type idx = fpath.rfind('.');
|
||||
if (idx != std::string::npos)
|
||||
{
|
||||
g_out_file_name = fpath.substr(0, idx);
|
||||
g_out_file_ext = fpath.substr(idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_out_file_name = fpath;
|
||||
g_out_file_ext = ".png";
|
||||
}
|
||||
}
|
||||
if (!cmd_parser.check())
|
||||
{
|
||||
cmd_parser.printErrors();
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_modeMultiQR = cmd_parser.has("multi") && cmd_parser.get<bool>("multi");
|
||||
g_detectOnly = cmd_parser.has("detect") && cmd_parser.get<bool>("detect");
|
||||
|
||||
g_saveDetections = cmd_parser.has("save_detections") && cmd_parser.get<bool>("save_detections");
|
||||
g_saveAll = cmd_parser.has("save_all") && cmd_parser.get<bool>("save_all");
|
||||
|
||||
int return_code = 0;
|
||||
if (in_file_name.empty())
|
||||
{
|
||||
return_code = liveQRCodeDetect(out_file_name);
|
||||
return_code = liveQRCodeDetect();
|
||||
}
|
||||
else
|
||||
{
|
||||
return_code = imageQRCodeDetect(samples::findFile(in_file_name), out_file_name);
|
||||
return_code = imageQRCodeDetect(samples::findFile(in_file_name));
|
||||
}
|
||||
return return_code;
|
||||
}
|
||||
|
||||
void drawQRCodeContour(Mat &color_image, vector<Point> transform)
|
||||
static
|
||||
void drawQRCodeContour(Mat &color_image, const vector<Point>& corners)
|
||||
{
|
||||
if (!transform.empty())
|
||||
if (!corners.empty())
|
||||
{
|
||||
double show_radius = (color_image.rows > color_image.cols)
|
||||
? (2.813 * color_image.rows) / color_image.cols
|
||||
@ -61,127 +102,246 @@ void drawQRCodeContour(Mat &color_image, vector<Point> transform)
|
||||
double contour_radius = show_radius * 0.4;
|
||||
|
||||
vector< vector<Point> > contours;
|
||||
contours.push_back(transform);
|
||||
contours.push_back(corners);
|
||||
drawContours(color_image, contours, 0, Scalar(211, 0, 148), cvRound(contour_radius));
|
||||
|
||||
RNG rng(1000);
|
||||
for (size_t i = 0; i < 4; i++)
|
||||
{
|
||||
Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
|
||||
circle(color_image, transform[i], cvRound(show_radius), color, -1);
|
||||
circle(color_image, corners[i], cvRound(show_radius), color, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void drawFPS(Mat &color_image, double fps)
|
||||
{
|
||||
ostringstream convert;
|
||||
convert << cvRound(fps) << " FPS (QR detection)";
|
||||
convert << cv::format("%.2f", fps) << " FPS (" << getQRModeString() << ")";
|
||||
putText(color_image, convert.str(), Point(25, 25), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 0, 255), 2);
|
||||
}
|
||||
|
||||
int liveQRCodeDetect(const string& out_file)
|
||||
static
|
||||
void drawQRCodeResults(Mat& frame, const vector<Point>& corners, const vector<cv::String>& decode_info, double fps)
|
||||
{
|
||||
VideoCapture cap(0);
|
||||
if(!cap.isOpened())
|
||||
if (!corners.empty())
|
||||
{
|
||||
cout << "Cannot open a camera" << endl;
|
||||
return -4;
|
||||
for (size_t i = 0; i < corners.size(); i += 4)
|
||||
{
|
||||
size_t qr_idx = i / 4;
|
||||
vector<Point> qrcode_contour(corners.begin() + i, corners.begin() + i + 4);
|
||||
drawQRCodeContour(frame, qrcode_contour);
|
||||
|
||||
cout << "QR[" << qr_idx << "] @ " << Mat(qrcode_contour).reshape(2, 1) << ": ";
|
||||
if (decode_info.size() > qr_idx)
|
||||
{
|
||||
if (!decode_info[qr_idx].empty())
|
||||
cout << "'" << decode_info[qr_idx] << "'" << endl;
|
||||
else
|
||||
cout << "can't decode QR code" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "decode information is not available (disabled)" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "QR code is not detected" << endl;
|
||||
}
|
||||
|
||||
QRCodeDetector qrcode;
|
||||
TickMeter total;
|
||||
for(;;)
|
||||
drawFPS(frame, fps);
|
||||
}
|
||||
|
||||
static
|
||||
void runQR(
|
||||
QRCodeDetector& qrcode, const Mat& input,
|
||||
vector<Point>& corners, vector<cv::String>& decode_info
|
||||
// +global: bool g_modeMultiQR, bool g_detectOnly
|
||||
)
|
||||
{
|
||||
if (!g_modeMultiQR)
|
||||
{
|
||||
Mat frame, src, straight_barcode;
|
||||
string decode_info;
|
||||
vector<Point> transform;
|
||||
if (!g_detectOnly)
|
||||
{
|
||||
String decode_info1 = qrcode.detectAndDecode(input, corners);
|
||||
decode_info.push_back(decode_info1);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool detection_result = qrcode.detect(input, corners);
|
||||
CV_UNUSED(detection_result);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!g_detectOnly)
|
||||
{
|
||||
bool result_detection = qrcode.detectAndDecodeMulti(input, decode_info, corners);
|
||||
CV_UNUSED(result_detection);
|
||||
}
|
||||
else
|
||||
{
|
||||
bool result_detection = qrcode.detectMulti(input, corners);
|
||||
CV_UNUSED(result_detection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
double processQRCodeDetection(QRCodeDetector& qrcode, const Mat& input, Mat& result, vector<Point>& corners)
|
||||
{
|
||||
if (input.channels() == 1)
|
||||
cvtColor(input, result, COLOR_GRAY2BGR);
|
||||
else
|
||||
input.copyTo(result);
|
||||
|
||||
cout << "Run " << getQRModeString()
|
||||
<< " on image: " << input.size() << " (" << typeToString(input.type()) << ")"
|
||||
<< endl;
|
||||
|
||||
TickMeter timer;
|
||||
|
||||
vector<cv::String> decode_info;
|
||||
timer.start();
|
||||
runQR(qrcode, input, corners, decode_info);
|
||||
timer.stop();
|
||||
|
||||
double fps = 1 / timer.getTimeSec();
|
||||
drawQRCodeResults(result, corners, decode_info, fps);
|
||||
|
||||
return fps;
|
||||
}
|
||||
|
||||
int liveQRCodeDetect()
|
||||
{
|
||||
VideoCapture cap(0);
|
||||
|
||||
if (!cap.isOpened())
|
||||
{
|
||||
cout << "Cannot open a camera" << endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
cout << "Press 'm' to switch between detectAndDecode and detectAndDecodeMulti" << endl;
|
||||
cout << "Press 'd' to switch between decoder and detector" << endl;
|
||||
cout << "Press ' ' (space) to save result into images" << endl;
|
||||
cout << "Press 'ESC' to exit" << endl;
|
||||
QRCodeDetector qrcode;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
Mat frame;
|
||||
cap >> frame;
|
||||
if (frame.empty())
|
||||
{
|
||||
cout << "End of video stream" << endl;
|
||||
break;
|
||||
}
|
||||
cvtColor(frame, src, COLOR_BGR2GRAY);
|
||||
|
||||
total.start();
|
||||
bool result_detection = qrcode.detect(src, transform);
|
||||
if (result_detection)
|
||||
bool forceSave = g_saveAll;
|
||||
|
||||
Mat result;
|
||||
|
||||
try
|
||||
{
|
||||
decode_info = qrcode.decode(src, transform, straight_barcode);
|
||||
if (!decode_info.empty()) { cout << decode_info << endl; }
|
||||
vector<Point> corners;
|
||||
double fps = processQRCodeDetection(qrcode, frame, result, corners);
|
||||
cout << "FPS: " << fps << endl;
|
||||
forceSave |= (g_saveDetections && !corners.empty());
|
||||
//forceSave |= fps < 1.0;
|
||||
}
|
||||
catch (const cv::Exception& e)
|
||||
{
|
||||
cerr << "ERROR exception: " << e.what() << endl;
|
||||
forceSave = true;
|
||||
}
|
||||
total.stop();
|
||||
double fps = 1 / total.getTimeSec();
|
||||
total.reset();
|
||||
|
||||
if (result_detection) { drawQRCodeContour(frame, transform); }
|
||||
drawFPS(frame, fps);
|
||||
if (!result.empty())
|
||||
imshow("QR code", result);
|
||||
|
||||
imshow("Live QR code detector", frame);
|
||||
char c = (char)waitKey(30);
|
||||
int code = waitKey(1);
|
||||
if (code < 0 && !forceSave)
|
||||
continue; // timeout
|
||||
char c = (char)code;
|
||||
if (c == ' ' || forceSave)
|
||||
{
|
||||
string fsuffix = cv::format("-%05d", g_save_idx++);
|
||||
|
||||
string fname_input = g_out_file_name + fsuffix + "_input.png";
|
||||
cout << "Saving QR code detection input: '" << fname_input << "' ..." << endl;
|
||||
imwrite(fname_input, frame);
|
||||
|
||||
string fname = g_out_file_name + fsuffix + g_out_file_ext;
|
||||
cout << "Saving QR code detection result: '" << fname << "' ..." << endl;
|
||||
imwrite(fname, result);
|
||||
|
||||
cout << "Saved" << endl;
|
||||
}
|
||||
if (c == 'm')
|
||||
{
|
||||
g_modeMultiQR = !g_modeMultiQR;
|
||||
cout << "Switching QR code mode ==> " << (g_modeMultiQR ? "detectAndDecodeMulti" : "detectAndDecode") << endl;
|
||||
}
|
||||
if (c == 'd')
|
||||
{
|
||||
g_detectOnly = !g_detectOnly;
|
||||
cout << "Switching QR decoder mode ==> " << (g_detectOnly ? "detect" : "decode") << endl;
|
||||
}
|
||||
if (c == 27)
|
||||
{
|
||||
cout << "'ESC' is pressed. Exiting..." << endl;
|
||||
break;
|
||||
if (c == ' ' && !out_file.empty())
|
||||
imwrite(out_file, frame); // TODO write original frame too
|
||||
}
|
||||
}
|
||||
cout << "Exit." << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int imageQRCodeDetect(const string& in_file, const string& out_file)
|
||||
int imageQRCodeDetect(const string& in_file)
|
||||
{
|
||||
Mat color_src = imread(in_file, IMREAD_COLOR), src;
|
||||
cvtColor(color_src, src, COLOR_BGR2GRAY);
|
||||
Mat straight_barcode;
|
||||
string decoded_info;
|
||||
vector<Point> transform;
|
||||
const int count_experiments = 10;
|
||||
double transform_time = 0.0;
|
||||
bool result_detection = false;
|
||||
TickMeter total;
|
||||
|
||||
Mat input = imread(in_file, IMREAD_COLOR);
|
||||
cout << "Run " << getQRModeString()
|
||||
<< " on image: " << input.size() << " (" << typeToString(input.type()) << ")"
|
||||
<< endl;
|
||||
|
||||
QRCodeDetector qrcode;
|
||||
vector<Point> corners;
|
||||
vector<cv::String> decode_info;
|
||||
|
||||
TickMeter timer;
|
||||
for (size_t i = 0; i < count_experiments; i++)
|
||||
{
|
||||
total.start();
|
||||
transform.clear();
|
||||
result_detection = qrcode.detect(src, transform);
|
||||
total.stop();
|
||||
transform_time += total.getTimeSec();
|
||||
total.reset();
|
||||
if (!result_detection)
|
||||
continue;
|
||||
corners.clear();
|
||||
decode_info.clear();
|
||||
|
||||
total.start();
|
||||
decoded_info = qrcode.decode(src, transform, straight_barcode);
|
||||
total.stop();
|
||||
transform_time += total.getTimeSec();
|
||||
total.reset();
|
||||
timer.start();
|
||||
runQR(qrcode, input, corners, decode_info);
|
||||
timer.stop();
|
||||
}
|
||||
double fps = count_experiments / transform_time;
|
||||
if (!result_detection)
|
||||
cout << "QR code not found" << endl;
|
||||
if (decoded_info.empty())
|
||||
cout << "QR code cannot be decoded" << endl;
|
||||
|
||||
drawQRCodeContour(color_src, transform);
|
||||
drawFPS(color_src, fps);
|
||||
|
||||
cout << "Input image file path: " << in_file << endl;
|
||||
cout << "Output image file path: " << out_file << endl;
|
||||
cout << "Size: " << color_src.size() << endl;
|
||||
double fps = count_experiments / timer.getTimeSec();
|
||||
cout << "FPS: " << fps << endl;
|
||||
cout << "Decoded info: " << decoded_info << endl;
|
||||
|
||||
if (!out_file.empty())
|
||||
Mat result; input.copyTo(result);
|
||||
drawQRCodeResults(result, corners, decode_info, fps);
|
||||
|
||||
imshow("QR", result); waitKey(1);
|
||||
|
||||
if (!g_out_file_name.empty())
|
||||
{
|
||||
imwrite(out_file, color_src);
|
||||
string out_file = g_out_file_name + g_out_file_ext;
|
||||
cout << "Saving result: " << out_file << endl;
|
||||
imwrite(out_file, result);
|
||||
}
|
||||
|
||||
for(;;)
|
||||
{
|
||||
imshow("Detect QR code on image", color_src);
|
||||
if (waitKey(0) == 27)
|
||||
break;
|
||||
}
|
||||
cout << "Press any key to exit ..." << endl;
|
||||
waitKey(0);
|
||||
cout << "Exit." << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user