forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
255 lines (227 loc) · 7.98 KB
/
index.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Copyright 2016, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// [START functions_ocr_setup]
const config = require('./config.json');
// Get a reference to the Pub/Sub component
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
// Get a reference to the Cloud Storage component
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
// Get a reference to the Cloud Vision API component
const Vision = require('@google-cloud/vision');
const vision = new Vision.ImageAnnotatorClient();
// Get a reference to the Translate API component
const {Translate} = require('@google-cloud/translate');
const translate = new Translate();
const Buffer = require('safe-buffer').Buffer;
// [END functions_ocr_setup]
// [START functions_ocr_publish]
/**
* Publishes the result to the given pubsub topic and returns a Promise.
*
* @param {string} topicName Name of the topic on which to publish.
* @param {object} data The message data to publish.
*/
function publishResult(topicName, data) {
const dataBuffer = Buffer.from(JSON.stringify(data));
return pubsub
.topic(topicName)
.get({autoCreate: true})
.then(([topic]) => topic.publisher().publish(dataBuffer));
}
// [END functions_ocr_publish]
// [START functions_ocr_detect]
/**
* Detects the text in an image using the Google Vision API.
*
* @param {string} bucketName Cloud Storage bucket name.
* @param {string} filename Cloud Storage file name.
* @returns {Promise}
*/
function detectText(bucketName, filename) {
let text;
console.log(`Looking for text in image ${filename}`);
return vision
.textDetection(`gs://${bucketName}/${filename}`)
.then(([detections]) => {
const annotation = detections.textAnnotations[0];
text = annotation ? annotation.description : '';
console.log(`Extracted text from image (${text.length} chars)`);
return translate.detect(text);
})
.then(([detection]) => {
if (Array.isArray(detection)) {
detection = detection[0];
}
console.log(`Detected language "${detection.language}" for ${filename}`);
// Submit a message to the bus for each language we're going to translate to
const tasks = config.TO_LANG.map(lang => {
let topicName = config.TRANSLATE_TOPIC;
if (detection.language === lang) {
topicName = config.RESULT_TOPIC;
}
const messageData = {
text: text,
filename: filename,
lang: lang,
};
return publishResult(topicName, messageData);
});
return Promise.all(tasks);
});
}
// [END functions_ocr_detect]
// [START functions_ocr_rename]
/**
* Appends a .txt suffix to the image name.
*
* @param {string} filename Name of a file.
* @param {string} lang Language to append.
* @returns {string} The new filename.
*/
function renameImageForSave(filename, lang) {
return `${filename}_to_${lang}.txt`;
}
// [END functions_ocr_rename]
// [START functions_ocr_process]
/**
* This function is exported by index.js, and is executed when
* a file is uploaded to the Cloud Storage bucket you created
* for uploading images.
*
* @param {object} event.data (Node 6) A Google Cloud Storage File object.
* @param {object} event (Node 8+) A Google Cloud Storage File object.
*/
exports.processImage = event => {
let file = event.data || event;
return Promise.resolve()
.then(() => {
if (file.resourceState === 'not_exists') {
// This was a deletion event, we don't want to process this
return;
}
if (!file.bucket) {
throw new Error(
'Bucket not provided. Make sure you have a "bucket" property in your request'
);
}
if (!file.name) {
throw new Error(
'Filename not provided. Make sure you have a "name" property in your request'
);
}
return detectText(file.bucket, file.name);
})
.then(() => {
console.log(`File ${file.name} processed.`);
});
};
// [END functions_ocr_process]
// [START functions_ocr_translate]
/**
* This function is exported by index.js, and is executed when
* a message is published to the Cloud Pub/Sub topic specified
* by the TRANSLATE_TOPIC value in the config.json file. The
* function translates text using the Google Translate API.
*
* @param {object} event.data (Node 6) The Cloud Pub/Sub Message object.
* @param {object} event (Node 8+) The Cloud Pub/Sub Message object.
* @param {string} {messageObject}.data The "data" property of the Cloud Pub/Sub
* Message. This property will be a base64-encoded string that you must decode.
*/
exports.translateText = event => {
const pubsubData = event.data.data || event.data;
const jsonStr = Buffer.from(pubsubData, 'base64').toString();
const payload = JSON.parse(jsonStr);
return Promise.resolve()
.then(() => {
if (!payload.text) {
throw new Error(
'Text not provided. Make sure you have a "text" property in your request'
);
}
if (!payload.filename) {
throw new Error(
'Filename not provided. Make sure you have a "filename" property in your request'
);
}
if (!payload.lang) {
throw new Error(
'Language not provided. Make sure you have a "lang" property in your request'
);
}
console.log(`Translating text into ${payload.lang}`);
return translate.translate(payload.text, payload.lang);
})
.then(([translation]) => {
const messageData = {
text: translation,
filename: payload.filename,
lang: payload.lang,
};
return publishResult(config.RESULT_TOPIC, messageData);
})
.then(() => {
console.log(`Text translated to ${payload.lang}`);
});
};
// [END functions_ocr_translate]
// [START functions_ocr_save]
/**
* This function is exported by index.js, and is executed when
* a message is published to the Cloud Pub/Sub topic specified
* by the RESULT_TOPIC value in the config.json file. The
* function saves the data packet to a file in GCS.
*
* @param {object} event.data (Node 6) The Cloud Pub/Sub Message object.
* @param {object} event (Node 8+) The Cloud Pub/Sub Message object.
* @param {string} {messageObject}.data The "data" property of the Cloud Pub/Sub
* Message. This property will be a base64-encoded string that you must decode.
*/
exports.saveResult = event => {
const pubsubData = event.data.data || event.data;
const jsonStr = Buffer.from(pubsubData, 'base64').toString();
const payload = JSON.parse(jsonStr);
return Promise.resolve()
.then(() => {
if (!payload.text) {
throw new Error(
'Text not provided. Make sure you have a "text" property in your request'
);
}
if (!payload.filename) {
throw new Error(
'Filename not provided. Make sure you have a "filename" property in your request'
);
}
if (!payload.lang) {
throw new Error(
'Language not provided. Make sure you have a "lang" property in your request'
);
}
console.log(`Received request to save file ${payload.filename}`);
const bucketName = config.RESULT_BUCKET;
const filename = renameImageForSave(payload.filename, payload.lang);
const file = storage.bucket(bucketName).file(filename);
console.log(`Saving result to ${filename} in bucket ${bucketName}`);
return file.save(payload.text);
})
.then(() => {
console.log(`File saved.`);
});
};
// [END functions_ocr_save]