Google Cloud Vision API : "error code: 3" , "message": "Bad image data." - json

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 !

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);

Related

Free open-source Japanese APIs

Can't find anywhere namely API (REST API to receive JSON on request). What I'm looking for is:
Hiragana
Such a format:
{
ta: {
symbol: "た",
transliteration: "ta", // id
unicode: "\u305f",
pronunciation: "https://audio.db/.../ta.mp3" // Audio file or URL for audio file
}
}
// or
[
{
symbol: "た",
transliteration: "ta",
unicode: "\u305f",
pronunciation: "https://audio.db/.../ta.mp3"
}
]
Dictionary
Such a format:
{
neko: {
kanji: "猫",
kanjiUnicode: "\u732b",
hiragana: "ねこ",
// startsWith or hiraganaUnicode are required (I need to access first letter of the word)
hiraganaUnicode: "\u306d\u3053",
startsWith: "ne",
pronunciation: "https://audio.db/.../neko.mp3",
// picture or pictureSet would be useful
picture: "https://image.db/.../neko.webp",
pictureSet: ["https://image.db/.../neko1.webp", "https://image.db/.../neko2.webp"]
}
}
I have found this online dictionary. It seems quite good and accurate but the problem is it doesn't have documentation at all.

Parsing JSON to get only specific children not all the children

I have been trying to parse the following JSON data using JSON.Parse(), I only need the url tags inside images not the caption or resizedImageUrls.
{"images": [
{
"url": "https://media.IMG_0001.jpg",
"caption": "Photo1",
"resizedImageUrls": {
"size135x100": "https://media.IMG_0001_135x100.jpg",
"size476x317": "https://media.IMG_0001_476x317.jpg",
"size656x437": "https://media.IMG_0001_656x437.jpg"
}
},
{
"url": "https://media.IMG_0002.jpg",
"caption": "Photo2",
"resizedImageUrls": {
"size135x100": "https://media.IMG_0002_135x100.jpg",
"size476x317": "https://media.IMG_0002_476x317.jpg",
"size656x437": "https://media.IMG_0002_656x437.jpg"
}
},{
"url": "https://media.IMG_0003.jpg",
"caption": "Photo3",
"resizedImageUrls": {
"size135x100": "https://media.IMG_0003_135x100.jpg",
"size476x317": "https://media.IMG_0003_476x317.jpg",
"size656x437": "https://media.IMG_0003_656x437.jpg"
}
}
]}
I declared the above JSON as variable data and then used following code.
var items = JSON.parse(data);
return {
url: items.images;
}
But it returned all the urls, captions and resized image urls. I know another method is to use items.images[0].url. But, sometimes there are lots of image urls and its not feasible to add codes from 0 to n numbers. I thought about using for loop, but, I dont know how.
You can make a map and return urls only.
const items = JSON.parse(data);
const urls = items.images.map(item => item.url);

How parse JSON data in Codeigniter 3

In my project, using Codeigniter 3, I have a controller that get a JSON object in response from an API call, I'm trying to parse the JSON object but I'm not able to figure out to the access to each data.
The JSON object is the following:
{
"ok":true,
"result":
{
"message_id":9,
"sender_chat":
{
"id":-11234567899,
"title":"Sandbox",
"username":"JustDummyUsername",
"type":"channel"
},
"chat":
{
"id":-11234156789,
"title":"Sandbox",
"username":"JustDummyUsername",
"type":"channel"},
"date":1629538111,
"text":"Lorem ipsum dummy text",
"entities":
[
{
"offset":0,
"length":26,
"type":"bold"
},
{
"offset":112,
"length":22,
"type":"text_link",
"url":"xxxxxxxxxx"
}
]
}
}
}
I try, for example, the following code to get the message_id value
$json = $this->get_json_data();
$message_id = $json->result->message_id;
or
$json = $this->get_json_data();
$message_id = $json[0]->result->message_id;
but in both cases I get an error
Trying to get property 'result' of non-object
What's wrong?
Thanks a lot for any feedback.
EDIT: Silly me...I forgot json_decode().
Fixed
Fixed. Silly me. I forgot to json_decore the returned data

