MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / detectText

Function detectText

functions/ocr/app/index.js:55–90  ·  view source on GitHub ↗
(bucketName, filename)

Source from the content-addressed store, hash-verified

53 * @returns {Promise}
54 */
55const detectText = async (bucketName, filename) => {
56 console.log(`Looking for text in image ${filename}`);
57 const [textDetections] = await vision.textDetection(
58 `gs://${bucketName}/${filename}`
59 );
60 const [annotation] = textDetections.textAnnotations;
61 const text = annotation ? annotation.description.trim() : '';
62 console.log('Extracted text from image:', text);
63
64 let [translateDetection] = await translate.detect(text);
65 if (Array.isArray(translateDetection)) {
66 [translateDetection] = translateDetection;
67 }
68 console.log(
69 `Detected language "${translateDetection.language}" for ${filename}`
70 );
71
72 // Submit a message to the bus for each language we're going to translate to
73 const TO_LANGS = process.env.TO_LANG.split(',');
74 const topicName = process.env.TRANSLATE_TOPIC;
75
76 const tasks = TO_LANGS.map(lang => {
77 const messageData = {
78 text: text,
79 filename: filename,
80 lang: lang,
81 };
82
83 // Helper function that publishes translation result to a Pub/Sub topic
84 // For more information on publishing Pub/Sub messages, see this page:
85 // https://cloud.google.com/pubsub/docs/publisher
86 return publishResult(topicName, messageData);
87 });
88
89 return Promise.all(tasks);
90};
91// [END functions_ocr_detect]
92
93/**

Callers 1

index.jsFile · 0.70

Calls 1

publishResultFunction · 0.70

Tested by

no test coverage detected