-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTesseractService.php
309 lines (247 loc) · 7.32 KB
/
TesseractService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
declare(strict_types=1);
/**
* Files_FullTextSearch_OCR - OCR your files before index
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2018
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files_FullTextSearch_Tesseract\Service;
use Exception;
use OC\Files\View;
use OCP\EventDispatcher\GenericEvent;
use OCP\Files\File;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files_FullTextSearch\Model\AFilesDocument;
use OCP\FullTextSearch\Model\IIndexDocument;
use OCP\FullTextSearch\Model\ISearchRequest;
use Psr\Log\LoggerInterface;
use Spatie\PdfToImage\Exceptions\PageDoesNotExist;
use Spatie\PdfToImage\Pdf;
use thiagoalessio\TesseractOCR\TesseractOCR;
use Throwable;
/**
* Class TesseractService
*
* @package OCA\Files_FullTextSearch_Tesseract\Service
*/
class TesseractService {
public function __construct(
private ConfigService $configService,
private LoggerInterface $logger
) {
}
/**
* @param string $mimeType
* @param string $extension
*
* @return bool
*/
public function parsedMimeType(string $mimeType, string $extension): bool {
$ocrMimes = [
'image/png',
'image/jpeg',
'image/tiff',
'image/vnd.djvu',
'application/pdf'
];
foreach ($ocrMimes as $mime) {
if (strpos($mimeType, $mime) === 0) {
return true;
}
}
if ($mimeType === 'application/octet-stream') {
return $this->parsedExtension($extension);
}
return false;
}
/**
* @param GenericEvent $e
*/
public function onFileIndexing(GenericEvent $e): void {
/** @var Node $file */
$file = $e->getArgument('file');
if (!$file instanceof File) {
return;
}
/** @var \OCP\Files_FullTextSearch\Model\AFilesDocument $document */
$document = $e->getArgument('document');
$this->extractContentUsingTesseractOCR($document, $file);
}
/**
* @param GenericEvent $e
*/
public function onSearchRequest(GenericEvent $e) {
/** @var ISearchRequest $file */
$request = $e->getArgument('request');
$request->addPart('ocr');
}
/**
* @param AFilesDocument $document
* @param File $file
*/
private function extractContentUsingTesseractOCR(AFilesDocument &$document, File $file): void {
try {
if ($this->configService->getAppValue(ConfigService::TESSERACT_ENABLED) !== '1') {
return;
}
$extension = pathinfo($document->getPath(), PATHINFO_EXTENSION);
if (!$this->parsedMimeType($document->getMimetype(), $extension)) {
return;
}
$this->logger->debug(
'extracting content using TesseractOCR',
[
'documentId' => $document->getId(),
'path' => $document->getPath(),
'mime' => $document->getMimetype(),
'extension' => $extension
]
);
// TODO: How to set options so that the index can be reset if admin settings are changed
// $this->configService->setDocumentIndexOption($document, ConfigService::FILES_OCR);
if ($this->ocrPdf($document, $file)) {
return;
}
$content = $this->ocrFile($file);
} catch (Throwable $e) {
return;
}
$document->setContent(base64_encode($content), IIndexDocument::ENCODED_BASE64);
}
/**
* @param File $file
*
* @return string
* @throws NotFoundException
*/
private function ocrFile(File $file): string {
try {
$path = $this->getAbsolutePath($file);
} catch (Exception $e) {
$this->logger->notice('issue during ocrFile()', ['exception' => $e]);
throw new NotFoundException();
}
return $this->ocrFileFromPath($path);
}
/**
* @param string $path
*
* @return string
*/
private function ocrFileFromPath(string $path): string {
$this->logger->debug('generating the TesseractOCR wrapper', ['path' => $path]);
$ocr = new TesseractOCR($path);
$ocr->psm($this->configService->getAppValue(ConfigService::TESSERACT_PSM));
$lang = explode(',', $this->configService->getAppValue(ConfigService::TESSERACT_LANG));
call_user_func_array([$ocr, 'lang'], array_map('trim', $lang));
$this->logger->debug('running the OCR command', ['command' => $ocr->command]);
// if ($this->configService->getLogLevel() > 0) {
// $ocr->command .= ' 2> /dev/null';
// }
try {
$result = $ocr->run();
$this->logger->debug('OCR command ran smoothly');
} catch (Exception $e) {
$this->logger->notice('failed to OCR', [
'exception' => $e,
'path' => $path,
'cmd' => $ocr->command,
'lang' => $lang
]);
$result = '';
}
return $result;
}
/**
* @param AFilesDocument $document
* @param File $file
*
* @return bool
* @throws NotFoundException
*/
private function ocrPdf(AFilesDocument $document, File $file): bool {
if ($document->getMimetype() !== 'application/pdf') {
return false;
}
if ($this->configService->getAppValue(ConfigService::TESSERACT_PDF) !== '1') {
return true;
}
$this->logger->debug('looks like we\'re working on a PDF file');
try {
$path = $this->getAbsolutePath($file);
$this->logger->debug('Absolute path', ['path' => $path]);
$pdf = new Pdf($path);
} catch (Exception $e) {
$this->logger->notice('failed to ocrPdf', ['exception' => $e, 'document' => $document]);
throw new NotFoundException();
}
$content = '';
$pages = $pdf->getNumberOfPages();
$this->logger->debug('PDF contains ' . $pages . ' page(s)');
$limit = (int)$this->configService->getAppValue(ConfigService::TESSERACT_PDF_LIMIT);
$pages = ($limit > 0 && $pages > $limit) ? $limit : $pages;
$this->logger->debug('App will now ocr ' . $pages . ' page(s)');
for ($i = 1; $i <= $pages; $i++) {
$this->logger->debug('Creating a temp image file for page #' . $i);
$tmpFile = tmpfile();
$tmpPath = stream_get_meta_data($tmpFile)['uri'];
$this->logger->debug('temp image file: ' . $tmpPath . ' for page #' . $i);
try {
$this->logger->debug('opening the PDF at the page #' . $i);
$pdf->setPage($i);
$this->logger->debug('saving the current page as image', ['tmpPath' => $tmpPath]);
$pdf->saveImage($tmpPath);
$content .= $this->ocrFileFromPath($tmpPath);
} catch (PageDoesNotExist $e) {
}
fclose($tmpFile);
}
$this->logger->debug('Saving the data into the IndexDocument');
$document->addPart('ocr', $content);
return true;
}
/**
* @param string $extension
*
* @return bool
*/
private function parsedExtension(string $extension): bool {
$ocrExtensions = [
// 'djvu'
];
if (in_array($extension, $ocrExtensions)) {
return true;
}
return false;
}
/**
* @param File $file
*
* @return string
* @throws Exception
*/
private function getAbsolutePath(File $file): string {
$view = new View('');
return $view->getLocalFile($file->getPath());
}
}