|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using iText.Commons.Utils; |
| 4 | +using iText.Kernel.Colors; |
| 5 | +using iText.Kernel.Pdf; |
| 6 | +using iText.Pdfocr.Onnxtr.Detection; |
| 7 | +using iText.Pdfocr.Onnxtr.Orientation; |
| 8 | +using iText.Pdfocr.Onnxtr.Recognition; |
| 9 | +using iText.Test; |
| 10 | + |
| 11 | +namespace iText.Pdfocr.Onnxtr { |
| 12 | + [NUnit.Framework.Category("IntegrationTest")] |
| 13 | + public class OnnxTRCmykIntegrationTest : ExtendedITextTest { |
| 14 | + private static readonly String TEST_DIRECTORY = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 15 | + .CurrentContext.TestDirectory) + "/resources/itext/pdfocr/OnnxTRCmykIntegrationTest/"; |
| 16 | + |
| 17 | + private static readonly String TEST_IMAGE_DIRECTORY = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 18 | + .CurrentContext.TestDirectory) + "/resources/itext/pdfocr/images/"; |
| 19 | + |
| 20 | + private static readonly String TARGET_DIRECTORY = NUnit.Framework.TestContext.CurrentContext.TestDirectory |
| 21 | + + "/test/resources/itext/pdfocr/OnnxTRCmykIntegrationTest/"; |
| 22 | + |
| 23 | + private static readonly String FAST = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 24 | + .CurrentContext.TestDirectory) + "/resources/itext/pdfocr/models/rep_fast_tiny-28867779.onnx"; |
| 25 | + |
| 26 | + private static readonly String CRNNVGG16 = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 27 | + .CurrentContext.TestDirectory) + "/resources/itext/pdfocr/models/crnn_vgg16_bn-662979cc.onnx"; |
| 28 | + |
| 29 | + private static readonly String MOBILENETV3 = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 30 | + .CurrentContext.TestDirectory) + "/resources/itext/pdfocr/models/mobilenet_v3_small_crop_orientation-5620cf7e.onnx"; |
| 31 | + |
| 32 | + private static OnnxTrOcrEngine OCR_ENGINE; |
| 33 | + |
| 34 | + [NUnit.Framework.OneTimeSetUp] |
| 35 | + public static void BeforeClass() { |
| 36 | + CreateOrClearDestinationFolder(TARGET_DIRECTORY); |
| 37 | + IDetectionPredictor detectionPredictor = OnnxDetectionPredictor.Fast(FAST); |
| 38 | + IRecognitionPredictor recognitionPredictor = OnnxRecognitionPredictor.CrnnVgg16(CRNNVGG16); |
| 39 | + IOrientationPredictor orientationPredictor = OnnxOrientationPredictor.MobileNetV3(MOBILENETV3); |
| 40 | + OCR_ENGINE = new OnnxTrOcrEngine(detectionPredictor, orientationPredictor, recognitionPredictor); |
| 41 | + } |
| 42 | + |
| 43 | + [NUnit.Framework.OneTimeTearDown] |
| 44 | + public static void AfterClass() { |
| 45 | + OCR_ENGINE.Close(); |
| 46 | + } |
| 47 | + |
| 48 | + [NUnit.Framework.Test] |
| 49 | + public virtual void RainbowInvertedCmykTest() { |
| 50 | + String src = TEST_IMAGE_DIRECTORY + "rainbow_inverted_cmyk.jpg"; |
| 51 | + String dest = TARGET_DIRECTORY + "rainbowInvertedCmykTest.pdf"; |
| 52 | + String cmpTxt = TEST_DIRECTORY + "cmp_rainbowInvertedCmykTest.txt"; |
| 53 | + DoOcrAndCreatePdf(src, dest, CreatorProperties("Text1", DeviceCmyk.MAGENTA)); |
| 54 | + using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(dest))) { |
| 55 | + ExtractionStrategy extractionStrategy = OnnxTestUtils.ExtractTextFromLayer(pdfDocument, 1, "Text1"); |
| 56 | + NUnit.Framework.Assert.AreEqual(DeviceCmyk.MAGENTA, extractionStrategy.GetFillColor()); |
| 57 | + NUnit.Framework.Assert.AreEqual(GetCmpText(cmpTxt), extractionStrategy.GetResultantText()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [NUnit.Framework.Test] |
| 62 | + public virtual void RainbowAdobeCmykTest() { |
| 63 | + String src = TEST_IMAGE_DIRECTORY + "rainbow_adobe_cmyk.jpg"; |
| 64 | + String dest = TARGET_DIRECTORY + "rainbowAdobeCmykTest.pdf"; |
| 65 | + String cmpTxt = TEST_DIRECTORY + "cmp_rainbowAdobeCmykTest.txt"; |
| 66 | + DoOcrAndCreatePdf(src, dest, CreatorProperties("Text1", DeviceCmyk.MAGENTA)); |
| 67 | + using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(dest))) { |
| 68 | + ExtractionStrategy extractionStrategy = OnnxTestUtils.ExtractTextFromLayer(pdfDocument, 1, "Text1"); |
| 69 | + NUnit.Framework.Assert.AreEqual(DeviceCmyk.MAGENTA, extractionStrategy.GetFillColor()); |
| 70 | + NUnit.Framework.Assert.AreEqual(GetCmpText(cmpTxt), extractionStrategy.GetResultantText()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + [NUnit.Framework.Test] |
| 75 | + public virtual void RainbowCmykNoProfileTest() { |
| 76 | + String src = TEST_IMAGE_DIRECTORY + "rainbow_cmyk_inverted_no_profile.jpg"; |
| 77 | + String dest = TARGET_DIRECTORY + "rainbowCmykNoProfileTest.pdf"; |
| 78 | + String cmpTxt = TEST_DIRECTORY + "cmp_rainbowCmykNoProfileTest.txt"; |
| 79 | + DoOcrAndCreatePdf(src, dest, CreatorProperties("Text1", DeviceCmyk.MAGENTA)); |
| 80 | + using (PdfDocument pdfDocument = new PdfDocument(new PdfReader(dest))) { |
| 81 | + ExtractionStrategy extractionStrategy = OnnxTestUtils.ExtractTextFromLayer(pdfDocument, 1, "Text1"); |
| 82 | + NUnit.Framework.Assert.AreEqual(DeviceCmyk.MAGENTA, extractionStrategy.GetFillColor()); |
| 83 | + NUnit.Framework.Assert.AreEqual(GetCmpText(cmpTxt), extractionStrategy.GetResultantText()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private OcrPdfCreatorProperties CreatorProperties(String layerName, Color color) { |
| 88 | + OcrPdfCreatorProperties ocrPdfCreatorProperties = new OcrPdfCreatorProperties(); |
| 89 | + ocrPdfCreatorProperties.SetTextLayerName(layerName); |
| 90 | + ocrPdfCreatorProperties.SetTextColor(color); |
| 91 | + return ocrPdfCreatorProperties; |
| 92 | + } |
| 93 | + |
| 94 | + private void DoOcrAndCreatePdf(String imagePath, String destPdfPath, OcrPdfCreatorProperties ocrPdfCreatorProperties |
| 95 | + ) { |
| 96 | + OcrPdfCreator ocrPdfCreator = ocrPdfCreatorProperties != null ? new OcrPdfCreator(OCR_ENGINE, ocrPdfCreatorProperties |
| 97 | + ) : new OcrPdfCreator(OCR_ENGINE); |
| 98 | + using (PdfWriter writer = new PdfWriter(destPdfPath)) { |
| 99 | + ocrPdfCreator.CreatePdf(JavaCollectionsUtil.SingletonList(new FileInfo(imagePath)), writer).Close(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private String GetCmpText(String txtPath) { |
| 104 | + int bytesCount = (int)new FileInfo(txtPath).Length; |
| 105 | + char[] array = new char[bytesCount]; |
| 106 | + using (StreamReader stream = new StreamReader(iText.Commons.Utils.FileUtil.GetInputStreamForFile(System.IO.Path.Combine |
| 107 | + (txtPath)))) { |
| 108 | + stream.Read(array, 0, bytesCount); |
| 109 | + return new String(array); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments