27 std::vector<uint8_t> bytes;
31 std::ifstream ifs(req.
imagePath, std::ios::binary);
32 if (ifs) bytes.assign((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
38 out.
text = textDto.text;
39 std::ostringstream oss;
40 for (
const auto &w : words) {
41 oss << w.text <<
"\t" << w.bbox.x <<
"\t" << w.bbox.y <<
"\t" << w.bbox.width <<
"\t" << w.bbox.height <<
"\t" << w.confidence <<
"\n";
45 out.
words.reserve(words.size());
46 for (
const auto& w : words) {
54 out.
words.push_back(wr);
64 int cx = w.
x + w.
width / 2;
66 return cx >= c.x && cx < (c.x + c.width) && cy >= c.y && cy < (c.y + c.height);
74 for (
auto& cell : t.
cells) {
76 std::vector<WRef> cellWords;
77 cellWords.reserve(out.
words.size() / 4);
78 for (
const auto& w : out.
words) {
79 if (!inCell(w.bbox, cell.bbox))
continue;
80 cellWords.push_back(WRef{ &w, w.bbox.
y + w.bbox.
height / 2 });
83 if (cellWords.empty()) {
89 std::sort(cellWords.begin(), cellWords.end(), [](
const WRef& a,
const WRef& b) {
90 if (a.cy != b.cy) return a.cy < b.cy;
91 return a.w->bbox.x < b.w->bbox.x;
95 for (
const auto& cw : cellWords) avgH += cw.w->bbox.height;
96 avgH = std::max(1, avgH /
static_cast<int>(cellWords.size()));
97 int lineTol = std::max(4, avgH / 2);
99 std::vector<std::vector<const core::ports::text_recognition::Word*>> lines;
100 std::vector<int> lineY;
102 for (
const auto& cw : cellWords) {
103 if (lines.empty() || std::abs(cw.cy - lineY.back()) > lineTol) {
104 lines.push_back({ cw.w });
105 lineY.push_back(cw.cy);
107 lines.back().push_back(cw.w);
108 lineY.back() = (lineY.back() + cw.cy) / 2;
112 std::ostringstream text;
116 for (
size_t li = 0; li < lines.size(); ++li) {
117 auto& ln = lines[li];
118 std::sort(ln.begin(), ln.end(), [](
const auto* a,
const auto* b) { return a->bbox.x < b->bbox.x; });
119 for (
size_t wi = 0; wi < ln.size(); ++wi) {
121 text << ln[wi]->text;
122 sumConf += ln[wi]->confidence;
125 if (li + 1 < lines.size()) text <<
"\n";
128 cell.text = text.str();
129 cell.confidence = n > 0 ? (sumConf / n) : 0;
132 out.
tables.push_back(std::move(t));