Integration of Google Cloud Vision in PHP

Machine learning approaches are getting popular rapidly. This approach saves a ton of time of manual work. While it’s not a guarantee of getting 100% correct results using machine learning, it does the stuff to some extent what it expects to do.

Google Cloud Vision runs on a machine learning model which allows us to integrate vision detection features in our application.

The examples of vision detection include:

  • Optical character recognition(OCR)
  • Landmark detection
  • Face detection
  • Image labeling

In this article, we study the first 2 approaches of OCR and landmark detection. If you are not aware of OCR then it is used to detect text from images. For landmark detection, I’ll use the image of the Taj Mahal and our script would name it correctly.

Create a Service Account on Google Cloud

To integrate the Cloud Vision API, you first need to create a service account in the cloud console. Below are the steps to create a service account. You can also get steps on this page.

  • In the Cloud Console, go to the Create service account page.
  • Create a project. You can also select an existing one.
  • Enable the Cloud Vision API for the project.
  • Create a service account.
  • Download a private key as JSON.

After this, you need to set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the downloaded JSON file.

The Window users can set it using the command prompt:

set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH

Whereas, using PowerShell command would be:

$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

Make sure to replace placeholder KEY_PATH with the JSON file path.

If you are on Linux or macOS machine, set the environment variable using the command:

export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

Integration of Google Cloud Vision in PHP

Once you have set up the environment variable, install the Cloud Vision library using the below command.

composer require google/cloud-vision

Upon installation of the library, we are able to interact with Vision API to perform different tasks like reading text from the image, detect a landmark from the image, and so on.

Read Text from Image

Google Cloud Vision uses the OCR technique to extract text from the image. I am going to use the below thumbnail from one of my YouTube videos.

youtube-thumbnail-phpmailer

Our goal is to pass this image to Cloud Vision and read its text programmatically. The code will be as follows.

<?php
require_once 'vendor/autoload.php';

use Google\Cloud\Vision\V1\ImageAnnotatorClient;

try {
    $imageAnnotatorClient = new ImageAnnotatorClient();

    $image_path = 'https://i3.ytimg.com/vi/oeVPsNBTWqU/hqdefault.jpg';
    $imageContent = file_get_contents($image_path);
    $response = $imageAnnotatorClient->textDetection($imageContent);
    $text = $response->getTextAnnotations();
    echo $text[0]->getDescription();

    if ($error = $response->getError()) {
        print('API Error: ' . $error->getMessage() . PHP_EOL);
    }

    $imageAnnotatorClient->close();
} catch(Exception $e) {
    echo $e->getMessage();
}

This code gives me output as ‘How TO SEND EMAIL USING GMAIL API WITH PΗPΜΑΙLE R’. It’s not totally correct in my case but it does the job as much as possible through machine learning. My actual image has ‘HOW’ in capital and no space before ‘R’ in ‘PHPMAILER’. These are little mistakes that may be fixed in future releases.

In the above code, I have passed the relative URL of the image. You can also use the absolute path.

Detect Landmark

Google Cloud Vision intelligently detects a landmark from the image. For instance, I’ll use the Taj Mahal image as shown below. I would send the content of this image and Cloud Vision detects its landmark through their image recognition technique.

Image Source: Unsplash

The code will be used as follows.

<?php
require_once 'vendor/autoload.php';

use Google\Cloud\Vision\V1\ImageAnnotatorClient;

try {
    $imageAnnotatorClient = new ImageAnnotatorClient();

    $path = 'https://source.unsplash.com/_FQOe94ooU8/640x426';
    $image = file_get_contents($path);
    $response = $imageAnnotatorClient->landmarkDetection($image);
    $landmarks = $response->getLandmarkAnnotations();
    printf('%d landmark found:' . "<br>", count($landmarks));
    foreach ($landmarks as $landmark) {
        print($landmark->getDescription() . "<br>");
    }

    $imageAnnotatorClient->close();
} catch(Exception $e) {
    echo $e->getMessage();
}

The output of this is also not totally correct. I was expecting a single output as ‘Taj Mahal’. But for some reason, it gives me the ‘Taj Mahal’ text twice. I am not sure why it’s happening but maybe this is something the limitation of the machine learning model.

Similar to the above examples, you can also perform tasks like Face detection, Image labeling, etc. I am not going to cover the code for these. Instead, you will get the source code on the GitHub page.

I hope you got to know the integration of Google Cloud Vision using PHP. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

2 thoughts on “Integration of Google Cloud Vision in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *