How to use Google cloud vision API to detect logos in image upload in Laravel

How to use Google cloud vision API to detect logos in image upload in Laravel

Using Google cloud vision API to detect logos in image upload in Laravel application

In this article, we'll be looking at how to use the Google cloud vision API to detect logo in image upload in Laravel application.

Detect logos

Logo Detection is useful for detecting popular product or brand logos within an image file.

In order to be able to detect and retrieve information about product logos in an image the Google vision API comes handy.

In this tutorial How to use Google cloud vision API safe search detection to detect explicit content on image uploads in Laravel, we covered the details on how to create a Google Cloud Platform (GCP) project, service account credentials and cloud vision package integration into Laravel.

In order to keep this article brief we'll go straight to exploring how logos detection works on image file uploads because we already have a form for uploading files from the previous feature integration.

image.png

NB: each feature has its own route in the web.php file for clarity purposes.

You can also check into a specific branch on Github to go over its implementation.

  • Detect logos in a local image

Here's a repository with an example of its integration.

  • Import the classes
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
// this HtmlStringclass is used to format the text detected on the image
use Illuminate\Support\HtmlString;
  • Running the logoDetection on the uploaded image

    public function detectLogo(Request $request)
    {

        $request->validate([
            'avatar' => 'required|image|max:10240',
        ]);

        try {

            $imageAnnotator = new ImageAnnotatorClient([
                //we can also keep the details of the google cloud json file in an env and read it as an object here
                'credentials' => config_path('laravel-cloud.json')
            ]);

            # annotate the image
            $image = file_get_contents($request->file("avatar"));
            $response = $imageAnnotator->logoDetection($image);
            $logos = $response->getLogoAnnotations();

            $number_logos = count($logos);
            $logo_content = '';

            printf('%d logos found:' . PHP_EOL, count($logos));
            foreach ($logos as $logo) {
                $logo_content .= "{$logo->getDescription()}";
                print($logo->getDescription() . PHP_EOL);
            }

            $formatted_logo = new HtmlString($logo_content);

            return redirect()->route('home')
                ->with('success', "Logo detection successful!!! Number of Logos:  $number_logos Formatted Logos found on image uploaded: $formatted_logo");
        } catch (Exception $e) {
            return $e->getMessage();
        }
        $imageAnnotator->close();
    }
  • We'll upload this image and see how the Detect logos in an uploaded Image

Google_Homepage.svg.png

  • Logos detected in the image uploaded

image.png

  • Detect text in a remote image ( Google cloud storage, Cloudinary, S3 bucket etc)

In order to perform Vision API logo detection on remote image (Cloud or on the web) simply specify the file URL in the request.

Caution: When fetching images from HTTP/HTTPS URLs, Google cannot guarantee that the request will be completed. Your request might fail if the specified host denies the request (for example, due to request throttling or DoS prevention), or if Google throttles requests to the site for abuse prevention. As a best practice, don't depend on externally-hosted images for production applications.

Simply replace the upload file name with the image URL on the internet or remote storage

     $image = 'file_path...https://googleapis.com.......png';

            //run the image details detection feature on the image
            $response = $imageAnnotatorClient->logoDetection($image);

Here's the tutorial repository

Thank you for reading this article!!!.

If you find this article helpful, please share it with your network and feel free to use the comment section for questions, answers, and contributions.

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!