This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlabels-handwriting.yaml
100 lines (94 loc) · 3.11 KB
/
labels-handwriting.yaml
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
apiVersion: serverless.kyma-project.io/v1alpha1
kind: Function
metadata:
name: labels-handwriting
spec:
deps: |-
{
"name": "labels-handwriting",
"version": "1.0.0",
"dependencies": {
"node-fetch": "2.6.1",
"@google-cloud/vision":"2.3.8",
"nanoid": "3.1.25"
}
}
env:
- name: API_CONFIG
valueFrom:
configMapKeyRef:
key: config.json
name: showcase-functions-config
- name: GCP_EMAIL
valueFrom:
secretKeyRef:
key: GCP_EMAIL
name: kyma-showcase-secret
- name: GCP_API_KEY
valueFrom:
secretKeyRef:
key: GCP_API_KEY
name: kyma-showcase-secret
runtime: nodejs14
source: |-
module.exports = {
main: async function (event, context) {
async function getBase64(id) {
console.log("Getting base64 from DB");
const fetch = require('node-fetch');
const URL = JSON.parse(process.env.API_CONFIG).API_URL + '/' + id;
const response = await fetch(URL);
const data = await response.json();
const content = data.content;
const base64 = content.replace(/data:.*?base64,/, '');
console.log("Base64 loaded");
return base64;
}
async function getHandDetails(base64){
console.log("Getting handwriting details from GCP");
const vision = require('@google-cloud/vision');
const options = {
credentials: {
client_email: process.env.GCP_EMAIL,
private_key: process.env.GCP_API_KEY.replace(/\\n/gm, '\n'),
},
};
const client = new vision.ImageAnnotatorClient(options);
const request = {
image: {
content: Buffer.from(base64, 'base64'),
},
};
const [result] = await client.documentTextDetection(request);
const fullTextAnnotation = result.fullTextAnnotation;
const handJson = JSON.stringify({handwriting:fullTextAnnotation.text});
console.log("Handwriting details loaded");
return handJson;
}
async function putDetailsToDB(id, data){
console.log("Putting handwriting to DB");
const fetch = require('node-fetch');
const URL = JSON.parse(process.env.API_CONFIG).API_URL + '/' + id;
const response = await fetch(URL,{
method:'PUT',
body: data,
headers: {
'Content-type': 'application/json',
},
});
await console.log("Handwriting details put to DB with status: " + response.status);
return response.json();
}
try{
// Needed to remove unwanted apostrophes from our ID
const imgID = event.data.slice(1, -1);
console.log("Processing image: " + imgID);
const base64 = await getBase64(imgID);
const handDetails = await getHandDetails(base64);
await putDetailsToDB(imgID, handDetails);
}catch(err){
console.error(err);
return null;
}
}
}