73 lines
2.2 KiB
PHP
73 lines
2.2 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Core\Service\Google;
|
|
|
|
|
|
use App\Exceptions\ApiErrorException;
|
|
use Exception;
|
|
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class GoogleVisionLabelService
|
|
{
|
|
private $googleVisionAuthentication ;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->googleVisionAuthentication = base_path().'/resources/data/google-vision-authentication.json';
|
|
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$this->googleVisionAuthentication );
|
|
}
|
|
|
|
public function getImageGoogleVisionLabels($params)
|
|
{
|
|
$photoLabelList = [];
|
|
try
|
|
{
|
|
$imagePath = $params['image_paths'];
|
|
$image = file_get_contents($imagePath);
|
|
$imageAnnotator = new ImageAnnotatorClient();
|
|
$responseImageAnnotator = $imageAnnotator->labelDetection($image);
|
|
$getLabelAnnotations = $responseImageAnnotator->getLabelAnnotations();
|
|
$getLabelAnnotations = $getLabelAnnotations ? $getLabelAnnotations : null;
|
|
|
|
if( empty($getLabelAnnotations) )
|
|
{
|
|
throw new ApiErrorException(lang('Not tags found!'));
|
|
}
|
|
|
|
if ($getLabelAnnotations) {
|
|
$i = 1;
|
|
foreach ($getLabelAnnotations as $labelAnnotation)
|
|
{
|
|
$photoLabelList[] =
|
|
[
|
|
'description' => $labelAnnotation->getDescription(),
|
|
'score' => $labelAnnotation->getScore(),
|
|
'topicality' => $labelAnnotation->getTopicality(),
|
|
];
|
|
if($i == 3){
|
|
break;
|
|
}
|
|
$i++;
|
|
}
|
|
|
|
Log:info($photoLabelList);
|
|
}
|
|
|
|
|
|
|
|
$imageAnnotator->close();
|
|
|
|
}catch (Exception $e)
|
|
{
|
|
$message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
|
|
Log::error($message);
|
|
}
|
|
|
|
return $photoLabelList;
|
|
}
|
|
|
|
|
|
}
|