Files
coruna-lab/app/Services/Ocr/TesseractOcrDriver.php
2026-09-14 03:32:56 +08:00

40 lines
1.0 KiB
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');
$oem = max(0, (int) config('coruna.ocr.oem', 1));
$psm = max(0, (int) config('coruna.ocr.psm', 6));
$result = Process::timeout(90)->run([
$bin,
$imagePath,
'stdout',
'-l', 'eng',
'--oem', (string) $oem,
'--psm', (string) $psm,
]);
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());
}
}