首页 > 解决方案 > 谷歌视觉 API 中的奇怪错误

问题描述

我在使用 google vision apiTEXT_DETECTIONDOCUMENT_TEXT_DETECTION. 当图像有字符<(LESS THAN) 或>(GREATER THAN) 时,它会停止 OCR,并返回它之前的值。

示例图片:[1]:https ://i.stack.imgur.com/YlA5q.jpg [2]:https ://i.stack.imgur.com/Z6Mt8.jpg

我的代码如下:

$url = "https://vision.googleapis.com/v1/images:annotate?key=[API_KEY_HERE]";
$detection_type = "DOCUMENT_TEXT_DETECTION";
//$detection_type = "TEXT_DETECTION";
$image_validation = array('image/jpeg','image/png','image/gif');

if($_FILES){

    // validate uploaded file for allowed mime type
    if(in_array($_FILES['image']['type'],$image_validation)){

        // base64 encode image
        $image = file_get_contents($_FILES['image']['tmp_name']);
        $image_base64 = base64_encode($image);

        $json_request ='{
                "requests": [
                    {
                    "image": {
                        "content":"' . $image_base64. '"
                      },
                      "features": [
                          {
                            "type": "' .$detection_type. '",
                            "maxResults": 200
                          }
                      ]
                    }
                ]
            }';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $json_request);
        $json_response = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);


        // verify if we got a correct response
        if ( $status != 200 ) {
            die("Something when wrong. Status code: $status" );
        }

        // create an image identifier for the uploaded file
        switch($_FILES['image']['type']){
            case 'image/jpeg':
                $im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
                break;
            case 'image/png':
                $im = imagecreatefrompng($_FILES['image']['tmp_name']);
                break;
            case 'image/gif':
                $im = imagecreatefromgif($_FILES['image']['tmp_name']);
                break;
        }

        // transform the json response to an associative array
        $response = json_decode($json_response, true);
        // display the first text annotation
        //print_r($response);

        $output = $response['responses'][0]['textAnnotations'][0]['description'];
        echo $output;

标签: phpgoogle-cloud-vision

解决方案


我尝试执行简单的 curl 请求并获得了完整的结果。我跟着:

  1. 创建 request.json
{
  "requests": [
    {
      "image": {
        "source": {
          "imageUri": "gs://bucket/folder/YlA5q.jpg"
        }
       },
       "features": [
         {
           "type": "TEXT_DETECTION"
         }
       ]
    }
  ]
}
  1. 提交 curl 请求
curl -X POST -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -H "Content-Type: application/json; charset=utf-8" -d @request.json https://vision.googleapis.com/v1/images:annotate
  1. 我得到了结果
"text": "C\u003e\u003ex\u003c\u003cSEUNGWAN\u003c\u003c\u003c\u003c\n001M123123123\nCHAE seungwan\n"

在哪里

U+003C < 小于号

U+003E > 大于号

请参阅\u003C 是什么意思?官方文档

因此,OCR 并没有在 </> 标志上为我停下来。检查这个简单的请求是否产生相同的结果。

我不熟悉 PHP 客户端库,所以我不确定您的代码中可能出现的错误在哪里。您是否检查过没有 >/< 标志的图片是否正常工作?我的假设是这个问题与编码有关。


推荐阅读