path = $path; } public function execute() { $this->preProcess(); $text = $this->extractText(); return $text; } protected function preProcess() { $this->applyDewarp(); $this->applyDeskew(); } protected function applyDewarp() { $executablePath = resource_path('python/dewarp/page_dewarp.py'); $process = new Process([ 'python3', $executablePath, $this->path, ]); $process->run(); if (!$process->isSuccessful()) { throw new ProcessFailedException($process); } $fileName = pathinfo($this->path, PATHINFO_FILENAME); $filePath = $fileName . '_thresh.png'; $directory = pathinfo($this->path, PATHINFO_DIRNAME); $newPath = "$directory/$filePath"; $moved = File::move(base_path($filePath), $newPath); if ( ! $moved) { throw new \Exception('Something went wrong while moving file.'); } $this->path = $newPath; } protected function applyDeskew() { $executablePath = resource_path('libraries/deskew/Bin/deskew'); $newPath = pathinfo($this->path, PATHINFO_DIRNAME) . '/deskewed.png'; $process = new Process([ $executablePath, $this->path, '-o', $newPath ]); $process->run(); if ( ! $process->isSuccessful()) { throw new ProcessFailedException($process); } $this->path = $newPath; } protected function extractText() { $t = new TesseractOCR($this->path); // $t->oem(4); $t->psm(4); return $t->run(); } }