Logo

Office Address

123/A, Miranda City Likaoli
Prikano, Dope

Phone Number

+0989 7876 9865 9

+(090) 8765 86543 85

Email Address

info@example.com

example.mail@hum.com

How to read or detect text from image using Google Vision API

How to read or detect text from image using Google Vision API

In this blog post, i will be able to allow you to skills to read content of images with Google Vision API.

Google Vision API can easily detect and extract text from a picture . There are two annotation features (TEXT_DETECTION, DOCUMENT_TEXT_DETECTION) that perform Optical Character Recognition and return the extracted text, if any.

In order to use Google Vision API, you'll need to create new project or select an existing project and enable the Vision API.

Below are the example to read the text from image using HTML, Css and PHP


<!DOCTYPE html>
<html>
<head>
    <title>How to read or detect text from image using Google Vision API </title>
</head>
<body>

    <form enctype="multipart/form-data" action="" method="POST">
        Choose an image to upload: <input name="image" type="file" /><br />
        <input type="submit" value="Upload Document" />
    </form>

<?php

$url = "https://vision.googleapis.com/v1/images:annotate?key={api_key}";
$detection_type = "TEXT_DETECTION";
$image_validation = array('image/jpeg','image/png','image/gif');

if($_FILES){

    // validate uploaded file for allowed mime type
    if(in_array($_FILES['image']['type'],$image_validation)){            

        // base64 encode image
        $image = file_get_contents($_FILES['image']['tmp_name']);
        $image_base64 = base64_encode($image);

        $json_request ='{
                  "requests": [
                    {
                    "image": {
                        "content":"' . $image_base64. '"
                      },
                      "features": [
                          {
                              "type": "' .$detection_type. '",
                              "maxResults": 200
                          }
                      ]
                    }
                ]
            }';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $json_request);
        $json_response = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);

        // verify if we got a correct response
        if ( $status != 200 ) {
            die("Something when wrong. Status code: $status" );
        }
        
        // create an image identifier for the uploaded file
        switch($_FILES['image']['type']){
            case 'image/jpeg':
                $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
                break;
            case 'image/png':
                $im = imagecreatefrompng($_FILES['image']['tmp_name']);
                break;
            case 'image/gif':
                $im = imagecreatefromgif($_FILES['image']['tmp_name']);
                break;
        }

        // transform the json response to an associative array
        $response = json_decode($json_response, true);            
        // display the first text annotation
        echo'<pre>';
        print_r($response['responses'][0]['textAnnotations'][0]['description']);
        echo'</pre>';
    
    }
    else{
        echo 'File type Invalied';
    }

    }
?>

</body>
</html>

Tags

  • How Google Vision API works

  • Google vision API pricing

  • Google vision API documentation

  • Google vision API demo
  • How to read or detect text from image using Google Vision API
  • Document text detection google vision

Related Posts

Would You Like to Exchange Guest Posts with Us? Unlock the Benefits of Guest Post Services

Would You Like to Exchange Guest Posts with Us? Unlock the Benefits of Guest Post Services

Guest posting has become one of the most effective strategies for building online visibility, authority, and driving organic traffic. By exchanging guest posts, businesses and bloggers can leverage ea

Read More
Features of Website Designing: The Key Elements for a Successful Website

Features of Website Designing: The Key Elements for a Successful Website

In today’s digital era, having a website is no longer an option but a necessity for businesses to thrive. A well-designed website not only attracts visitors but also builds trust and credibility

Read More
How to fix JavaScript is parsed during initial page load

How to fix JavaScript is parsed during initial page load

What is site page rendering?  Delivering is where everything the assets needed to show a page is called, and the whole page design is shown in the program. Now and again, it might happen that

Read More