【发布时间】:2020-09-02 22:35:00
【问题描述】:
我在使用 google vision api TEXT_DETECTION 和 DOCUMENT_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;
【问题讨论】: