在 Codeigniter 中使用 Google Cloud Vision,找不到 Class ImageAnnotatorClient

Use Google Cloud Vision in Codeigniter, Cannot Find Class ImageAnnotatorClient

我想使用 google 云视觉实现 google 云视觉文本检测。

我已经从 google cloud vision 安装了 codeignier 中的 composer 到第三方供应商。

我在构造中的设置是:

include APPPATH . 'third_party/vendor/autoload.php';
        require_once APPPATH.'third_party/vendor/google/cloud-vision/src/V1/ImageAnnotatorClient.php';          

我调用 OCR 的函数是:

function upload_ocr_image()
    {               
        if (count($_FILES) === 0) { echo 'no image received from unity'; }

        $phone_code = $this->input->post('phone_code', true);
        $phone_number = $this->input->post('phone_number', true);

        $allowedType = array(IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG);       
        $imgType = exif_imagetype($_FILES['ocr_image']['tmp_name']);

        if(!in_array($imgType,$allowedType))
        {
            echo "Images Type Error. Images Type Only : GIF , JPEG, PNG";
            exit;
        }
        else
        {

            //upload original size front end slider
            $config['upload_path'] = './assets/ocr_image/';
            $config['allowed_types'] = '*';
            $config['file_name'] = $phone_code.$phone_number.".jpg";
            $config['overwrite'] = TRUE;
            $config['max_size'] = '8096';
            $config['max_width']  = '8000';
            $config['max_height']  = '8000';


            $this->load->library('upload', $config);

            $this->upload->initialize($config);
            if(!$this->upload->do_upload("ocr_image"))
            {
                echo "Maximum File Size Only 2 Mb Or Max Width = 2000 , Height = 2000";
                exit;
            }
            else
            {
                $img_data = $this->upload->data();

                // Authenticating with a keyfile path.

                $imageAnnotator = new ImageAnnotatorClient([
                    'credentials' => base_url().'assets/google_cloud_vision/keyfile.json'
                ]);

                # annotate the image
                $response = $imageAnnotator->textDetection($img_data['full_path']);
                $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: Class 'ImageAnnotatorClient' not found

这是哪一行:

$imageAnnotator = new ImageAnnotatorClient([

什么可能导致错误?从上面的构造中,我已经包含或 require_once Class.

的路径

这里有什么我遗漏的吗?

谢谢

这就是答案。

检查此 post :Namespace in PHP CodeIgniter Framework

只需在 config.php

底部添加检测命名空间的代码