Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support training without lstmf files #4215

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/ccstruct/imagedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@

#include <allheaders.h> // for pixDestroy, pixGetHeight, pixGetWidth, lept_...

#include <cinttypes> // for PRId64
#include <cinttypes> // for PRId64
#include <fstream> // for std::ifstream

namespace tesseract {

Expand Down Expand Up @@ -546,6 +547,31 @@ bool DocumentData::ReCachePages() {
delete page;
}
pages_.clear();
#if !defined(TESSERACT_IMAGEDATA_AS_PIX)
stweil marked this conversation as resolved.
Show resolved Hide resolved
auto name_size = document_name_.size();
if (name_size > 4 && document_name_.substr(name_size - 4) == ".png") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use to use std::filesystem::path as in

if (filePath.extension() == ".box") {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path refactoring should be done as more general procedure. It is ok to keep std::string in very local code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would you prefer using std::filesystem::path::extension() here or not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok as is. fs::paths can be refactored for 6.0 wit api changes/breakage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@egorpugin: document_name_ is not part of tesseract API - it is the private object used in imagedata.cpp only, So I do not think it will break something or change API.

@stweil: I am fine with the current PR code, just I think we should step by step use c++17 features.

// PNG image given instead of LSTMF file.
std::string gt_name = document_name_.substr(0, name_size - 3) + "gt.txt";
std::ifstream t(gt_name);
std::string line;
std::getline(t, line);
t.close();
ImageData *image_data = ImageData::Build(document_name_.c_str(), 0, "", nullptr, 0, line.c_str(), nullptr);
Image image = pixRead(document_name_.c_str());
image_data->SetPix(image);
pages_.push_back(image_data);
loaded_pages = 1;
pages_offset_ %= loaded_pages;
set_total_pages(loaded_pages);
set_memory_used(memory_used() + image_data->MemoryUsed());
#if 0
tprintf("Loaded %zu/%d lines (%d-%zu) of document %s\n", pages_.size(),
loaded_pages, pages_offset_ + 1, pages_offset_ + pages_.size(),
document_name_.c_str());
#endif
return !pages_.empty();
}
#endif
TFile fp;
if (!fp.Open(document_name_.c_str(), reader_) ||
!fp.DeSerializeSize(&loaded_pages) || loaded_pages <= 0) {
Expand Down
Loading