Skip to content

Commit 0cc5e82

Browse files
committed
Make decodeImageFilesToJson returning a json list instead of a list of tuples of origin/json
1 parent b3acb29 commit 0cc5e82

5 files changed

Lines changed: 38 additions & 14 deletions

File tree

source/lib/api/include/DecoderFacade.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,17 @@ namespace api
146146

147147
/* Barcodes from image or PDF input file/directory to json, raw byte-array or raw base64-string
148148
*/
149-
std::vector<std::pair<std::string, std::string>> decodeImageFilesToJson(std::filesystem::path path);
149+
std::vector<std::pair<std::string, std::string>> decodeImageFilesToJson(
150+
std::filesystem::path path,
151+
std::optional<dip::PreProcessorOptions> preProcessorOptions = std::nullopt);
150152

151-
std::vector<std::pair<std::string, std::vector<std::uint8_t>>> decodeImageFilesToRawBytes(std::filesystem::path path);
153+
std::vector<std::pair<std::string, std::vector<std::uint8_t>>> decodeImageFilesToRawBytes(
154+
std::filesystem::path path,
155+
std::optional<dip::PreProcessorOptions> preProcessorOptions = std::nullopt);
152156

153-
std::vector<std::pair<std::string, std::string>> decodeImageFilesToRawBase64(std::filesystem::path path);
157+
std::vector<std::pair<std::string, std::string>> decodeImageFilesToRawBase64(
158+
std::filesystem::path path,
159+
std::optional<dip::PreProcessorOptions> preProcessorOptions = std::nullopt);
154160

155161
/* Pre-loaded image data as input-element to json
156162
*/

source/lib/api/source/DecoderFacade.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,23 +463,29 @@ namespace api
463463
return decodeRawBytesToJson(utility::base64::decode(base64RawData), origin);
464464
}
465465

466-
std::vector<std::pair<std::string, std::string>> DecoderFacade::decodeImageFilesToJson(std::filesystem::path path)
466+
std::vector<std::pair<std::string, std::string>> DecoderFacade::decodeImageFilesToJson(
467+
std::filesystem::path path,
468+
std::optional<dip::PreProcessorOptions> preProcessorOptions)
467469
{
468470
auto result = std::vector<std::pair<std::string, std::string>>{};
469471
decodeImageFiles<decoder::api::Result>(path, [&](auto &&decoderResult, auto origin)
470472
{ result.emplace_back(std::make_pair(std::move(origin), interpretRawBytes(std::move(decoderResult.payload), origin))); });
471473
return result;
472474
}
473475

474-
std::vector<std::pair<std::string, std::vector<std::uint8_t>>> DecoderFacade::decodeImageFilesToRawBytes(std::filesystem::path path)
476+
std::vector<std::pair<std::string, std::vector<std::uint8_t>>> DecoderFacade::decodeImageFilesToRawBytes(
477+
std::filesystem::path path,
478+
std::optional<dip::PreProcessorOptions> preProcessorOptions)
475479
{
476480
auto result = std::vector<std::pair<std::string, std::vector<std::uint8_t>>>{};
477481
decodeImageFiles<decoder::api::Result>(path, [&](auto &&decoderResult, auto origin)
478482
{ result.emplace_back(std::make_pair(std::move(origin), std::move(decoderResult.payload))); });
479483
return result;
480484
}
481485

482-
std::vector<std::pair<std::string, std::string>> DecoderFacade::decodeImageFilesToRawBase64(std::filesystem::path path)
486+
std::vector<std::pair<std::string, std::string>> DecoderFacade::decodeImageFilesToRawBase64(
487+
std::filesystem::path path,
488+
std::optional<dip::PreProcessorOptions> preProcessorOptions)
483489
{
484490
auto result = std::vector<std::pair<std::string, std::string>>{};
485491
decodeImageFiles<decoder::api::Result>(path, [&](auto &&decoderResult, auto origin)

source/python/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
PROJECT(ticket_decoder)
55

6+
find_package(Boost REQUIRED COMPONENTS headers)
7+
68
# nanobind is installed via pip on python side and ships the cpp files with the python module,
79
# so no additional conan packages required. We only have to tell cmake to find those files.
810
#
@@ -42,7 +44,8 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
4244
easyloggingpp::easyloggingpp
4345
opencv::opencv_core_alias
4446
ticket-decoder-api
45-
ticket-decoder-dip)
47+
ticket-decoder-dip
48+
Boost::headers)
4649

