-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathTranskribusEngine.php
More file actions
164 lines (138 loc) · 4.57 KB
/
TranskribusEngine.php
File metadata and controls
164 lines (138 loc) · 4.57 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?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;
class TranskribusEngine extends EngineBase {
/** @var TranskribusClient */
protected $transkribusClient;
/** @var int line detection model ID to be used by the Transkribus engine */
protected $lineId;
/** @var int Default value for line detection model ID to be used by Transkribus */
public const DEFAULT_LINEID = 0;
/** @var string[] Model names for corresponding line detection model lang codes */
public const LINE_ID_MODEL_NAMES = [
'bali' => 'Balinese Line Detection Model',
];
/**
* TranskribusEngine constructor.
* @param TranskribusClient $transkribusClient
* @param Intuition $intuition
* @param string $projectDir
* @param HttpClientInterface $httpClient
*/
public function __construct(
TranskribusClient $transkribusClient,
Intuition $intuition,
string $projectDir,
HttpClientInterface $httpClient
) {
parent::__construct( $intuition, $projectDir, $httpClient );
$this->transkribusClient = $transkribusClient;
}
/**
* @inheritDoc
*/
public static function getId(): string {
return 'transkribus';
}
/**
* Get line detection models accepted by the engine
* @param bool $onlyLineIds Whether to return only the line detection model IDs
* @param bool $onlyLineIdLangs Whether to return only the line detection model IDs lang codes
* @return string[] Line detection model lang codes or model IDs or model ID names
*/
public function getValidLineIds( bool $onlyLineIds = false, bool $onlyLineIdLangs = false ): array {
$filteredLangList = array_filter(
$this->getModelList(), static function ( $value ) {
return isset( $value['line'] ) && $value['line'] !== '';
}
);
$lineIdLangs = array_keys( $filteredLangList );
// return only the lang names as written in the models.json file
if ( $onlyLineIdLangs ) {
return $lineIdLangs;
}
// create a list that maps from lang name to line detection model name
$lineIDList = [];
foreach ( $lineIdLangs as $lineIdLang ) {
$lineIDList[$lineIdLang] = $this->getLineIdModelName( $lineIdLang );
}
// create a list that maps from line detection model ID to line detection model name
$list = [];
foreach ( $lineIdLangs as $lineIDKey ) {
$list[$filteredLangList[$lineIDKey]['line']] = $lineIDList[$lineIDKey];
}
// return only the line detection model IDs
if ( $onlyLineIds ) {
return array_keys( $list );
}
return $list;
}
/**
* Get name of the given line detection model from the language code
* @param string|null $lineIdLang
* @return string
*/
public function getLineIdModelName( ?string $lineIdLang = null ): string {
return self::LINE_ID_MODEL_NAMES[$lineIdLang];
}
/**
* Set the line detection model ID for the Transkribus engine
* @param int $lineId
* @return void
*/
public function setLineId( int $lineId ): void {
$this->lineId = $lineId;
}
/**
* @inheritDoc
* @throws OcrException
*/
public function getResult(
string $imageUrl,
string $invalidLangsMode,
array $crop,
?array $langs = null,
int $rotate = 0
): EngineResult {
$this->checkImageUrl( $imageUrl );
$points = '';
if ( $crop ) {
$x = $crop['x'];
$y = $crop['y'];
$yPlusH = $crop['y'] + $crop['height'];
$xPlusW = $crop['x'] + $crop['width'];
$points = $x . ',' . $y . ' ' . $xPlusW . ',' .
$y . ' ' . $xPlusW . ',' . $yPlusH . ' ' . $x . ',' . $yPlusH;
}
$htrModelId = 0;
[ $validLangs, $invalidLangs ] = $this->filterValidLangs( $langs, $invalidLangsMode );
if ( !$validLangs ) {
throw new OcrException( 'transkribus-no-lang-error' );
}
if ( count( $validLangs ) > 1 ) {
throw new OcrException( 'transkribus-multiple-lang-error' );
}
$modelCode = $validLangs[0];
$modelInfo = $this->getModelList()[$modelCode];
$htrModelId = (int)$modelInfo['htr'];
$image = $this->getImage( $imageUrl, $crop, self::DO_DOWNLOAD_IMAGE );
if ( $rotate !== 0 ) {
$imagine = new Imagine();
$loaded = $imagine->load( $image->getData() );
$loaded->rotate( $rotate );
$image->setData( $loaded->get( 'jpg' ) );
}
$processId = $this->transkribusClient->initProcess( $image, $htrModelId, $this->lineId, $points );
$resText = '';
while ( $this->transkribusClient->processStatus !== 'FINISHED' ) {
$resText = $this->transkribusClient->retrieveProcessResult( $processId );
sleep( 2 );
}
$warnings = $invalidLangs ? [ $this->getInvalidLangsWarning( $invalidLangs ) ] : [];
return new EngineResult( $resText, $warnings );
}
}