OCR with LARAVEL
📄

OCR with LARAVEL

Tags
OCR
LARAVEL
Published
April 16, 2023
Author
Oussama Belhadi

What’s OCR?

notion image
 
Optical Character Recognition (OCR) is a technology that enables computers to read and recognize text from images or scanned documents. OCR software can recognize and convert characters from printed documents, handwritten documents, and even from images. OCR technology has made it possible for companies and individuals to convert physical documents into digital format quickly and efficiently.
 
 
OCR technology has a wide range of use cases across various industries. In the healthcare industry, OCR is used to digitize medical records and automate the process of data entry. In the banking industry, OCR is used to scan and recognize the information on checks, invoices, and other financial documents. OCR technology has also become essential in the legal industry, where it is used to convert physical contracts and legal documents into digital format.
 

Use Cases

One of the most popular use cases for OCR technology is in the publishing industry. Publishers often have a vast library of printed books and documents that they want to digitize for electronic distribution. OCR technology enables them to quickly scan and recognize the text in these documents, making the process of digitizing books and other documents much faster and more efficient.
 
Another common use case for OCR is in the transportation industry. Companies use OCR to scan and recognize license plates, allowing them to automate toll collection, parking enforcement, and other transportation-related tasks. OCR technology can also be used to read shipping labels and track packages as they move through the supply chain.
 
OCR technology is also used extensively in the education industry. Teachers and professors can use OCR to digitize textbooks and other course materials, making it easier for students to access and study the content. OCR technology can also be used to automatically grade multiple-choice exams and scan and recognize student handwriting.
In addition to these use cases, OCR technology is also used in the government sector, where it is used to digitize government records and improve the efficiency of data entry. Law enforcement agencies use OCR to recognize text in scanned documents and images to aid in investigations.
In conclusion, OCR technology has become an essential tool for individuals and businesses in a wide range of industries. It has enabled us to convert physical documents into digital format quickly and efficiently, saving time and resources. With its ability to recognize text in images and scanned documents, OCR has revolutionized the way we store, manage, and access information.
Regenerate response.
 

How to use OCR with LARAVEL?

There are a bunch of open source contributors, who have been working on an OCR project and making it’s integration much more easier, they made it downloadable, here’s the download page.
After installing it you will have to add it’s PATH to the User Environment variables, to make sure that everything went as it should so far, you can launch the Terminal and run the following command :
tesseract -v
if you get that reply, then you’re doing good so far.
notion image
 
Now you can create a normal LARAVEL project, and install the package in your project using the following command :
composer require thiagoalessio/tesseract_ocr
Now create a HomeController where we will do all the testing for now, you could find the working example here in my repo.
 
it supports two languages by default, in order to use more languages, you will have to install their files and add them manually to the first installation Path
 
 
The Views :
layout.blade.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>OCR Test</title> <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> </head> <body> @yield('content') </body> </html>
 
home.blade.php
@extends('layout') @section('content') <div class="container"> <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2"> <div class="panel panel-info" > <div class="panel-heading"> <div class="panel-title">Upload</div> </div> <div style="padding-top:30px" class="panel-body" > <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div> <form method="post" action="{{route('upload')}}" enctype="multipart/form-data" class="form-horizontal" role="form"> @csrf <div style="margin-bottom: 25px" class="input-group"> <input type="file" name="image" /> </div> <div style="margin-top:10px" class="form-group"> <!-- Button --> <div class="col-sm-12 controls"> <input type="submit" class="btn btn-success"> </div> </div> <div style="margin-top:10px" class="form-group"> <!-- Button --> <div class="col-sm-12 controls"> <label>Result:</label> @if(Session::has('text')) {{Session::get('text')}} @endif </div> </div> </form> </div> </div> <h2>Here are the supported languages :</h2> <ul> @foreach($Defaultlanguages as $dl) <li>{{$dl}}</li> @endforeach </ul> </div> </div> @endsection
 
The Controller :
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use thiagoalessio\TesseractOCR\TesseractOCR; class HomeController extends Controller { public function index(){ //just initializing an array to show up the default languages $Defaultlanguages=[]; foreach((new TesseractOCR())->availableLanguages() as $lang) { $Defaultlanguages[] = $lang; }; return view('home',compact('Defaultlanguages')); } public function upload(Request $request){ $image = $request->file('image'); $filename= date('YmdHi').$image->getClientOriginalName(); $image-> move(public_path('images'), $filename); $ocr = new TesseractOCR(public_path("images/$filename")); $ocr->lang('ara','en'); $text = $ocr->run(); //using a session to send back the response to the view return redirect()->back()->with('text',$text); } }
 
In this example it’s only supports images for now.
You can find the full documentation and more usefull infos on how to use this package here