File indexing completed on 2025-01-19 03:57:50
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2022-08-26 0007 * Description : CLI tool for OCR Text Converter plugin 0008 * 0009 * SPDX-FileCopyrightText: 2022 by Quoc Hung Tran <quochungtran1999 at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 // Qt includes 0016 0017 #include <QString> 0018 #include <QStringList> 0019 #include <QProcess> 0020 #include <QFile> 0021 #include <QUrl> 0022 #include <QDebug> 0023 #include <QCoreApplication> 0024 #include <QDir> 0025 0026 int main(int argc, char* argv[]) 0027 { 0028 QCoreApplication app(argc, argv); 0029 0030 QProcess* const ocrProcess = new QProcess(); 0031 ocrProcess->setProcessChannelMode(QProcess::SeparateChannels); 0032 QStringList args; 0033 0034 // ------------------------- IN/OUT ARGUMENTS ------------------------- 0035 0036 QString pathImage = QLatin1String("../../core/tests/ocrtextconverter/data/"); 0037 QString pathToSaveText = QLatin1String("../../core/tests/ocrtextconverter/data/"); 0038 QString inputFileName = pathImage.append(QLatin1String("scanned_img.jpg")); 0039 QString outputTextName = pathToSaveText.append(QLatin1String("scanned_img")); 0040 0041 // add configuration image 0042 0043 if (!inputFileName.isEmpty()) 0044 { 0045 args << inputFileName; 0046 } 0047 0048 // output base name 0049 0050 if (!outputTextName.isEmpty()) 0051 { 0052 args << outputTextName; 0053 } 0054 else 0055 { 0056 args << QLatin1String("stdout"); 0057 } 0058 0059 // ----------------------------- OPTIONS ----------------------------- 0060 0061 QString lang = QLatin1String("eng"); 0062 QString psm = QLatin1String("3"); 0063 QString oem = QLatin1String("3"); 0064 QString dpi = QLatin1String(""); 0065 QUrl userWords = QUrl(QLatin1String(" ")); 0066 QUrl userPatterns = QUrl(QLatin1String(" ")); 0067 0068 // User words 0069 0070 if (!userWords.isValid()) 0071 { 0072 args << QLatin1String("--user-words") << userWords.toLocalFile(); 0073 } 0074 0075 // User patterns 0076 0077 if (!userPatterns.isValid()) 0078 { 0079 args << QLatin1String("--user-patterns") << userPatterns.toLocalFile(); 0080 } 0081 0082 // page Segmentation mode 0083 0084 if (!psm.isEmpty()) 0085 { 0086 args << QLatin1String("--psm") << psm; 0087 } 0088 0089 // OCR enginge mode 0090 0091 if (!oem.isEmpty()) 0092 { 0093 args << QLatin1String("--oem") << oem; 0094 } 0095 0096 // Language 0097 0098 if (!lang.isEmpty()) 0099 { 0100 args << QLatin1String("-l") << lang; 0101 } 0102 0103 // Specify DPI for input image 0104 // Tesseract work best with 300 dpi for input image 0105 0106 if (!dpi.isEmpty()) 0107 { 0108 args << QLatin1String("--dpi") << dpi; 0109 } 0110 0111 // ------------------ Running tesseract process ------------------ 0112 0113 const QString cmd = QLatin1String("tesseract"); 0114 0115 ocrProcess->setProgram(cmd); 0116 ocrProcess->setArguments(args); 0117 0118 qDebug() << "Running OCR : " 0119 << ocrProcess->program() 0120 << ocrProcess->arguments(); 0121 0122 ocrProcess->start(); 0123 0124 bool successFlag = (ocrProcess->waitForFinished(-1) && (ocrProcess->exitStatus() == QProcess::NormalExit)); 0125 0126 if (!successFlag) 0127 { 0128 qWarning() << "Error starting OCR Process"; 0129 return 0; 0130 } 0131 0132 QString output = QString::fromLocal8Bit(ocrProcess->readAllStandardOutput()); 0133 0134 qDebug() << output; 0135 0136 delete ocrProcess; 0137 0138 return 0; 0139 }