php - 谷歌视觉 API 中的奇怪错误
问题描述
我在使用 google vision apiTEXT_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;
解决方案
我尝试执行简单的 curl 请求并获得了完整的结果。我跟着:
- 创建 request.json
{
"requests": [
{
"image": {
"source": {
"imageUri": "gs://bucket/folder/YlA5q.jpg"
}
},
"features": [
{
"type": "TEXT_DETECTION"
}
]
}
]
}
- 提交 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
- 我得到了结果
"text": "C\u003e\u003ex\u003c\u003cSEUNGWAN\u003c\u003c\u003c\u003c\n001M123123123\nCHAE seungwan\n"
在哪里
U+003C < 小于号
U+003E > 大于号
请参阅\u003C 是什么意思?和官方文档。
因此,OCR 并没有在 </> 标志上为我停下来。检查这个简单的请求是否产生相同的结果。
我不熟悉 PHP 客户端库,所以我不确定您的代码中可能出现的错误在哪里。您是否检查过没有 >/< 标志的图片是否正常工作?我的假设是这个问题与编码有关。
推荐阅读
- java - 准确的android线程处理程序postDelayed
- c - Apache httpd 模块:使用 SetHandler 时如何保留原始 POST“响应正文”?
- java - 是否可以使用java中的方法打破循环?
- java - 在 Java windowbuilder 中使用 for 循环生成多个按钮 n 次
- ruby - Ruby:嵌套散列在与另一个嵌套散列合并时丢失键和值
- python - Why am i getting index of the range error?
- excel - 用户表单文本框 - 英国日期格式
- regex - 正则表达式匹配不在特定目录中的所有路径
- go - Are ioutil.WriteFile file mode / permission constants stored anywhere?
- swift - How to display a text view in swift UI if the variable isn't nil?