Remove unused tesseract engines after some time

This commit is contained in:
Gres 2020-04-07 20:35:32 +03:00
parent 5397521c79
commit d931015d6b
2 changed files with 20 additions and 0 deletions

View File

@ -33,6 +33,9 @@ void RecognizeWorker::handle(const TaskPtr &task)
if (result->recognized.isEmpty())
result->error = engine->error();
lastGenerations_[task->sourceLanguage] = task->generation;
removeUnused(task->generation);
emit finished(result);
}
@ -44,3 +47,17 @@ void RecognizeWorker::reset(const QString &tessdataPath)
tessdataPath_ = tessdataPath;
engines_.clear();
}
void RecognizeWorker::removeUnused(Generation current)
{
const auto keepGenerations = 10;
for (auto it = lastGenerations_.begin(), end = lastGenerations_.end();
it != end;) {
if (current - it->second < keepGenerations) {
++it;
continue;
}
engines_.erase(it->first);
it = lastGenerations_.erase(it);
}
}

View File

@ -19,6 +19,9 @@ signals:
void finished(const TaskPtr &task);
private:
void removeUnused(Generation current);
std::map<QString, std::unique_ptr<Tesseract>> engines_;
std::map<QString, Generation> lastGenerations_;
QString tessdataPath_;
};