2

I am trying to POST HTTP request to Detect Text in images converted to base64 from mat image. When I run the code, I get

{
   "responses":[
      {
         "error":{
            "code":3,
            "message":"Bad image data."
         }
      }
   ]
}

My curl POST field is a JSON string like this:

{
   "requests":[
      {
         "image":{
            "content":"lZ+elp+elp+elp+elZ+elZ6dlZ6dlZ6dlJ6dlJ2ck52ck52ck52ck52ck52bk5ybkpyakpyakpyakZuZkJqYj5m...........srW0srWzsrWzsrWzsrWz"
         },
         "features":[
            {
               "type":"TEXT_DETECTION"
            }
         ]
      }
   ]
}

I am not sure if the base64 encoded image is valid. Please help !

Darius
  • 10,762
  • 2
  • 29
  • 50
nocturnal_abu
  • 57
  • 2
  • 9
  • Your request body structure is fine. The problem is with the Base64 string. It might be converted wrongly or you might paste it faulty. Can you reconvert the string to image to double check. You can use this [site](https://codebeautify.org/base64-to-image-converter). Bear in mind [these](https://cloud.google.com/vision/docs/supported-files) are the support image formats and there is 20MB size limitation. – Yurci Aug 08 '18 at 11:07
  • 1
    In any case you can try Vision API [Drag and Drop](https://cloud.google.com/vision/docs/drag-and-drop) or try the API with a different image format. – Yurci Aug 08 '18 at 11:08
  • I'm still stuck with this issue. I have tried the drag and drop tool, which works well, and made sure that the request I'm sending is identical to the one sent by google itself in the drag and drop tool, and still I get the "Bad image data" response... any ideas what could be wrong? – Luuc van der Zee Aug 01 '23 at 11:19

1 Answers1

0

I have found a solution by converting the Mat object into vector and then encode.

std::vector<uchar> array;
cv::imencode(".png",mat_img, array);


std::string encoded = Base64::encode(array);
nocturnal_abu
  • 57
  • 2
  • 9