我在尝试使用google vision API时收到此错误消息
Fatal error: Uncaught Error: Class 'Google\Cloud\Vision\VisionClient' not found in C:\xampp\htdocs\index.php:7 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php on line 7我在windows上用的是xampp。我使用composer安装了Google api (以管理员身份)
composer require google/cloud-vision我也运行(以admin身份)
composer install
composer updategoogle cloud sdk已安装。
这是我的代码
<?php
require 'C:\Users\MyUser\vendor\autoload.php';
use Google\Cloud\Vision\VisionClient;
$path = 'caption.jpg';
$vision = new VisionClient([
'projectId' => 'my-project-numbers',
'keyFilePath' => 'my-key.json'
]);
// Annotate an image, detecting faces.
$image = $vision->image(
fopen($path, 'r'),
['text']
);
$tadaa = $vision->annotate($image);
echo '<pre>';
var_dump($tadaa->text());
echo '</pre>';
?>发布于 2020-10-09 18:41:47
根据官方文档,我建议使用这个php客户端库:
# imports the Google Cloud client library
use Google\Cloud\Vision\V1\ImageAnnotatorClient;发布于 2020-10-09 19:46:50
使用https://cloud.google.com/vision/docs/ocr中的代码
下面是index.php
<?php
namespace Google\Cloud\Samples\Vision;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
$path = 'caption.jpg';
$imageAnnotator = new ImageAnnotatorClient();
# annotate the image
$image = file_get_contents($path);
$response = $imageAnnotator->textDetection($image);
$texts = $response->getTextAnnotations();
printf('%d texts found:' . PHP_EOL, count($texts));
foreach ($texts as $text) {
print($text->getDescription() . PHP_EOL);
# get bounds
$vertices = $text->getBoundingPoly()->getVertices();
$bounds = [];
foreach ($vertices as $vertex) {
$bounds[] = sprintf('(%d,%d)', $vertex->getX(), $vertex->getY());
}
print('Bounds: ' . join(', ', $bounds) . PHP_EOL);
}
$imageAnnotator->close();
?>我还是得到了
Fatal error: Uncaught Error: Class 'Google\Cloud\Vision\V1\ImageAnnotatorClient' not found in C:\xampp\htdocs\index.php:8 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php on line 8这是composer.json
{
"require": {
"google/cloud-vision": "^1.2"
}
}https://stackoverflow.com/questions/64278132
复制相似问题