Optical Character Recognition (OCR)

The Vision API can detect and extract text from images. There are two annotation features that support optical character recognition (OCR):

  • TEXT_DETECTION detects and extracts text from any image. For example, a photograph might contain a street sign or traffic sign. The JSON includes the entire extracted string, as well as individual words, and their bounding boxes.
  • DOCUMENT_TEXT_DETECTION also extracts text from an image, but the response is optimized for dense text and documents. The JSON includes page, block, paragraph, word, and break information.Learn more about DOCUMENT_TEXT_DETECTION for handwriting extraction and text extraction from files (PDF/TIFF).

Text detection requests

Set up your GCP project and authentication

If you have not created a Google Cloud Platform (GCP) project and service account credentials, do so now. Expand this section for instructions.

Detect text in a local image

The Vision API can perform feature detection on a local image file by sending the contents of the image file as a base64 encoded string in the body of your request.

Google vision api implementation with laravel 5.8

run command in composer

composer require google/cloud-vision

include file in controller file

use Google\Cloud\Vision\VisionClient;

$vision = new VisionClient(['keyFile' => json_decode(file_get_contents("key.json"), true)]); 
$image = $vision->image($base64image, 
        [
            'WEB_DETECTION',
            'TEXT_DETECTION'
        ]);
        
        $result = $vision->annotate($image);
        //print_r($result); exit;
        $texts = $result->text();
        $web = $result->web();
        foreach($texts as $key=>$text)
        {
            $description[]=$text->description();
        }
        // fetch text from image //
        print_r($description[0]);
        foreach ($web->entities() as $key=>$entity)
        {
            $entity_per=number_format(@$entity->info()['score'] * 100 , 2);
            if(isset($entity->info()['description']))
            {
                $match_condition[$entity_per]=['Identity'=>ucfirst($entity->info()['description'])];
            }
            else
            { 
                $match_condition[$entity_per]=['Identity'=>'N/A']; 
                
            }
        }
        //print best match//

       $best_match = current($match_condition);