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

时间:2019-04-01 02:53:32

标签: php codeigniter google-cloud-vision

我想使用Google Cloud vision实施Google Cloud vision textDetection。

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

我在构造中的设置是:

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();

            }
        }
    }

但是在进行文本检测之前,我遇到了一个错误:

  

致命错误:找不到类'ImageAnnotatorClient'

这是哪一行:

$ imageAnnotator = new ImageAnnotatorClient([

什么可能导致错误?从上面的构造中,我已经包含或require_once指向类的路径。

这里有我想念的东西吗?

谢谢

1 个答案:

答案 0 :(得分:0)

这就是答案。

查看此帖子:Namespace in PHP CodeIgniter Framework

只需在config.php底部添加代码以检测名称空间