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.
Option 2: Add Tesseract to System PATH
- The default installation directory on Windows is usually C:\Program Files\Tesseract-OCR.
- 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. - Open new command prompt and run the below command
Step 1: Install Laravel Application
Step 2: Install LaraOCR Package
after successful installation push this package using the following command:
Step 3: Create a Controller named OcrController.php with the artisan command:
after successfully creating controller, paste the below code:
This is the Image I am using:
Step 4: Create a Route in routes/web.php as follows:
Step 5: Serve the application using artisan command:
Finally, let's browse the URL so we can get the Text from the Image (The image is already added statically in the controller)
Output:
{
"text": "How are you ?"
}
I hope this helps you. Thanks for reading.