MauiKit Image Tools

ocs.cpp
1#include "ocs.h"
2#include <QImage>
3#include <QDebug>
4
5#include <tesseract/baseapi.h>
6#include <leptonica/allheaders.h>
7#include "OCRLanguageModel.h"
8#if TESSERACT_MAJOR_VERSION < 5
9#include <tesseract/strngs.h>
10#include <tesseract/genericvector.h>
11#endif
12
13//static cv::Mat qimageToMatRef(QImage &img, int format)
14// {
15// return cv::Mat(img.height(),
16// img.width(),
17// format,
18// img.bits(),
19// static_cast<size_t>(img.bytesPerLine()));
20// }
21// static cv::Mat qimageToMat(QImage img, int format)
22// {
23// return cv::Mat(img.height(),
24// img.width(),
25// format,
26// img.bits(),
27// static_cast<size_t>(img.bytesPerLine()));
28// }
29// static QImage matToQimageRef(cv::Mat &mat, QImage::Format format)
30// {
31// return QImage(mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), format);
32// }
33// static QImage matToQimage(cv::Mat mat, QImage::Format format)
34// {
35// return QImage(mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), format);
36// }
37
38OCS::OCS(QObject *parent) : QObject(parent)
39 ,m_tesseract(new tesseract::TessBaseAPI())
40 ,m_languages(new OCRLanguageModel(this))
41{
42 if (m_tesseract->Init(nullptr, "eng"))
43 {
44 qDebug()<<"Failed tesseract OCR init";
45 return;
46 }
47 m_tesseract->SetPageSegMode(tesseract::PSM_AUTO);
48
49 std::vector<std::string> availableLanguages;
50#if TESSERACT_MAJOR_VERSION < 5
51 GenericVector<STRING> languageVector;
52 m_tesseract->GetAvailableLanguagesAsVector(&languageVector);
53 for (int i = 0; i < languageVector.size(); i++) {
54 availableLanguages.push_back(languageVector[i].c_str());
55 }
56#else
57 m_tesseract->GetAvailableLanguagesAsVector(&availableLanguages);
58#endif
59
60 m_languages->setLanguages(availableLanguages);
61}
62
63OCS::~OCS()
64{
65 m_tesseract->End();
66}
67
68QString OCS::filePath() const
69{
70 return m_filePath;
71}
72
73QRect OCS::area() const
74{
75 return m_area;
76}
77
78QString OCS::getText()
79{
80 QUrl url(QUrl::fromUserInput(m_filePath));
81 if(!url.isLocalFile())
82 {
83 qDebug() << "URL is not local :: OCR";
84 return "Error!";
85 }
86
87 if (m_tesseract->Init(nullptr, m_languages->getLanguagesString().c_str()))
88 {
89 qDebug() << "Failed tesseract OCR init";
90 return "Error!";
91 }
92
93 m_tesseract->SetPageSegMode(tesseract::PSM_AUTO);
94
95 QString outText;
96
97
98
99 if(!m_area.isEmpty())
100 {
101 QImage img(url.toLocalFile());
102
103 img = img.copy(m_area);
104 // img = img.convertToFormat(QImage::Format_Grayscale8);
105
106 m_tesseract->SetImage(img.bits(), img.width(), img.height(), 4, img.bytesPerLine());
107
108 }else
109 {
110 Pix* im = pixRead(url.toLocalFile().toStdString().c_str());
111 m_tesseract->SetImage(im);
112
113 }
114
115 outText = QString::fromStdString(m_tesseract->GetUTF8Text());
116
117 return outText;
118}
119
120void OCS::setFilePath(QString filePath)
121{
122 if (m_filePath == filePath)
123 return;
124
125 m_filePath = filePath;
126 Q_EMIT filePathChanged(m_filePath);
127}
128
129void OCS::setArea(QRect area)
130{
131 if (m_area == area)
132 return;
133
134 m_area = area;
135 Q_EMIT areaChanged(m_area);
136}
137
Q_EMITQ_EMIT
bool isEmpty() const const
QString fromStdString(const std::string &str)
QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, UserInputResolutionOptions options)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:04:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.