Skip to content

Commit b35e080

Browse files
committed
Support multilingual model for onnxtr
DEVSIX-9295
1 parent db1e34f commit b35e080

8 files changed

Lines changed: 156871 additions & 58 deletions

File tree

pdfocr-onnxtr/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939

4040
<properties>
4141
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
42+
<sonar.exclusions>
43+
**com/itextpdf/pdfocr/onnxtr/recognition/Vocabulary.java
44+
</sonar.exclusions>
4245
</properties>
4346

4447
<build>

pdfocr-onnxtr/src/main/java/com/itextpdf/pdfocr/onnxtr/recognition/OnnxRecognitionPredictor.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,36 @@ public static OnnxRecognitionPredictor master(String modelPath) {
158158
* @return a new predictor object with the PARSeq model loaded
159159
*/
160160
public static OnnxRecognitionPredictor parSeq(String modelPath) {
161-
return new OnnxRecognitionPredictor(OnnxRecognitionPredictorProperties.parSeq(modelPath));
161+
return new OnnxRecognitionPredictor(
162+
OnnxRecognitionPredictorProperties.parSeq(modelPath));
163+
}
164+
165+
/**
166+
* Creates a new text recognition predictor using an existing pre-trained
167+
* PARSeq model, stored on disk.
168+
*
169+
* <p>
170+
* This can be used to load the following models from OnnxTR:
171+
* <ul>
172+
* <li>
173+
* <a href="https://github.com/felixdittrich92/OnnxTR/releases/download/v0.0.1/parseq-00b40714.onnx">
174+
* parseq
175+
* </a>
176+
* <li>
177+
* <a href="https://github.com/felixdittrich92/OnnxTR/releases/download/v0.1.2/parseq_dynamic_8_bit-5b04d9f7.onnx">
178+
* parseq (8-bit quantized)
179+
* </a>
180+
* </ul>
181+
*
182+
* @param modelPath path to the pre-trained model
183+
* @param vocabulary vocabulary used for the model output (without special tokens)
184+
* @param additionalTokens amount of additional tokens in the total vocabulary after the end-of-string token
185+
*
186+
* @return a new predictor object with the PARSeq model loaded
187+
*/
188+
public static OnnxRecognitionPredictor parSeq(String modelPath, Vocabulary vocabulary, int additionalTokens) {
189+
return new OnnxRecognitionPredictor(
190+
OnnxRecognitionPredictorProperties.parSeq(modelPath, vocabulary, additionalTokens));
162191
}
163192

164193
/**

pdfocr-onnxtr/src/main/java/com/itextpdf/pdfocr/onnxtr/recognition/OnnxRecognitionPredictorProperties.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,38 @@ public static OnnxRecognitionPredictorProperties master(String modelPath) {
175175
* @return a new text recognition properties object for a PARSeq model
176176
*/
177177
public static OnnxRecognitionPredictorProperties parSeq(String modelPath) {
178+
return OnnxRecognitionPredictorProperties.parSeq(modelPath, Vocabulary.LATIN_EXTENDED, 0);
179+
}
180+
181+
/**
182+
* Creates a new text recognition properties object for existing pre-trained
183+
* PARSeq models, stored on disk.
184+
*
185+
* <p>
186+
* This can be used to load the following models from OnnxTR:
187+
* <ul>
188+
* <li>
189+
* <a href="https://github.com/felixdittrich92/OnnxTR/releases/download/v0.0.1/parseq-00b40714.onnx">
190+
* parseq
191+
* </a>
192+
* <li>
193+
* <a href="https://github.com/felixdittrich92/OnnxTR/releases/download/v0.1.2/parseq_dynamic_8_bit-5b04d9f7.onnx">
194+
* parseq (8-bit quantized)
195+
* </a>
196+
* </ul>
197+
*
198+
* @param modelPath path to the pre-trained model
199+
* @param vocabulary vocabulary used for the model output (without special tokens)
200+
* @param additionalTokens amount of additional tokens in the total vocabulary after the end-of-string token
201+
*
202+
* @return a new text recognition properties object for a PARSeq model
203+
*/
204+
public static OnnxRecognitionPredictorProperties parSeq(String modelPath, Vocabulary vocabulary,
205+
int additionalTokens) {
178206
return new OnnxRecognitionPredictorProperties(
179207
modelPath,
180208
DEFAULT_INPUT_PROPERTIES,
181-
new EndOfStringPostProcessor(Vocabulary.FRENCH, 0)
209+
new EndOfStringPostProcessor(vocabulary, additionalTokens)
182210
);
183211
}
184212

pdfocr-onnxtr/src/main/java/com/itextpdf/pdfocr/onnxtr/recognition/Vocabulary.java

Lines changed: 1057 additions & 0 deletions
Large diffs are not rendered by default.

pdfocr-onnxtr/src/test/java/com/itextpdf/pdfocr/onnxtr/OnnxDoImageOcrLanguagesTest.java

Lines changed: 171 additions & 53 deletions
Large diffs are not rendered by default.

pdfocr-onnxtr/src/test/java/com/itextpdf/pdfocr/onnxtr/OnnxTRCmykIntegrationTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ private static boolean isFixedInJdk(String versionStr) {
140140
String[] split = versionStr.split("[._-]");
141141
int minorVer = Integer.parseInt(split[split.length - 1]);
142142

143-
if (minorVer % 10 == 2) {
144-
return false;
145-
}
146143
switch (majorVer) {
147144
case 8:
148145
isFixed = minorVer >= 351;
22.2 KB
Loading

0 commit comments

Comments
 (0)