Skip to content

Commit ee28868

Browse files
Qrreader changes (#371)
* 1. upgraded the zxing library 2. Added 4 props in QR Reader module - tryHarder - saveQRImages - qrImagesPath - frameRotationCounter 3. Added loop test for QR module * checked out to v2.2.1 for zxing submodule and some minor changes in Readme file. * added some warning logs and trycatch block to handle some exception * changed submodule zxing-cpp to Apra-Labs/zxing-cpp
1 parent b84edfc commit ee28868

File tree

6 files changed

+147
-21
lines changed

6 files changed

+147
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ git clone --recursive https://github.com/Apra-Labs/ApraPipes.git
1313

1414
## Prerequisites
1515
* Run ```sudo apt-get update && sudo apt-get install build-essential``` to get latest build tools
16-
* CMake minimum version 3.14 - Follow [this article](https://anglehit.com/how-to-install-the-latest-version-of-cmake-via-command-line/) to update cmake
16+
* CMake minimum version 3.17 - Follow [this article](https://anglehit.com/how-to-install-the-latest-version-of-cmake-via-command-line/) to update cmake
1717
* ffmpeg
1818
```
1919
sudo apt install yasm -y

base/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ IF(ENABLE_ARM64)
2626
ENDIF(ENABLE_ARM64)
2727

2828
project(APRAPIPES)
29-
set(CMAKE_CXX_STANDARD 14)
29+
set(CMAKE_CXX_STANDARD 17)
3030

3131
hunter_add_package(Boost COMPONENTS system thread filesystem serialization log chrono)
3232
find_package(Boost CONFIG REQUIRED system thread filesystem serialization log chrono)
@@ -60,13 +60,13 @@ find_library(LIB_AVFORMAT libavformat.a PATHS ${FFMPEG_ROOT}/libavformat NO_DEFA
6060
find_library(LIB_AVCODEC libavcodec.a PATHS ${FFMPEG_ROOT}/libavcodec NO_DEFAULT_PATH)
6161
find_library(LIB_AVUTIL libavutil.a PATHS ${FFMPEG_ROOT}/libavutil NO_DEFAULT_PATH)
6262
find_library(LIB_FFSWR libswresample.a PATHS ${FFMPEG_ROOT}/libswresample NO_DEFAULT_PATH)
63-
find_library(LIB_ZXING libZXing.a PATHS ../thirdparty/zxing-cpp/_install/lib NO_DEFAULT_PATH)
63+
find_library(LIB_ZXING libZXing.a PATHS ../thirdparty/zxing-cpp/_build/zxing-cpp.release/core NO_DEFAULT_PATH)
6464

6565

6666
IF(ENABLE_CUDA)
6767
IF(ENABLE_ARM64)
6868
SET(NVCODEC_LIB)
69-
SET ( JETSON_MULTIMEDIA_LIB_INCLUDE "/usr/src/jetson_multimedia_api/include" )
69+
SET ( JETSON_MULTIMEDIA_LIB_INCLUDE "/usr/src/jetson_multimedia_api/include" )
7070

7171
list(APPEND CMAKE_PREFIX_PATH /usr/lib/aarch64-linux-gnu/tegra)
7272
list(APPEND CMAKE_PREFIX_PATH /usr/lib/aarch64-linux-gnu/tegra-egl)
@@ -117,7 +117,7 @@ include_directories(AFTER SYSTEM include
117117
../thirdparty/Video_Codec_SDK_10.0.26/Interface
118118
../thirdparty/ffmpeg/include
119119
${CURSES_INCLUDE_DIR}
120-
../thirdparty/zxing-cpp/_install/include/ZXing
120+
../thirdparty/zxing-cpp/core/src
121121
)
122122

123123
# ApraPipes library

base/include/QRReader.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@
22

33
#include "Module.h"
44
#include "ReadBarcode.h"
5+
#include "TextUtfEncoding.h"
56

67
class QRReaderProps : public ModuleProps
78
{
89
public:
9-
QRReaderProps() : ModuleProps() {}
10+
QRReaderProps(bool _tryHarder = false, bool _saveQRImages = false, string _qrImagesPath = "", int _frameRotationCounter = 10) : ModuleProps()
11+
{
12+
tryHarder = _tryHarder;
13+
saveQRImages = _saveQRImages;
14+
qrImagesPath = _qrImagesPath;
15+
frameRotationCounter = _frameRotationCounter;
16+
}
17+
18+
size_t getSerializeSize()
19+
{
20+
return ModuleProps::getSerializeSize();
21+
}
22+
bool tryHarder;
23+
bool saveQRImages;
24+
string qrImagesPath;
25+
int frameRotationCounter;
1026
};
1127

1228
class QRReader : public Module

base/src/QRReader.cpp

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,28 @@
33
#include "FrameMetadataFactory.h"
44
#include "Frame.h"
55
#include "Logger.h"
6-
#include "ReadBarcode.h"
7-
#include "TextUtfEncoding.h"
6+
#include "RawImageMetadata.h"
7+
#include <boost/filesystem.hpp>
8+
namespace fs = boost::filesystem;
89

910
class QRReader::Detail
1011
{
1112

1213
public:
13-
Detail() : mWidth(0), mHeight(0)
14+
Detail(QRReaderProps _props) : mWidth(0), mHeight(0)
1415
{
15-
mHints.setEanAddOnSymbol(ZXing::EanAddOnSymbol::Read);
16+
mReaderOptions.setEanAddOnSymbol(ZXing::EanAddOnSymbol::Read);
17+
LOG_INFO << "Setting tryHarder as " << _props.tryHarder;
18+
mReaderOptions.setTryHarder(_props.tryHarder);
19+
mSaveQRImages = _props.saveQRImages;
20+
mQRImagesFolderName = _props.qrImagesPath;
21+
mFrameRotationCounter = _props.frameRotationCounter;
22+
if (mFrameRotationCounter <= 0)
23+
{
24+
LOG_WARNING << "You are setting frameRotationCounter less than 1";
25+
mFrameRotationCounter = 1;
26+
}
27+
mFrameCounter = 0;
1628
}
1729

1830
~Detail() {}
@@ -50,17 +62,18 @@ class QRReader::Detail
5062

5163
int mWidth;
5264
int mHeight;
53-
ZXing::DecodeHints mHints;
65+
ZXing::ReaderOptions mReaderOptions;
5466
std::string mOutputPinId;
5567
ZXing::ImageFormat mImageFormat;
56-
57-
private:
58-
framemetadata_sp mMetadata;
68+
bool mSaveQRImages;
69+
fs::path mQRImagesFolderName;
70+
int mFrameCounter;
71+
int mFrameRotationCounter;
5972
};
6073

6174
QRReader::QRReader(QRReaderProps _props) : Module(TRANSFORM, "QRReader", _props)
6275
{
63-
mDetail.reset(new Detail());
76+
mDetail.reset(new Detail(_props));
6477
auto metadata = framemetadata_sp(new FrameMetadata(FrameMetadata::GENERAL));
6578
mDetail->mOutputPinId = addOutputPin(metadata);
6679
}
@@ -111,7 +124,15 @@ bool QRReader::init()
111124
{
112125
return false;
113126
}
114-
127+
boost::system::error_code ec;
128+
if (mDetail->mSaveQRImages && (!fs::create_directories(mDetail->mQRImagesFolderName, ec)))
129+
{
130+
if (ec)
131+
{
132+
LOG_ERROR << "Failed to create directory: " << mDetail->mQRImagesFolderName << ". Error: " << ec.message();
133+
mDetail->mQRImagesFolderName = "";
134+
}
135+
}
115136
return true;
116137
}
117138

@@ -124,10 +145,40 @@ bool QRReader::process(frame_container &frames)
124145
{
125146
auto frame = frames.begin()->second;
126147

127-
const auto &result = ZXing::ReadBarcode({static_cast<uint8_t *>(frame->data()), mDetail->mWidth, mDetail->mHeight, mDetail->mImageFormat}, mDetail->mHints);
128-
129-
auto text = ZXing::TextUtfEncoding::ToUtf8(result.text());
130-
148+
const auto &result = ZXing::ReadBarcode({static_cast<uint8_t *>(frame->data()), mDetail->mWidth, mDetail->mHeight, mDetail->mImageFormat}, mDetail->mReaderOptions);
149+
150+
auto text = result.text();
151+
if (text.length())
152+
{
153+
LOG_INFO << "ZXING decoded QR: " << text;
154+
}
155+
156+
if (mDetail->mSaveQRImages && (mDetail->mQRImagesFolderName != ""))
157+
{
158+
fs::path savePath = mDetail->mQRImagesFolderName / (std::to_string(mDetail->mFrameCounter) + ".raw");
159+
try
160+
{
161+
std::ofstream outFile(savePath.string(), std::ios::binary);
162+
if (outFile)
163+
{
164+
outFile.write(static_cast<char *>(frame->data()), frame->size());
165+
outFile.close();
166+
}
167+
else
168+
{
169+
LOG_ERROR << "Failed to save frame to " << savePath.string();
170+
}
171+
}
172+
catch (const std::exception &e)
173+
{
174+
LOG_ERROR << "Exception caught while saving frame to " << savePath.string() << ": " << e.what() << std::endl;
175+
}
176+
mDetail->mFrameCounter++;
177+
if ((mDetail->mFrameCounter % mDetail->mFrameRotationCounter) == 0)
178+
{
179+
mDetail->mFrameCounter = 0;
180+
}
181+
}
131182
auto outFrame = makeFrame(text.length(), mDetail->mOutputPinId);
132183
memcpy(outFrame->data(), text.c_str(), outFrame->size());
133184
frames.insert(make_pair(mDetail->mOutputPinId, outFrame));

base/test/QRReader_tests.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#include "Logger.h"
1010
#include "AIPExceptions.h"
1111
#include "QRReader.h"
12+
#include "PipeLine.h"
13+
#include "RotateCV.h"
14+
#include "VirtualCameraSink.h"
15+
16+
#define VIDEO_DEV "/dev/video99"
1217

1318
BOOST_AUTO_TEST_SUITE(QRReader_tests)
1419

@@ -39,6 +44,34 @@ BOOST_AUTO_TEST_CASE(rgb)
3944
BOOST_TEST(expectedOutput == actualOutput);
4045
}
4146

47+
BOOST_AUTO_TEST_CASE(test)
48+
{
49+
FileReaderModuleProps fileReaderProps("./data/qrData/h.raw");
50+
fileReaderProps.readLoop = true;
51+
auto fileReader = boost::shared_ptr<FileReaderModule>(new FileReaderModule(fileReaderProps));
52+
auto metadata = framemetadata_sp(new RawImageMetadata(720, 1280, ImageMetadata::ImageType::RGB, CV_8UC3, 0, CV_8U, FrameMetadata::HOST, true));
53+
fileReader->addOutputPin(metadata);
54+
55+
QRReaderProps qrReaderProps(true,true,"./data",10);
56+
auto QRData = boost::shared_ptr<QRReader>(new QRReader(qrReaderProps));
57+
fileReader->setNext(QRData);
58+
59+
auto sink = boost::shared_ptr<ExternalSinkModule>(new ExternalSinkModule());
60+
QRData->setNext(sink);
61+
62+
BOOST_TEST(fileReader->init());
63+
BOOST_TEST(QRData->init());
64+
BOOST_TEST(sink->init());
65+
66+
fileReader->step();
67+
QRData->step();
68+
auto frames = sink->pop();
69+
BOOST_TEST(frames.size() == 1);
70+
auto outputFrame = frames.cbegin()->second;
71+
std::string expectedOutput = "0005100788";
72+
auto actualOutput = std::string(const_cast<const char*>( static_cast<char*>(outputFrame->data()) ), outputFrame->size() );
73+
}
74+
4275
BOOST_AUTO_TEST_CASE(yuv420)
4376
{
4477
Logger::setLogLevel(boost::log::trivial::severity_level::trace);
@@ -66,5 +99,31 @@ BOOST_AUTO_TEST_CASE(yuv420)
6699
BOOST_TEST(expectedOutput == actualOutput);
67100
}
68101

102+
BOOST_AUTO_TEST_CASE(readLoop)
103+
{
104+
Logger::setLogLevel(boost::log::trivial::severity_level::trace);
105+
FileReaderModuleProps fileReaderProps("./data/qrData/h.raw");
106+
fileReaderProps.readLoop = true;
107+
auto fileReader = boost::shared_ptr<FileReaderModule>(new FileReaderModule(fileReaderProps));
108+
auto metadata = framemetadata_sp(new RawImageMetadata(720, 1280, ImageMetadata::ImageType::RGB, CV_8UC3, 0, CV_8U, FrameMetadata::HOST, true));
109+
fileReader->addOutputPin(metadata);
110+
111+
auto m1 = boost::shared_ptr<RotateCV>(new RotateCV(RotateCVProps(90)));
112+
fileReader->setNext(m1);
113+
114+
QRReaderProps qrReaderProps(true, true, "./data", 10);
115+
auto QRData = boost::shared_ptr<QRReader>(new QRReader(qrReaderProps));
116+
m1->setNext(QRData);
117+
118+
119+
auto p = boost::shared_ptr<PipeLine>(new PipeLine("test"));
120+
p->appendModule(fileReader);
121+
p->init();
122+
p->run_all_threaded();
123+
boost::this_thread::sleep_for(boost::chrono::seconds(60));
124+
125+
p->wait_for_all(true);
126+
}
127+
69128

70129
BOOST_AUTO_TEST_SUITE_END()

thirdparty/zxing-cpp

Submodule zxing-cpp updated 1124 files

0 commit comments

Comments
 (0)