generated from pot-app/pot-app-recognize-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
49 lines (43 loc) · 1.17 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-FileCopyrightText: 2024 [email protected]
//
// SPDX-License-Identifier: GPL-3.0-or-later
async function recognize(base64, lang, options) {
const { config, utils } = options;
const { tauriFetch: fetch } = utils;
const { api_key } = config;
if (!api_key) {
throw new Error("API key not found!");
}
const requestData = {
requests: [
{
image: { content: base64 },
features: { type: "TEXT_DETECTION" }
}
]
};
const res = await fetch(
`https://vision.googleapis.com/v1/images:annotate?key=${api_key}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: {
type: "Json",
payload: requestData
}
}
);
if (res.ok) {
const result = res.data;
const text = result?.responses?.[0]?.fullTextAnnotation?.text;
if (text) {
return text;
} else {
throw new Error("Response Parse Error");
}
} else {
throw new Error(`Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`);
}
}