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

KDE's Doxygen guidelines are available online.