Graph API: JSON batching to upload item to OneDrive is failing

I am trying to upload a text file to OneDrive using Graph APIs and I also want to update it in the same request using JSON batch.
My JSON request body is below:
{
"requests":[
{
"id":"1",
"method":"PUT",
"url":"/drives/b!ddubdQaackeT9nu3x4onivgPxHH2-
gpFsk_mo9hryZabqK7w279YSpMqiNodZDaa/items/01BTTSDZ56Y2GOVW772
5BZO354PWSELRRZ:/abc2.txt:/content",
"headers":{
"Content-Type":"application/octet-stream",
"Content-Length":"21"
},
"body":{
"content":"Test content for body"
}
},
{
"id":"2",
"method":"PATCH",
"url":"/drives/b!ddubdQaackeT9nu3x4onivgPxHH2-
gpFsk_mo9hryZabqK7w279YSpMqiNodZDaa/items/01BTTSDZ56Y
2GOVW7725BZO354PWSELRRZ:/abc2.txt",
"headers":{
"Content-Type":"application/json; charset=utf-8"
},
"body":{
"fileSystemInfo":{
"lastModifiedDateTime":"2020-08-09T00:49:37.7758742+03:00"
}
},
"dependsOn":["1"]
}
]
}
When I send this request from my code, I always get a response "Invalid body for request id: 1. The body must be a valid base64 string or JSON.".
Postman refused to run the above request with the message "Method not allowed".
In the above example, I am uploading and updating text files but my code will have to handle all file types (e.g. images, videos, etc.)
Not sure if I am correctly specifying all the JSON fields. Unfortunately unable to find much info on this. Any help would be appreciated.

Best Schema for a Data List in JSON for RestFul API to use in Angular

I've been wondering for some days what kind of scheme would be more appropriate to use a data list in json in a web application.
I'm developing a REST Web Application, and im using Angular for front end, i should order, filter and print these data list also in xml ...
For you what scheme is better and why?
1) {
"datas": [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
]
}
2) {
"datas": [{
"data": { "first":"","second":""},
"data": { "first":"","second":""},
"data": { "first":"","second":""}
}]
}
3) [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
]
Thanks so much.
The first and third notations are quite similar because the third notation is included in your first.
So the question is "Should I return my datas as an array or should I return an object with a property that contain the array ?
It will depend on either you want to have more information alongside your datas or not.
For exemple, if your API might return an error, you will want to manage it from the front end.
In case of error, the JSON will looks like this :
{
"datas": null,
"error": "An error occured because of some reasons..."
}
At the opposite, if everything goes well and your API actually return the results, it will looks like this :
{
"datas": [
{ "first":"","second":""},
{ "first":"","second":""},
{ "first":"","second":""}
],
"error": null
}
Then your front end can use the error property to manage errors sent from the API.
var result = getDatas(); // Load datas from the API
if(result.error){
// Handle the error, display a message to the user, ...
} else {
doSomething(result.datas); // Use your datas
}
If you don't need to have extra properties like error then you can stick with the third schema.
The second notation is invalid. The datas array will contain only one object which will have one property named data. In this case data is a property that is defined multiple times so the object in the array will contain only the last occurence:
var result = {
"datas": [{
"data": { "first":"a","second":"b"},
"data": { "first":"c","second":"d"},
"data": { "first":"e","second":"f"}
}]
}
console.log("Content of result.datas[0].data : ")
console.log(result.datas[0].data)
Obviously the first option would be easy to use. Once you will access datas it'll give you an array. Any operation (filter, sort, print) on that array will be easy in comparison to anything else. Everywhere you just need to pass datas not datas.data.