OCR Integration in Laravel using LaraOCR Package with Tesseract

By Ved Prakash N | Jan 05, 2024 | Laravel
Share : Whatsapp

https://www.fundaofwebit.com/post/ocr-integration-in-laravel-using-laraocr-package-with-tesseract

OCR Integration in Laravel using LaraOCR Package with Tesseract

How to convert image to text in Laravel


In this post, you will be learning about how to convert image to text in Laravel using LaraOCR Package with Tesseract OCR.


Requirements

OCR Engine Tesseract should be install in the system.
Follow Tesseract installation guide here( https://github.com/tesseract-ocr/tessdoc?tab=readme-ov-file#compiling-and-installation ). 
Make sure from the command line you have the tesseract command available.


Install on Windows (Tesseract installer for Windows) :

Go to doc : https://github.com/UB-Mannheim/tesseract/wiki 

Check for the Latest installer :- Current Latest is (05-01-2024) : tesseract-ocr-w64-setup-5.3.3.20231005.exe (64 bit) 

Note: While Installation, Copy the Destination Path : C:\Program Files\Tesseract-OCR


Once Tesseract OCR is installed in your system, you need to check the Tesseract Version, so there are 2 ways to check it:

Option 1: Specify the Full Path

Open new command prompt and paste the below Full Path to the Tesseract executable.

"C:\Program Files\Tesseract-OCR\tesseract.exe" --version


Option 2: Add Tesseract to System PATH
  1. The default installation directory on Windows is usually C:\Program Files\Tesseract-OCR.
  2. Add Tesseract to System PATH:
    * Open the Start menu and search for "Environment Variables" or "Edit the system environment variables."
    * Click on "Environment Variables."
    * Under "System variables," find the "Path" variable, select it, and click "Edit."
    * Click "New" and add the path to the directory where Tesseract is installed (e.g., C:\Program Files\Tesseract-OCR).
    * Click "OK" to close each dialog box.
  3. Open new command prompt and run the below command
tesseract --version



Step 1: Install Laravel Application

composer create-project laravel/laravel example-app


Step 2: Install LaraOCR Package

composer require alimranahmed/laraocr

after successful installation push this package using the following command:

php artisan vendor:publish --provider="Alimranahmed\LaraOCR\LaraOCRServiceProvider"


Step 3: Create a Controller named OcrController.php with the artisan command:

php artisan make:controller OcrController

after successfully creating controller, paste the below code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Alimranahmed\LaraOCR\Facades\OCR;

class OcrController extends Controller
{
    public function ocrImage()
    {
        $imagePath = public_path('images/img-1.png');

        $ocrText = OCR::scan($imagePath);

        // Use $ocrText as needed in your application
        return response()->json(['text' => $ocrText]);
    }
}


This is the Image I am using:


Step 4: Create a Route in routes/web.php as follows:

Route::get('/ocr', [App\Http\Controllers\OcrController::class, 'ocrImage']);


Step 5: Serve the application using artisan command:

php artisan serve


Finally, let's browse the URL so we can get the Text from the Image (The image is already added statically in the controller)

http://localhost:8000/ocr


Output:
{
  "text": "How are you ?"
}


I hope this helps you. Thanks for reading.