5#include <QFutureWatcher>
7#include <tesseract/baseapi.h>
8#include <leptonica/allheaders.h>
9#include "OCRLanguageModel.h"
10#if TESSERACT_MAJOR_VERSION < 5
11#include <tesseract/strngs.h>
12#include <tesseract/genericvector.h>
41 ,m_tesseract(new tesseract::TessBaseAPI())
42 ,m_languages(new OCRLanguageModel(this))
43 ,m_boxesTypes(BoxType::Word | BoxType::Line | BoxType::Paragraph)
45 std::vector<std::string> availableLanguages;
46#if TESSERACT_MAJOR_VERSION < 5
47 GenericVector<STRING> languageVector;
48 m_tesseract->GetAvailableLanguagesAsVector(&languageVector);
49 for (
int i = 0; i < languageVector.size(); i++) {
50 availableLanguages.push_back(languageVector[i].c_str());
53 m_tesseract->GetAvailableLanguagesAsVector(&availableLanguages);
56 m_languages->setLanguages(availableLanguages);
69QRect OCS::area()
const
74bool OCS::autoRead()
const
79void OCS::setAutoRead(
bool value)
81 if(m_autoRead == value)
88void OCS::setBoxesType(OCS::BoxesType types)
90 if(m_boxesTypes == types)
95 qDebug() <<
"Setting the boxes types" << m_boxesTypes << types;
100void OCS::getTextAsync()
104 qDebug() <<
"URL is not local :: OCR";
107 typedef QMap<BoxType, TextBoxes> Res;
108 auto func = [](QUrl url, BoxesType levels) -> Res
111 tesseract::TessBaseAPI *api =
new tesseract::TessBaseAPI();
112 api->Init(NULL,
"eng");
113 api->SetImage(image);
116 TextBoxes wordBoxes, lineBoxes, paragraphBoxes;
118 auto levelFunc = [](tesseract::TessBaseAPI *api, tesseract::PageIteratorLevel
level) -> TextBoxes
121 tesseract::ResultIterator* ri = api->GetIterator();
124 qDebug() <<
"Getting text for level" <<
level;
127 const char* word = ri->GetUTF8Text(level);
128 float conf = ri->Confidence(level);
130 ri->BoundingBox(level, &x1, &y1, &x2, &y2);
132 printf(
"word: '%s'; \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n",
133 word, conf, x1, y1, x2, y2);
138 }
while (ri->Next(level));
144 if(levels.testFlag(Word))
145 wordBoxes = levelFunc(api, tesseract::RIL_WORD);
147 if(levels.testFlag(Line))
148 lineBoxes = levelFunc(api, tesseract::RIL_TEXTLINE);
150 if(levels.testFlag(Paragraph))
151 paragraphBoxes = levelFunc(api, tesseract::RIL_PARA);
155 return Res{{Word, wordBoxes}, {Line, lineBoxes}, {Paragraph, paragraphBoxes}};
158 auto watcher =
new QFutureWatcher<Res>;
162 m_wordBoxes = watcher->result()[Word];
163 m_lineBoxes = watcher->result()[Line];
164 m_paragraphBoxes = watcher->result()[Paragraph];
165 Q_EMIT wordBoxesChanged();
166 Q_EMIT lineBoxesChanged();
167 Q_EMIT paragraphBoxesChanged();
168 watcher->deleteLater();
171 qDebug() <<
"GEtting text for boxes " << m_boxesTypes << m_boxesTypes.testFlag(Word);
173 watcher->setFuture(future);
181 qDebug() <<
"URL is not local :: OCR";
185 if (m_tesseract->Init(
nullptr, m_languages->getLanguagesString().c_str()))
187 qDebug() <<
"Failed tesseract OCR init";
191 m_tesseract->SetPageSegMode(tesseract::PSM_AUTO);
195 if(!m_area.isEmpty())
198 img = img.copy(m_area);
201 m_tesseract->SetImage(img.bits(), img.width(), img.height(), 4, img.bytesPerLine());
206 m_tesseract->SetImage(im);
214void OCS::setFilePath(
QString filePath)
216 if (m_filePath == filePath)
219 m_filePath = filePath;
220 Q_EMIT filePathChanged(m_filePath);
223void OCS::setArea(
QRect area)
229 Q_EMIT areaChanged(m_area);
233TextBoxes OCS::wordBoxes()
const
238TextBoxes OCS::paragraphBoxes()
const
240 return m_paragraphBoxes;
243TextBoxes OCS::lineBoxes()
const
248OCS::BoxesType OCS::boxesType()
253void OCS::classBegin()
257void OCS::componentComplete()
259 qDebug() <<
"OCS CALSS COMPLETED IN QML";
260 connect(
this, &OCS::filePathChanged, [
this](QString)
QStringView level(QStringView ifopt)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromStdString(const std::string &str)
std::string toStdString() const const
QFuture< T > run(Function function,...)
bool isLocalFile() const const
QString toLocalFile() const const