MauiKit Image Tools

ocs.h
1#pragma once
2
3#include <QObject>
4#include <QUrl>
5#include <QRect>
6#include <QVariantMap>
7#include "image2text_export.h"
8#include <QQmlParserStatus>
9
10namespace tesseract
11{
12class TessBaseAPI;
13}
14
15namespace cv
16{
17class Mat;
18}
19
20typedef QVector<QVariantMap> TextBoxes;
21
22class OCRLanguageModel;
23class QImage;
24
25class IMAGE2TEXT_EXPORT OCS : public QObject, public QQmlParserStatus
26{
27 Q_OBJECT
28 Q_INTERFACES(QQmlParserStatus)
29 Q_PROPERTY(QString filePath READ filePath WRITE setFilePath NOTIFY filePathChanged)
30 Q_PROPERTY(QRect area READ area WRITE setArea NOTIFY areaChanged)
31
32 /** Immediately scan the image after a file url has been provided
33 */
34 Q_PROPERTY(bool autoRead READ autoRead WRITE setAutoRead NOTIFY autoReadChanged)
35
36 Q_PROPERTY(TextBoxes wordBoxes READ wordBoxes NOTIFY wordBoxesChanged)
37 Q_PROPERTY(TextBoxes paragraphBoxes READ paragraphBoxes NOTIFY paragraphBoxesChanged)
38 Q_PROPERTY(TextBoxes lineBoxes READ lineBoxes NOTIFY lineBoxesChanged)
39
40 Q_PROPERTY(BoxesType boxesType READ boxesType WRITE setBoxesType NOTIFY boxesTypeChanged)
41
42 /**
43 * The confidence thresshold to accept the retrieved text. Only result confidence levels above the threshold will be accepted
44 */
45 Q_PROPERTY(float confidenceThreshold READ confidenceThreshold WRITE setConfidenceThreshold NOTIFY confidenceThresholdChanged)
46 Q_PROPERTY(QString whiteList READ whiteList WRITE setWhiteList NOTIFY whiteListChanged)
47 Q_PROPERTY(QString blackList READ blackList WRITE setBlackList NOTIFY blackListChanged FINAL)
48
49 Q_PROPERTY(bool preprocessImage READ preprocessImage WRITE setPreprocessImage NOTIFY preprocessImageChanged FINAL)
50 Q_PROPERTY(PageSegMode pageSegMode READ pageSegMode WRITE setPageSegMode NOTIFY pageSegModeChanged)
51
52 Q_PROPERTY(bool ready READ ready NOTIFY readyChanged)
53
54public:
55
56 enum BoxType
57 {
58 Nothing = 0x0,
59 Word = 0x1,
60 Paragraph = 0x2,
61 Line = 0x4
62 };
63 Q_DECLARE_FLAGS(BoxesType, BoxType)
64 Q_FLAG(BoxesType)
65 Q_ENUM(BoxType)
66
67 enum PageSegMode
68 {
69 Auto,
70 Auto_OSD,
71 SingleColumn,
72 SingleLine,
73 SingleBlock,
74 SingleWord
75 }; Q_ENUM(PageSegMode)
76
77 explicit OCS(QObject *parent = nullptr);
78 ~OCS();
79
80 QString filePath() const;
81 QRect area() const;
82 bool autoRead() const;
83 TextBoxes wordBoxes() const;
84 TextBoxes paragraphBoxes() const;
85 TextBoxes lineBoxes() const;
86 OCS::BoxesType boxesType();
87 float confidenceThreshold();
88
89 QString whiteList() const;
90 QString blackList() const;
91
92 PageSegMode pageSegMode() const;
93
94 bool preprocessImage() const;
95
96 /**
97 * @brief See the Qt documentation on the QQmlParserStatus.
98 */
99 void classBegin() override;
100
101 /**
102 * @brief See the Qt documentation on the QQmlParserStatus.
103 */
104 void componentComplete() override;
105
106 bool ready() const;
107
108public Q_SLOTS:
109 QString getText();
110 void getTextAsync();
111
112 void setFilePath(QString filePath);
113 void setArea(QRect area);
114 void setAutoRead(bool value);
115 void setBoxesType(OCS::BoxesType types);
116 void setConfidenceThreshold(float value);
117
118 int wordBoxAt(const QPoint point);
119 QVector<int> wordBoxesAt(const QRect &rect);
120
121 void setWhiteList(const QString &value);
122 void setBlackList(const QString &value);
123
124 void setPreprocessImage(bool value);
125 void setPageSegMode(OCS::PageSegMode value);
126
127 static QString versionString();
128
129private:
130 tesseract::TessBaseAPI *m_tesseract;
131 OCRLanguageModel *m_languages;
132
133 QString m_filePath;
134 QRect m_area;
135 bool m_autoRead = false;
136 bool m_ready = false;
137
138 TextBoxes m_wordBoxes;
139 TextBoxes m_paragraphBoxes;
140 TextBoxes m_lineBoxes;
141
142 BoxesType m_boxesTypes;
143 float m_confidenceThreshold;
144
145 QString m_whiteList;
146 QString m_blackList;
147
148 bool m_preprocessImage = true;
149
150 QMap<QString, PageSegMode> m_segModesModel;
151 PageSegMode m_segMode = PageSegMode::Auto_OSD;
152
153 void do_preprocessImage(const QImage &image);
154
155 cv::Mat *m_imgMat = nullptr;///remeber to delete
156 QImage *m_ocrImg = nullptr;///remeber to delete
157
158Q_SIGNALS:
159 void filePathChanged(QString filePath);
160 void areaChanged(QRect area);
161 void autoReadChanged();
162 void textReady(QString text);
163 void wordBoxesChanged();
164 void lineBoxesChanged();
165 void paragraphBoxesChanged();
166 void boxesTypeChanged();
167 void confidenceThresholdChanged();
168 void whiteListChanged();
169 void blackListChanged();
170 void preprocessImageChanged();
171 void pageSegModeChanged();
172 void readyChanged();
173};
174Q_DECLARE_OPERATORS_FOR_FLAGS(OCS::BoxesType)
const char * versionString()
Q_ENUM(...)
Q_FLAG(...)
Q_INTERFACES(...)
Q_PROPERTY(...)
virtual void classBegin()=0
virtual void componentComplete()=0
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 11:56:08 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.