Google Cloud Vision / PHP - 使用标签和安全搜索检测发出单个请求
Google Cloud Vision / PHP - Make single request with Label and Safe Search detection
在 google Cloud Vision 上,您需要按请求付费。如果你执行 "Label Detection" 你会得到一个免费的 "Safe Search" 但它必须被整合到同一个请求中。我有标签检测和安全搜索检测的工作代码,但我不确定如何将两者合并为一个请求。
有人在 Python 中回答了这个问题,但不确定如何在 PHP 中翻译它。
有人知道我如何在 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);
使用ImageAnnotatorClient::annotateImage
:
use Google\Cloud\Vision\V1\Feature\Type;
$res = $v->annotateImage(file_get_contents($fileName), [
Type::LABEL_DETECTION,
Type::SAFE_SEARCH_DETECTION
]);
$labels = $res->getLabelAnnotations();
$safeSearch = $res->getSafeSearchAnnotation();
在 google Cloud Vision 上,您需要按请求付费。如果你执行 "Label Detection" 你会得到一个免费的 "Safe Search" 但它必须被整合到同一个请求中。我有标签检测和安全搜索检测的工作代码,但我不确定如何将两者合并为一个请求。
有人在 Python 中回答了这个问题,但不确定如何在 PHP 中翻译它。
有人知道我如何在 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);
使用ImageAnnotatorClient::annotateImage
:
use Google\Cloud\Vision\V1\Feature\Type;
$res = $v->annotateImage(file_get_contents($fileName), [
Type::LABEL_DETECTION,
Type::SAFE_SEARCH_DETECTION
]);
$labels = $res->getLabelAnnotations();
$safeSearch = $res->getSafeSearchAnnotation();