4750
IF (SKBUILD_PLATLIB_DIR)
4851
install(FILES ${PROJECT_SOURCE_DIR}/etc/__init__.py DESTINATION ${SKBUILD_PROJECT_NAME})

source/python/source/Binding.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010

1111
#include "lib/api/include/DecoderFacade.h"
1212

13+
#include <boost/algorithm/string.hpp>
14+
1315
#include <memory>
1416
#include <string>
17+
#include <sstream>
18+
#include <ranges>
1519

1620
class DecoderFacadeWrapper
1721
{
@@ -72,15 +76,21 @@ class DecoderFacadeWrapper
7276
return get().decodeRawBase64ToJson(base64, origin);
7377
}
7478

75-
std::vector<std::pair<std::string, std::string>> decodeFiles(
79+
std::string decodeFiles(
7680
std::string const &path,
7781
int const rotationDegree,
7882
unsigned int const scalePercent,
7983
std::string const &splittingMode,
8084
unsigned int const flippingMode)
8185
{
82-
auto options = dip::PreProcessorOptions{rotationDegree, scalePercent, splittingMode, flippingMode};
83-
return get().decodeImageFilesToJson(path);
86+
auto preProcessorOptions = std::make_optional(dip::PreProcessorOptions{rotationDegree, scalePercent, splittingMode, flippingMode});
87+
auto result = get().decodeImageFilesToJson(path, std::move(preProcessorOptions))
88+
| std::views::transform([](auto &&pair) { return std::move(pair.second); });
89+
90+
auto resultJson = std::vector<std::string>(std::begin(result), std::end(result));
91+
auto json = std::stringstream();
92+
json << "[" << boost::algorithm::join(resultJson, ",\n") << "]";
93+
return json.str();
8494
}
8595
};
8696

source/test/python/test_decode_uic918.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_decode_pdf_files(self):
3535
if not len(result):
3636
self.skipTest("Poppler input support not compiled in: " + self.pdf_input_file)
3737

38-
records = loads(result[0][1])
38+
records = loads(result)[0]
3939
assert records['records']['U_FLEX']['transportDocuments'][0]['openTicket']['fromStationName'] == 'Kassel+City'
4040
assert records['validated'] == 'false'
4141

@@ -46,11 +46,10 @@ def test_decode_image_files(self):
4646
decoder_facade = DecoderFacade()
4747
result = decoder_facade.decode_files(self.image_input_file)
4848

49-
records = loads(result[0][1])
49+
records = loads(result)[0]
5050
assert records['records']['U_FLEX']['transportDocuments'][0]['openTicket']['tariffs'][0]['tariffDesc'] == 'Deutschland-Ticket'
5151
assert records['validated'] == 'false'
5252

53-
5453
def test_decode_files_not_existing(self):
5554
with self.assertRaisesRegex(RuntimeError, '^Decoding failed with: Path to load input elements from does not exist: Not existing file$'):
5655
decoder_facade = DecoderFacade()
@@ -59,7 +58,7 @@ def test_decode_files_not_existing(self):
5958
def test_decode_files_without_aztec_code(self):
6059
decoder_facade = DecoderFacade()
6160
result = decoder_facade.decode_files('source/test/io/etc/minimal.pdf')
62-
assert len(result) == 0
61+
assert len(loads(result)) == 0
6362

6463
def test_two_instances(self):
6564
decoder_facadeA = DecoderFacade()

0 commit comments

Comments
 (0)