微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

谷歌视觉API中的奇怪错误

如何解决谷歌视觉API中的奇怪错误

我在使用Google视觉API TEXT_DETECTIONDOCUMENT_TEXT_DETECTION时遇到了奇怪的经历。 当图像的字符为<(小于)或>(大于)时,它将停止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,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;

解决方法

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

  1. 创建request.json
{
  "requests": [
    {
      "image": {
        "source": {
          "imageUri": "gs://bucket/folder/YlA5q.jpg"
        }
       },"features": [
         {
           "type": "TEXT_DETECTION"
         }
       ]
    }
  ]
}
  1. 提交卷曲请求
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>大于号

请参见What does \u003C mean?official doc

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

我不熟悉PHP客户端库,因此不确定代码中可能的错误在哪里。您是否检查了没有> /

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。