Issues with base64 decoding failed

2,742 views
Skip to first unread message

JeevanAnand Anne

unread,
Jun 6, 2018, 1:31:55 PM6/6/18
to cloud-vision-discuss
Hello,

I am trying to extract text from an image. I tried doing it in Python and was able to do Label Detection and Text Detection. I am able to get the response back.

from google.cloud import vision
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="vision.json"
import base64
client = vision.ImageAnnotatorClient()
image_path ='Outlook.png'
with open(image_path, 'rb') as image:
    content = image.read()
    print(content)
    #image_content = base64.b64encode(content)
    response = client.annotate_image({'image': {'content': content}, 'features': [{'type': vision.enums.Feature.Type.TEXT_DETECTION}],})
    print(response)

Unfortunately, I am passing the same requests '{"requests":  [{ "features":  [ {"type" : "TEXT_DETECTION", maxResults: 3}], "image": {"content": "' + response.getBody()  + '"}}]}'; in Javascript. I am trying to call the Google Cloud Vision API from third party tool. But I am receiving the below error. I am able to get the similar content (binary data of an image) in python as well as in Javascript, passing the same content, but I am passing the content to Google Cloud REST API. Any suggestions in resolving the issue.

"{\n  \"error\": {\n    \"code\": 400,\n    \"message\": \"Invalid value at 'requests[0].image.content' (TYPE_BYTES), Base64 decoding failed for \\\"�PNG\\r\\n\\u001a\\n\\u0000\\u0000\\u0000\\rIHDR\\u0000\\u0000\\u00040\\u0000\\u0000\\u0001�\\b\\u0006\\u0000\\u0000\\u0000,�R�\\u0000\\u0000\\f\\u0015iCCPICC Profile\\u0000\\u0000H��W\\u0007XS�\\u0016�[R\\b\\t-\\u0010\\u0001)�7A�t�\\bH\\u0007\\u001b!\\tI(\\u0001\\u0012��\\u001d]Tp-��bEWElk\\u0001d��ba\\u0011�����ʺX��ʛ\\u0014�����;\\u007fΜs�?sϝ�\\u0000�j����F�\\u0000�\\u0011\\u0016�bC���)�L�\\u0013�\\u0001}@\\u0001f��b���bb\\\"\",\n    \"status\": \"INVALID_ARGUMENT\",\n    \"details\": [\n      {\n        \"@type\": \"type.googleapis.com/google.rpc.BadRequest\",\n        \"fieldViolations\": [\n          {\n            \"field\": \"requests[0].image.content\",\n            \"description\": \"Invalid value at 'requests[0].image.content' (TYPE_BYTES), Base64 decoding failed for \\\"�PNG\\r\\n\\u001a\\n\\u0000\\u0000\\u0000\\rIHDR\\u0000\\u0000\\u00040\\u0000\\u0000\\u0001�\\b\\u0006\\u0000\\u0000\\u0000,�R�\\u0000\\u0000\\f\\u0015iCCPICC Profile\\u0000\\u0000H��W\\u0007XS�\\u0016�[R\\b\\t-\\u0010\\u0001)�7A�t�\\bH\\u0007\\u001b!\\tI(\\u0001\\u0012��\\u001d]Tp-��bEWElk\\u0001d��ba\\u0011�����ʺX��ʛ\\u0014�����;\\u007fΜs�?sϝ�\\u0000�j����F�\\u0000�\\u0011\\u0016�bC���)�L�\\u0013�\\u0001}@\\u0001f��b���bb\\\"\"\n          }\n        ]\n      }\n    ]\n  }\n}\n"


Regards,
Jeevan

Xinjie Zheng

unread,
Jun 6, 2018, 1:35:22 PM6/6/18
to JeevanAnand Anne, cloud-vision-discuss
Hi Jeevananand,

You can use e.g. the developer tool to intercept outgoing requests.
Without further information my guess is you probably didn't set up the correct header, e.g.
Content-Type: application/json

--
You received this message because you are subscribed to the Google Groups "cloud-vision-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-vision-discuss+unsub...@googlegroups.com.
To post to this group, send email to cloud-vision-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cloud-vision-discuss/bc522b50-b655-4891-b127-3deed57a0866%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Xinjie Zheng
郑欣杰
Software Engineer

JeevanAnand Anne

unread,
Jun 6, 2018, 1:43:13 PM6/6/18
to cloud-vision-discuss
Hello Xinjie,

I have setup the correct header. Please find below the code which I have written. As the image is local, passing the binary content to the API. I am able to get the response back in Python using google cloud library with json credentials, having issues sending to REST API.

var contentBody = '{"requests":  [{ "features":  [ {"type" : "TEXT_DETECTION", maxResults: 3}], "image": {"content": "' + response.getBody()  + '"}}]}';

var restMessage = new sn_ws.RESTMessageV2();
        restMessage.setBasicAuth("admin", "admin");
        restMessage.setHttpMethod("post");
        restMessage.setRequestHeader("Content-type", "application/json");
        restMessage.setEndpoint("https://vision.googleapis.com/v1/images:annotate?key=< KEY >");
        restMessage.setRequestBody(contentBody);
        var response = restMessage.execute();

Regards,
Jeevan Anne
To post to this group, send email to cloud-visi...@googlegroups.com.

George Du

unread,
Jun 6, 2018, 6:13:09 PM6/6/18
to jeeva...@gmail.com, cloud-visi...@googlegroups.com
Hi Jeevan, can you try base64 encoding the image before sending it via REST? The client library uses gRPC whereas when you use http, you must use JSON with a b64-encoded input.

To unsubscribe from this group and stop receiving emails from it, send an email to cloud-vision-dis...@googlegroups.com.



--
Xinjie Zheng
郑欣杰
Software Engineer

--
You received this message because you are subscribed to the Google Groups "cloud-vision-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-vision-dis...@googlegroups.com.

To post to this group, send email to cloud-visi...@googlegroups.com.

JeevanAnand Anne

unread,
Jun 7, 2018, 1:21:36 PM6/7/18
to cloud-vision-discuss
Hello George,

Thanks. I was able to find sort out after base64 encoding. 

Regards,
Jeevan Anne
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-vision-discuss+unsub...@googlegroups.com.



--
Xinjie Zheng
郑欣杰
Software Engineer

--
You received this message because you are subscribed to the Google Groups "cloud-vision-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cloud-vision-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages