ThijsFeryn / google-cloud-vision-api

A wrapper around the Google Cloud Vision API written in PHP that depends on Guzzle
1 stars 1 forks source link

How to use Label Dectection for multiple images? #2

Closed brucehieu96 closed 6 years ago

brucehieu96 commented 6 years ago

Hello, sr for the wrong place that i asked u 'cause i've just used GitHub for a few days! After seeing your hint in the old place, i'm still not understand what you mean and how to do it! Can you describe more details? I mean i'll use the loop for each image with the label dectection feature, is it right?

ThijsFeryn commented 6 years ago

Hi,

Here's an example on how to load multiple images from the filesystem and process them using label detection:

<?php
require dirname(__DIR__).'/vendor/autoload.php';
$api = new GoogleCloudVision\Api(getenv('apiKey'));
foreach (new DirectoryIterator(dirname(__FILE__).'/img') as $fileInfo) {
    if($fileInfo->isDot()
        || !$fileInfo->isFile()
        || !preg_match('/^(jpg|png)$/',$fileInfo->getExtension()))
            continue;
    $api->addImageByFilename($fileInfo->getPathname());
}
$api->addFeature($api::FEATURE_LABEL_DETECTION,10);
$result = $api->request();

foreach($result as $key=>$value) {
  var_dump($key,$value);
}

The script assumes that there is an img folder in the current directory containing either jpg or png files that need to be processed. I'm using a DirectoryIterator to scan the img folder for images.

You could also refactor this script and load the data via a collection of URLs as well.

Hope this helps!

brucehieu96 commented 6 years ago

Thank you so so much! I extremely appriciate this!

ThijsFeryn commented 6 years ago

No worries, glad I could help.

brucehieu96 commented 6 years ago

'Cause i'm doing the Final Project like Instagram, but i wanna give something differences, so i wanna add google vision to my project to get the tags of each image automatically. Thanks to your api library, i can solve 70% my problem!