Skip to content

Commit 5491b40

Browse files
authored
Implement --skip-text as default setting (#126) (#128)
Closing #113
1 parent 959e767 commit 5491b40

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,13 @@ Currently the following settings are available per workflow:
126126
Name | Description
127127
--- | ---
128128
Languages | The languages to be used for OCR processing. The languages can be choosen from a dropdown list. For PDF files this setting corresponds to the `-l` parameter of `ocrmypdf`. **Please note** that you'll have to install the appropriate languages like described in the [`ocrmypdf` documentation](https://ocrmypdf.readthedocs.io/en/latest/languages.html).
129-
Remove background | If the switch is set, the OCR processor will try to remove the background of the document before processing and instead set a white background. For PDF files this setting corresponds to the [`--remove-background`](https://ocrmypdf.readthedocs.io/en/latest/cookbook.html?highlight=remove-background#image-processing) parameter of `ocrmypdf`. **Please note** that without setting this option, the [`--redo-ocr`](https://ocrmypdf.readthedocs.io/en/latest/errors.html?highlight=redo-ocr#page-already-has-text) option will be set, which is **not** compatible to the mentioned `--remove-background`-parameter. So if you set this switch to "on", make sure your PDF documents do not already contain text, otherwise you might find errors in your NC logs and OCR is not possible.
129+
Remove background | If the switch is set, the OCR processor will try to remove the background of the document before processing and instead set a white background. For PDF files this setting corresponds to the [`--remove-background`](https://ocrmypdf.readthedocs.io/en/latest/cookbook.html?highlight=remove-background#image-processing) parameter of `ocrmypdf`.
130+
<!--
131+
132+
Uncomment this section if we implemented the --redo-ocr/--skip-text option as a workflow setting
133+
134+
**Please note** that without setting this option, the [`--redo-ocr`](https://ocrmypdf.readthedocs.io/en/latest/errors.html?highlight=redo-ocr#page-already-has-text) option will be set, which is **not** compatible to the mentioned `--remove-background`-parameter. So if you set this switch to "on", make sure your PDF documents do not already contain text, otherwise you might find errors in your NC logs and OCR is not possible.
135+
-->
130136

131137
#### Global settings
132138
As a Nextcloud administrator you're able to configure global settings which apply to all configured OCR-workflows on the current system.
@@ -160,7 +166,7 @@ To **test** if your file gets processed properly you can do the following steps:
160166
</p>
161167

162168
### PDF
163-
For processing PDF files, the external command line tool [`OCRmyPDF`](https://github.com/jbarlow83/OCRmyPDF) is used. The tool is invoked with the [`--redo-ocr`](https://ocrmypdf.readthedocs.io/en/latest/advanced.html#when-ocr-is-skipped) parameter so that it will perform a detailed text analysis. The detailed analysis masks out visible text and sends the image of each page to the OCR processor. After processing, additional text is inserted as OCR, whereas existing text in a mixed file document (images embedded into text pages) is not disrupted.
169+
For processing PDF files, the external command line tool [`OCRmyPDF`](https://github.com/jbarlow83/OCRmyPDF) is used. The tool is always invoked with the [`--skip-text`](https://ocrmypdf.readthedocs.io/en/latest/advanced.html#when-ocr-is-skipped) parameter so that it will skip pages which already contain text. Please note that with that parameter set, it's currently not possible to analize pages with mixed content (see https://github.com/R0Wi/workflow_ocr/issues/113 for furhter information).
164170

165171
### Images
166172
For processing single images (currently `jpg` and `png` are supported), `ocrmypdf` converts the image to a PDF. The converted PDF file will then be OCR processed and saved as a new file with the original filename and the extension `.pdf` (for example `myImage.jpg` will be saved to `myImage.jpg.pdf`). The original image fill will remain untouched.

lib/OcrProcessors/OcrMyPdfBasedProcessor.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct(ICommand $command, LoggerInterface $logger) {
5959
}
6060

6161
public function ocrFile(File $file, WorkflowSettings $settings, GlobalSettings $globalSettings): OcrProcessorResult {
62-
$commandStr = 'ocrmypdf -q ' . $this->getCommandlineArgs($settings, $globalSettings) . ' - - | cat';
62+
$commandStr = 'ocrmypdf ' . $this->getCommandlineArgs($settings, $globalSettings) . ' - - | cat';
6363

6464
$inputFileContent = $file->getContent();
6565

@@ -108,7 +108,8 @@ protected function getAdditionalCommandlineArgs(WorkflowSettings $settings, Glob
108108

109109

110110
private function getCommandlineArgs(WorkflowSettings $settings, GlobalSettings $globalSettings): string {
111-
$args = [];
111+
// Default setting is quiet with skip-text
112+
$args = ['-q', '--skip-text'];
112113

113114
// Language settings
114115
if ($settings->getLanguages()) {
@@ -123,8 +124,11 @@ private function getCommandlineArgs(WorkflowSettings $settings, GlobalSettings $
123124
$args[] = "-l $langStr";
124125
}
125126

126-
// Remove background option (incompatible with redo-ocr)
127-
$args[] = $settings->getRemoveBackground() ? '--remove-background' : '--redo-ocr';
127+
// Remove background option (NOTE :: this is incompatible with redo-ocr, so if we
128+
// decide to make this configurable, make it exclusive against each other!)
129+
if ($settings->getRemoveBackground()) {
130+
$args[] = '--remove-background';
131+
}
128132

129133
// Number of CPU's to be used
130134
$processorCount = intval($globalSettings->processorCount);

tests/Unit/OcrProcessors/PdfOcrProcessorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function testThrowsErrorIfOcrFileWasEmpty() {
179179
public function testLanguageSettingsAreSetCorrectly() {
180180
$this->command->expects($this->once())
181181
->method('setCommand')
182-
->with('ocrmypdf -q -l deu+eng --redo-ocr - - | cat');
182+
->with('ocrmypdf -q --skip-text -l deu+eng - - | cat');
183183
$this->command->expects($this->once())
184184
->method('execute')
185185
->willReturn(true);
@@ -194,7 +194,7 @@ public function testLanguageSettingsAreSetCorrectly() {
194194
public function testInvalidLanguagesAreFiltered() {
195195
$this->command->expects($this->once())
196196
->method('setCommand')
197-
->with('ocrmypdf -q -l deu+eng --redo-ocr - - | cat');
197+
->with('ocrmypdf -q --skip-text -l deu+eng - - | cat');
198198
$this->command->expects($this->once())
199199
->method('execute')
200200
->willReturn(true);
@@ -209,7 +209,7 @@ public function testInvalidLanguagesAreFiltered() {
209209
public function testRemoveBackgroundFlagIsSetCorrectly() {
210210
$this->command->expects($this->once())
211211
->method('setCommand')
212-
->with('ocrmypdf -q --remove-background - - | cat');
212+
->with('ocrmypdf -q --skip-text --remove-background - - | cat');
213213
$this->command->expects($this->once())
214214
->method('execute')
215215
->willReturn(true);
@@ -224,7 +224,7 @@ public function testRemoveBackgroundFlagIsSetCorrectly() {
224224
public function testProcessorCountIsNotSetIfGlobalSettingsDoesNotContainProcessorCount() {
225225
$this->command->expects($this->once())
226226
->method('setCommand')
227-
->with('ocrmypdf -q --redo-ocr - - | cat');
227+
->with('ocrmypdf -q --skip-text - - | cat');
228228
$this->command->expects($this->once())
229229
->method('execute')
230230
->willReturn(true);
@@ -239,7 +239,7 @@ public function testProcessorCountIsNotSetIfGlobalSettingsDoesNotContainProcesso
239239
public function testProcessorCountIsSetCorrectlyFromGobalSettings() {
240240
$this->command->expects($this->once())
241241
->method('setCommand')
242-
->with('ocrmypdf -q --redo-ocr -j 42 - - | cat');
242+
->with('ocrmypdf -q --skip-text -j 42 - - | cat');
243243
$this->command->expects($this->once())
244244
->method('execute')
245245
->willReturn(true);

0 commit comments

Comments
 (0)