

Laravel 8 Optical Character Recognition using Google Cloud Vision
source link: https://www.laravelcode.com/post/laravel-8-optical-character-recognition-using-google-cloud-vision
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Laravel 8 Optical Character Recognition using Google Cloud Vision
Google cloud vision is image OCR to get json details from the images. It helps to detect objects and faces, texts, and build valuable image with Google Cloud Vision.
In this article we will see how you can add Google Cloud Vision in your Laravel application and get text, object details from the images. Follow the below steps to implement in your application.
1 Create Google Cloud console account
Before you start working on Laravel, first create an account to Google and Go to create or manage project. Make sure you have enabled Cloud Vision API to your Google Console.
Get your Google key from the credentials menu.
2. Laravel setup
First open the CMD or Terminal to your computer and create a fresh Laravel application using below command.
composer create-project laravel/laravel imageocr
When your Laravel is setup, in .env file add the below line with Google key.
GOOGLE_CLOUD_KEY= your api key
Now install the Google's cloud vision package for PHP.
composer require google/cloud-vision
3. Create routes and Controller
First create routes in routes/web.php
file.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\GoogleOCRController;
Route::get('google-ocr', [GoogleOCRController::class, 'index'])->name('index');
Route::post('google-ocr', [GoogleOCRController::class, 'submit'])->name('submit');
Create controller file to handle routes.
php artisan make:controller GoogleOCRController
Open the app/Http/Controller/GoogleOCRController.php file and add these two methods.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\Likelihood;
class GoogleOCRController extends Controller
{
/**
* open the view.
*
* @param
* @return void
*/
public function index()
{
return view('googleOcr');
}
/**
* handle the image
*
* @param
* @return void
*/
public function submit(Request $request)
{
if($request->file('image')) {
// convert to base64
$image = base64_encode(file_get_contents($request->file('image')));
$client = new ImageAnnotatorClient();
$client->setImage($image);
$client->setFeature("TEXT_DETECTION");
$google_request = new GoogleCloudVision([$client], env('GOOGLE_CLOUD_KEY'));
$response = $google_request->annotate();
dd($response);
}
}
}
4. Create resource/views/googleOcr.blade.php blade file for upload image.
<!DOCTYPE html>
<html>
<head>
<title>Google Cloud Vision OCR</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row mt-3">
<form method="post" action="{{ route('submit') }}" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label for="image" class="form-label">Select image:</label>
<input type="file" id="image" name="image" class="form-control">
</div>
<button type="submit" class="btn btn btn-primary">Submit</button>
</form>
</div>
</div>
</body>
</html>
Now run the Laravel server
php artisan serve
And go to the http://localhost:8000/google-ocr
in your web browser. Upload the image and you will get the image deails response in json format.
Thank you for giving time to read the article.

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Recommend
-
6
What is Optical character recognition? 06/21/2021 2 minutes to read In this article Optical character recognition (OCR) allows you to extra...
-
6
Optical character recognition From Wikipedia, the free encyclopedia Jump to navigation
-
11
Optical Character Recognition: How the Hell Does It Work?September 23rd 2021 new story
-
10
Optical character recognition with TensorFlow Lite Optical character recognition with TensorFlow Lite: A new example app September 27, 2021 Posted by Wei Wei, TensorFlow Developer Advocate
-
9
Reading Handwritten Pages With Azure and Optical Character Recognition An introduction to optical character recognition (OCR) in the cloud and how to use it from your applications. Learn about image-to...
-
8
Using the Google Optical Character Recognition APIJune 17th 2022 new story4
-
7
AI Inference Software Fundamentals: Getting Started with Optical Character Recognition ...
-
4
Welcome to DeskTranslate!
-
8
Dig Security adds optical character recognition to help find sensitive information in image files
-
12
Exploring Optical Character Recognition (OCR): An Experiment with OpenCV and PyTesseract
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK