Skip to content

Commit dc8c849

Browse files
committed
1 parent 3b07dfb commit dc8c849

4 files changed

Lines changed: 121 additions & 26 deletions

File tree

functions/ocr/app/index.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,26 @@ function publishResult (topicName, data) {
4747
/**
4848
* Detects the text in an image using the Google Vision API.
4949
*
50-
* @param {object} file Cloud Storage File instance.
50+
* @param {string} bucketName Cloud Storage bucket name.
51+
* @param {string} filename Cloud Storage file name.
5152
* @returns {Promise}
5253
*/
53-
function detectText (file) {
54+
function detectText (bucketName, filename) {
5455
let text;
5556

56-
console.log(`Looking for text in image ${file.name}`);
57-
return vision.detectText(file)
58-
.then(([_text]) => {
59-
if (Array.isArray(_text)) {
60-
text = _text[0];
61-
} else {
62-
text = _text;
63-
}
57+
console.log(`Looking for text in image ${filename}`);
58+
return vision.textDetection({ source: { imageUri: `gs://${bucketName}/${filename}` } })
59+
.then(([detections]) => {
60+
const annotation = detections.textAnnotations[0];
61+
text = annotation ? annotation.description : '';
6462
console.log(`Extracted text from image (${text.length} chars)`);
6563
return translate.detect(text);
6664
})
6765
.then(([detection]) => {
6866
if (Array.isArray(detection)) {
6967
detection = detection[0];
7068
}
71-
console.log(`Detected language "${detection.language}" for ${file.name}`);
69+
console.log(`Detected language "${detection.language}" for ${filename}`);
7270

7371
// Submit a message to the bus for each language we're going to translate to
7472
const tasks = config.TO_LANG.map((lang) => {
@@ -78,7 +76,7 @@ function detectText (file) {
7876
}
7977
const messageData = {
8078
text: text,
81-
filename: file.name,
79+
filename: filename,
8280
lang: lang,
8381
from: detection.language
8482
};
@@ -128,9 +126,7 @@ exports.processImage = function processImage (event) {
128126
throw new Error('Filename not provided. Make sure you have a "name" property in your request');
129127
}
130128

131-
file = storage.bucket(file.bucket).file(file.name);
132-
133-
return detectText(file);
129+
return detectText(file.bucket, file.name);
134130
})
135131
.then(() => {
136132
console.log(`File ${file.name} processed.`);

functions/ocr/app/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
"safe-buffer": "5.1.1"
2525
},
2626
"devDependencies": {
27-
"@google-cloud/nodejs-repo-tools": "1.4.16",
28-
"ava": "0.21.0",
27+
"@google-cloud/nodejs-repo-tools": "1.4.17",
28+
"ava": "0.22.0",
2929
"proxyquire": "1.8.0",
30-
"sinon": "3.0.0"
30+
"sinon": "3.2.1"
3131
},
3232
"cloud-repo-tools": {
3333
"requiresKeyFile": true,

functions/ocr/app/test/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function getSample () {
5454
bucket: sinon.stub().returns(bucket)
5555
};
5656
const visionMock = {
57-
detectText: sinon.stub().returns(Promise.resolve([ text ]))
57+
textDetection: sinon.stub().returns(Promise.resolve([{ textAnnotations: [{ description: text }] }]))
5858
};
5959
const translateMock = {
6060
detect: sinon.stub().returns(Promise.resolve([{ language: `ja` }])),

functions/ocr/app/yarn.lock

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@
8181
string-format-obj "^1.1.0"
8282
through2 "^2.0.3"
8383

84-
"@google-cloud/nodejs-repo-tools@1.4.16":
85-
version "1.4.16"
86-
resolved "https://registry.yarnpkg.com/@google-cloud/nodejs-repo-tools/-/nodejs-repo-tools-1.4.16.tgz#a87b1f9db8426494ee7ea21a3a0cf172c66fe350"
84+
"@google-cloud/nodejs-repo-tools@1.4.17":
85+
version "1.4.17"
86+
resolved "https://registry.yarnpkg.com/@google-cloud/nodejs-repo-tools/-/nodejs-repo-tools-1.4.17.tgz#6458d12467cf93dc931d64640afabca1dfc19af2"
8787
dependencies:
8888
ava "0.21.0"
8989
colors "1.1.2"
@@ -92,7 +92,7 @@
9292
handlebars "4.0.10"
9393
lodash "4.17.4"
9494
proxyquire "1.8.0"
95-
sinon "3.0.0"
95+
sinon "3.2.0"
9696
string "3.3.3"
9797
supertest "3.0.0"
9898
yargs "8.0.2"
@@ -414,6 +414,91 @@ ava@0.21.0:
414414
unique-temp-dir "^1.0.0"
415415
update-notifier "^2.1.0"
416416

417+
ava@0.22.0:
418+
version "0.22.0"
419+
resolved "https://registry.yarnpkg.com/ava/-/ava-0.22.0.tgz#4c28a1fdef7e749ba0c8131ac18a7ca489eef049"
420+
dependencies:
421+
"@ava/babel-preset-stage-4" "^1.1.0"
422+
"@ava/babel-preset-transform-test-files" "^3.0.0"
423+
"@ava/write-file-atomic" "^2.2.0"
424+
"@concordance/react" "^1.0.0"
425+
ansi-escapes "^2.0.0"
426+
ansi-styles "^3.1.0"
427+
arr-flatten "^1.0.1"
428+
array-union "^1.0.1"
429+
array-uniq "^1.0.2"
430+
arrify "^1.0.0"
431+
auto-bind "^1.1.0"
432+
ava-init "^0.2.0"
433+
babel-core "^6.17.0"
434+
bluebird "^3.0.0"
435+
caching-transform "^1.0.0"
436+
chalk "^2.0.1"
437+
chokidar "^1.4.2"
438+
clean-stack "^1.1.1"
439+
clean-yaml-object "^0.1.0"
440+
cli-cursor "^2.1.0"
441+
cli-spinners "^1.0.0"
442+
cli-truncate "^1.0.0"
443+
co-with-promise "^4.6.0"
444+
code-excerpt "^2.1.0"
445+
common-path-prefix "^1.0.0"
446+
concordance "^3.0.0"
447+
convert-source-map "^1.2.0"
448+
core-assert "^0.2.0"
449+
currently-unhandled "^0.4.1"
450+
debug "^2.2.0"
451+
dot-prop "^4.1.0"
452+
empower-core "^0.6.1"
453+
equal-length "^1.0.0"
454+
figures "^2.0.0"
455+
find-cache-dir "^1.0.0"
456+
fn-name "^2.0.0"
457+
get-port "^3.0.0"
458+
globby "^6.0.0"
459+
has-flag "^2.0.0"
460+
hullabaloo-config-manager "^1.1.0"
461+
ignore-by-default "^1.0.0"
462+
import-local "^0.1.1"
463+
indent-string "^3.0.0"
464+
is-ci "^1.0.7"
465+
is-generator-fn "^1.0.0"
466+
is-obj "^1.0.0"
467+
is-observable "^0.2.0"
468+
is-promise "^2.1.0"
469+
js-yaml "^3.8.2"
470+
last-line-stream "^1.0.0"
471+
lodash.clonedeepwith "^4.5.0"
472+
lodash.debounce "^4.0.3"
473+
lodash.difference "^4.3.0"
474+
lodash.flatten "^4.2.0"
475+
loud-rejection "^1.2.0"
476+
make-dir "^1.0.0"
477+
matcher "^1.0.0"
478+
md5-hex "^2.0.0"
479+
meow "^3.7.0"
480+
ms "^2.0.0"
481+
multimatch "^2.1.0"
482+
observable-to-promise "^0.5.0"
483+
option-chain "^1.0.0"
484+
package-hash "^2.0.0"
485+
pkg-conf "^2.0.0"
486+
plur "^2.0.0"
487+
pretty-ms "^2.0.0"
488+
require-precompiled "^0.1.0"
489+
resolve-cwd "^2.0.0"
490+
safe-buffer "^5.1.1"
491+
slash "^1.0.0"
492+
source-map-support "^0.4.0"
493+
stack-utils "^1.0.0"
494+
strip-ansi "^4.0.0"
495+
strip-bom-buf "^1.0.0"
496+
supports-color "^4.0.0"
497+
time-require "^0.1.2"
498+
trim-off-newlines "^1.0.1"
499+
unique-temp-dir "^1.0.0"
500+
update-notifier "^2.1.0"
501+
417502
aws-sign2@~0.6.0:
418503
version "0.6.0"
419504
resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.6.0.tgz#14342dd38dbcc94d0e5b87d763cd63612c0e794f"
@@ -3038,9 +3123,23 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
30383123
version "3.0.2"
30393124
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"
30403125

3041-
sinon@3.0.0:
3042-
version "3.0.0"
3043-
resolved "https://registry.yarnpkg.com/sinon/-/sinon-3.0.0.tgz#f6919755c8c705e0b4ae977e8351bbcbaf6d91de"
3126+
sinon@3.2.0:
3127+
version "3.2.0"
3128+
resolved "https://registry.yarnpkg.com/sinon/-/sinon-3.2.0.tgz#8848a66ab6e8b80b5532e3824f59f83ea2628c77"
3129+
dependencies:
3130+
diff "^3.1.0"
3131+
formatio "1.2.0"
3132+
lolex "^2.1.2"
3133+
native-promise-only "^0.8.1"
3134+
nise "^1.0.1"
3135+
path-to-regexp "^1.7.0"
3136+
samsam "^1.1.3"
3137+
text-encoding "0.6.4"
3138+
type-detect "^4.0.0"
3139+
3140+
sinon@3.2.1:
3141+
version "3.2.1"
3142+
resolved "https://registry.yarnpkg.com/sinon/-/sinon-3.2.1.tgz#d8adabd900730fd497788a027049c64b08be91c2"
30443143
dependencies:
30453144
diff "^3.1.0"
30463145
formatio "1.2.0"

0 commit comments

Comments
 (0)