I have a strange experience using google vision api TEXT_DETECTION
and DOCUMENT_TEXT_DETECTION
. When an image has a character <
(LESS THAN) or >
(GREATER THAN) , it stops OCR, and return the values before it.
Sample images: [1]: https://i.stack.imgur.com/YlA5q.jpg [2]: https://i.stack.imgur.com/Z6Mt8.jpg
My code is below:
$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;
I tried to perform simple curl request and I obtained the full results. I followed:
{
"requests": [
{
"image": {
"source": {
"imageUri": "gs://bucket/folder/YlA5q.jpg"
}
},
"features": [
{
"type": "TEXT_DETECTION"
}
]
}
]
}
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"
where
U+003C < Less-than sign
U+003E > Greater-than sign
See What does \< mean? and the official doc .
Therefore, OCR did not stop for me on the </> signs. Check that this simple request yields the same result.
I am not familiar with the PHP client library so I am uncertain where the possible mistake in your code might be. Have you checked that pictures without >/< signs work properly? My assumption is that the issue is related with encoding.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.