38 lines
944 B
PHP
38 lines
944 B
PHP
<?php
|
|
|
|
namespace App\Services\Ocr;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Process;
|
|
|
|
class TesseractOcrDriver implements OcrDriver
|
|
{
|
|
public function recognize(string $imagePath): string
|
|
{
|
|
if (! is_file($imagePath)) {
|
|
return '';
|
|
}
|
|
|
|
$bin = (string) config('coruna.ocr.tesseract', 'tesseract');
|
|
$langs = (string) config('coruna.ocr.langs', 'eng+chi_sim');
|
|
$result = Process::timeout(90)->run([
|
|
$bin,
|
|
$imagePath,
|
|
'stdout',
|
|
'-l', $langs,
|
|
'--psm', '6',
|
|
]);
|
|
|
|
if (! $result->successful()) {
|
|
Log::channel('stack')->warning('tesseract ocr failed', [
|
|
'path' => $imagePath,
|
|
'error' => mb_substr($result->errorOutput() ?: $result->output(), 0, 500),
|
|
]);
|
|
|
|
return '';
|
|
}
|
|
|
|
return trim($result->output());
|
|
}
|
|
}
|