This repository was archived by the owner on May 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTesseractEngine.php
More file actions
146 lines (129 loc) · 4.2 KB
/
Copy pathTesseractEngine.php
File metadata and controls
146 lines (129 loc) · 4.2 KB
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
<?php
declare( strict_types = 1 );
namespace App\Engine;
use App\Exception\OcrException;
use Imagine\Gd\Imagine;
use Krinkle\Intuition\Intuition;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use thiagoalessio\TesseractOCR\TesseractOCR;
use thiagoalessio\TesseractOCR\UnsuccessfulCommandException;
class TesseractEngine extends EngineBase {
/** @var TesseractOCR */
private $ocr;
/** @var int Maximum value for page segmentation mode. */
public const MAX_PSM = 13;
/** @var int Default value for page segmentation mode. */
public const DEFAULT_PSM = 3;
/**
* TesseractEngine constructor.
* @param HttpClientInterface $httpClient
* @param Intuition $intuition
* @param string $projectDir
* @param TesseractOCR $tesseractOcr
*/
public function __construct(
HttpClientInterface $httpClient,
Intuition $intuition,
string $projectDir,
TesseractOCR $tesseractOcr
) {
parent::__construct( $intuition, $projectDir, $httpClient );
$this->ocr = $tesseractOcr;
}
/**
* @inheritDoc
*/
public static function getId(): string {
return 'tesseract';
}
/**
* @inheritDoc
*/
public function getResult(
string $imageUrl,
string $invalidLangsMode,
array $crop,
?array $langs = null,
int $rotate = 0
): EngineResult {
// Check the URL and fetch the image data.
$this->checkImageUrl( $imageUrl );
[ $validLangs, $invalidLangs ] = $this->filterValidLangs( $langs, $invalidLangsMode );
$image = $this->getImage( $imageUrl, $crop, self::DO_DOWNLOAD_IMAGE );
// If there is a rotation, apply it to the image data.
if ( $rotate !== 0 ) {
$imagine = new Imagine();
$loaded = $imagine->load( $image->getData() );
$loaded->rotate( $rotate );
$image->setData( $loaded->get( 'jpg' ) );
}
$this->ocr->imageData( $image->getData(), $image->getSize() );
if ( $validLangs ) {
$this->ocr->lang( ...$validLangs );
}
// Env vars are passed through by the thiagoalessio/tesseract_ocr package to the tesseract command,
// but when they're loaded from Symfony's .env they aren't actually available (by design),
// so we have to load this one manually. We only process one image at a time, so don't benefit from
// multiple threads. See https://github.com/tesseract-ocr/tesseract/issues/898 for some more info.
putenv( 'OMP_THREAD_LIMIT=1' );
try {
$text = $this->ocr->run();
} catch ( UnsuccessfulCommandException $e ) {
// An UnsuccessfulCommandException is thrown when there's no output, but that's not an
// actual error so we check for it here and just show a warning. The same exception class
// is also used for other things, hence the message check here.
if ( strpos( $e->getMessage(), 'The command did not produce any output' ) !== false ) {
return new EngineResult( '', [ $this->intuition->msg( 'tesseract-no-text-error' ) ] );
}
throw $e;
}
$warnings = $invalidLangs ? [ $this->getInvalidLangsWarning( $invalidLangs ) ] : [];
return new EngineResult( $text, $warnings );
}
/**
* Set the page segmentation mode.
* @param int $psm
*/
public function setPsm( int $psm ): void {
$this->validateOption( 'psm', $psm, self::MAX_PSM );
$this->ocr->psm( $psm );
}
/**
* Get available PSM IDs and values.
* @return mixed[][]
*/
public function getAvailablePsms(): array {
$psms = [];
$psmIds = [ 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ];
foreach ( $psmIds as $psmId ) {
array_push( $psms, [
'value' => $psmId,
// The following messages can be used here: 'tesseract-psm-0', 'tesseract-psm-1',
// 'tesseract-psm-3', 'tesseract-psm-4', 'tesseract-psm-5', 'tesseract-psm-6', 'tesseract-psm-7',
// 'tesseract-psm-8', 'tesseract-psm-9', 'tesseract-psm-10', 'tesseract-psm-11', 'tesseract-psm-12',
// 'tesseract-psm-13'
'label' => $this->intuition->msg( 'tesseract-psm-' . $psmId ),
] );
}
return $psms;
}
/**
* Validates the given option.
* @param string $option
* @param int $given
* @param int $maximum
* @throws OcrException
*/
private function validateOption( string $option, int $given, int $maximum ): void {
if ( $given > $maximum ) {
throw new OcrException(
'tesseract-param-error',
[
$this->intuition->msg( "tesseract-$option-label" ),
$given,
$maximum,
]
);
}
}
}