Google Cloud Vision / PHP-使用标签和安全搜索检测发出单个请求

时间:2019-02-14 05:38:25

标签: php google-api google-cloud-platform google-cloud-vision

在Google Cloud Vision上,您需要按请求付费。如果您执行“标签检测”,则会获得免费的“安全搜索”,但必须将其纳入相同的请求。我同时具有标签检测和安全搜索检测的工作代码,但是我不确定如何将两者组合为一个请求。

有人用Python回答了这个问题,但不确定如何用PHP进行翻译。

How to call for "Label Detection" and "Safe Search Detection" at a time on Google Cloud Vision API

有人知道我怎么用PHP称呼他们吗?任何见识将不胜感激。谢谢。


# imports the Google Cloud client library
use Google\Cloud\Vision\V1\ImageAnnotatorClient;

# instantiates a client
$imageAnnotator = new ImageAnnotatorClient();

# the name of the image file to annotate
$fileName = 'images/d4aed5533322946.jpg';

# prepare the image to be annotated
$image = file_get_contents($fileName);

# performs label detection on the image file
$response = $imageAnnotator->labelDetection($image);
$labels = $response->getLabelAnnotations();

if ($labels) {
    echo("Labels:" . PHP_EOL);
    foreach ($labels as $label) {
        echo($label->getDescription() . PHP_EOL);
    }
} 
#########安全搜索如下
function detect_safe_search($path)
{
    $imageAnnotator = new ImageAnnotatorClient();

    # annotate the image
    $image = file_get_contents($path);
    $response = $imageAnnotator->safeSearchDetection($image);
    $safe = $response->getSafeSearchAnnotation();

    $adult = $safe->getAdult();
    $medical = $safe->getMedical();
    $spoof = $safe->getSpoof();
    $violence = $safe->getViolence();
    $racy = $safe->getRacy();

    # names of likelihood from google.cloud.vision.enums
    $likelihoodName = ['UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY',
    'POSSIBLE','LIKELY', 'VERY_LIKELY'];

echo "Adult $adult\n";
    printf("Adult: %s" . PHP_EOL, $likelihoodName[$adult]);
    printf("Medical: %s" . PHP_EOL, $likelihoodName[$medical]);
    printf("Spoof: %s" . PHP_EOL, $likelihoodName[$spoof]);
    printf("Violence: %s" . PHP_EOL, $likelihoodName[$violence]);
    printf("Racy: %s" . PHP_EOL, $likelihoodName[$racy]);

    $imageAnnotator->close();
}

$path = 'images/d4aed5533322946.jpg';
detect_safe_search($path);

echo "\n";
$path = 'images/5.jpg.6f23b929dcc008f3bc394b0b6b0c6e5e.jpg';
detect_safe_search($path);

1 个答案:

答案 0 :(得分:0)

使用antlr.NoViableAltException: unexpected AST node: query

ImageAnnotatorClient::annotateImage