{"id":841,"date":"2020-08-09T13:40:13","date_gmt":"2020-08-09T04:40:13","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=841"},"modified":"2020-10-30T23:56:12","modified_gmt":"2020-10-30T14:56:12","slug":"google-cloud-function-vision-api-ocr","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/09\/google-cloud-function-vision-api-ocr\/","title":{"rendered":"Google Cloud Functions \u306b \u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3066 Vision API \u3067 OCR"},"content":{"rendered":"<p><a href=\"https:\/\/www.amazon.co.jp\/Google-Cloud-Platform%E3%81%A7%E3%81%AF%E3%81%98%E3%82%81%E3%82%8B%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92%E3%81%A8%E6%B7%B1%E5%B1%A4%E5%AD%A6%E7%BF%92-%E5%90%89%E5%B7%9D%E9%9A%BC%E4%BA%BA-ebook\/dp\/B07F6YR7XT\/ref=as_li_ss_il?__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&amp;crid=SZSJ5OE8382E&amp;dchild=1&amp;keywords=google+cloud+platform&amp;qid=1596947898&amp;sprefix=google+cloud+p,aps,255&amp;sr=8-11&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=14811e37a960b3fb075eba43b66b4972&amp;language=ja_JP\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=B07F6YR7XT&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22&amp;language=ja_JP\" border=\"0\" \/><\/a><img loading=\"lazy\" decoding=\"async\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important;\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;language=ja_JP&amp;l=li2&amp;o=9&amp;a=B07F6YR7XT\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/><\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/02\/firebase-cli-google-cloud-functions\/\">Google Cloud Functions \u3078\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057Google Cloud Storage \u3078\u4fdd\u5b58\u3067\u304d\u305f<\/a>\u306e\u3067\u3001\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305f\u306e\u3067\u3001\u305d\u306e\u30d5\u30a1\u30a4\u30eb\u3092 Cloud Vision API\u306b\u304b\u3051\u3066OCR\u3092\u884c\u3046\u3002<\/p>\n<p><a href=\"https:\/\/firebase.google.com\/docs\/storage\/admin\/start?hl=ja\">Firebase Admin SDK<\/a> \u3092\u5229\u7528\u3057\u3066\u3044\u308b\u306e\u3067\u3001Google Cloud Platform \u5468\u308a\u306e\u8a8d\u8a3c\u304c\u96a0\u853d\u3055\u308c\u308b\u306e\u3067\u975e\u5e38\u306b\u4fbf\u5229\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u70b9\u306b\u306f\u307e\u3063\u305f\u304c\u3001\u306a\u3093\u3068\u304b\u590f\u5b63\u4f11\u66872\u65e5\u76ee\u306b\u758e\u901a\u3067\u304d\u305f\u3002<\/p>\n<ul>\n<li>Functions\u547c\u3073\u51fa\u3057URL\u3092\u9593\u9055\u3048\u3066\u3044\u308b\u3053\u3068\u306b\u6c17\u3065\u304b\u305a\u3001Vision API\u306f\u3001Firebase Admin SDK \u304c\u5229\u7528\u3067\u304d\u306a\u3044\u3068\u52d8\u9055\u3044\u3057\u7a7a\u56de\u308a\u3002\u7d50\u5c40\u5229\u7528\u3067\u304d\u305f\u3002<\/li>\n<li>TypeScript \u3068 import \u3092\u4f7f\u7528\u3057\u305f\u304b\u3063\u305f\u306e\u3060\u304c\u3001tsc \u306e\u30a8\u30e9\u30fc\u304c\u89e3\u6c7a\u3067\u304d\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u306f\u3001import \u3092\u4f7f\u7528\u3057\u3066\u3001\u578b\u5b9a\u7fa9\u306e\u6069\u6075\u3092\u3048\u3066\u3001\u30c7\u30d7\u30ed\u30a4\u6642\u306b\u306ftsc\u30a8\u30e9\u30fc\u3092\u56de\u907f\u3059\u308b\u305f\u3081\u306b\u3001require \u306b\u5207\u308a\u66ff\u3048\u308b\u306a\u3069\u3002<\/li>\n<\/ul>\n<h2>1.Functions \u5b9f\u88c5<\/h2>\n<h3>functions\/index.ts<\/h3>\n<pre class=\"prettyprint\">import * as functions from \u2018firebase-functions\u2019;\nimport * as admin from \u2018firebase-admin\u2019;\nadmin.initializeApp();\nimport * as path from \u2018path\u2019;\nimport * as os from \u2018os\u2019;\nimport * as fs from \u2018fs\u2019;\nimport * as Busboy from \u2018busboy\u2019;<\/pre>\n<p>\/\/ \u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6642\u306bTypeScript\u306e\u578b\u5b9a\u7fa9\u3092 import \u3057\u3066\u5229\u7528\u3057\u305f\u3044\u304c\u3001<br \/>\n\/\/\u3000\u30c7\u30d7\u30ed\u30a4\u3067\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308b\u306e\u3067\u3001require \u3092\u4f7f\u3046<br \/>\n\/\/import { v1p1beta1 as vision } from \u2018@google-cloud\/vision\u2019;<br \/>\nconst vision = require(\u201c@google-cloud\/vision\u201d).v1p1beta1;<\/p>\n<p>export const helloVisions = functions.https.onRequest((request, response) =&gt; {<br \/>\n if (request.method !== \u2018POST\u2019) {<br \/>\n response.status(405).end();<br \/>\n return;<br \/>\n }<br \/>\n const visionClient = new vision.ImageAnnotatorClient();<br \/>\n const busboy = new Busboy({headers: request.headers});<br \/>\n const tmpdir = os.tmpdir();<\/p>\n<pre><code>const uploads: {[key:string]:string} = {};\nconst fileWrites: Array&amp;lt;Promise&amp;lt;any&amp;gt;&amp;gt; = [];\n\nbusboy.on('file', (fieldname, file, filename) =&amp;gt; {\n    console.log(`Processed file ${filename}`);\n    const filepath = path.join(tmpdir, filename);\n\n    console.log(`Filepath ${filepath}`);\n    uploads[fieldname] = filepath;\n\n    const writeStream = fs.createWriteStream(filepath);\n    file.pipe(writeStream);\n\n    const promise = new Promise((resolve, reject) =&amp;gt; {\n        file.on('end', () =&amp;gt; {\n            writeStream.end();\n        });\n        writeStream.on('finish', resolve);\n        writeStream.on('error', reject);\n    });\n    fileWrites.push(promise);\n});\n\nbusboy.on('finish', async () =&amp;gt; {\n    await Promise.all(fileWrites);\n\n    for (const file in uploads) {\n        const filepath = uploads[file];\n        console.log(`Finish filename:${filepath}`);\n\n        const [textDetections] = await visionClient.textDetection(filepath);\n        const [annotation]:any = textDetections.textAnnotations;\n\n        const text = annotation ? annotation.description : '';\n        console.log(\"## OCR ## \" + text);\n        response.write(text);\n\n        try {\n            console.log(`UN LINK FILE : ${filepath}`);\n            fs.unlinkSync(filepath);\n        } catch(e) {\n        }\n    }\n    response.send();\n});\n\nbusboy.on('error', async () =&amp;gt; {\n    console.log(\"busboy on error\");\n});\nbusboy.end(request.rawBody);\n<\/code><\/pre>\n<p>});<\/p>\n<h2>2.\u547c\u3073\u51fa\u3057\u30da\u30fc\u30b8<\/h2>\n<pre class=\"prettyprint\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\u201cja\u201d&gt;\n&lt;head&gt;\n&lt;meta charset=\u201cuft\u20138\u201d&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;Cloud Vision&lt;\/h1&gt;\n &lt;form action=\u201chttps:\/\/{Firebase Cloud Functions \u306eURL}\/helloVisions\u201d method=\u201cPOST\u201d enctype=\u201cmultipart\/form-data\u201d&gt;\n &lt;p&gt;&lt;input type=\u201cfile\u201d name=\u201cupload_file1\u201d&gt;&lt;\/p&gt;\n &lt;p&gt;&lt;input type=\u201csubmit\u201d value=\u201cSubmit\u201d&gt;&lt;\/p&gt;\n &lt;\/form&gt;\n&lt;h1&gt;Log Console&lt;\/h1&gt;\n &lt;a href=\u201chttps:\/\/console.cloud.google.com\/logs\u201d target=\u201c_blnak\u201d&gt;GCP Log Console&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<h2>3.\u5b9f\u884c<\/h2>\n<p>OCR\u306b\u304b\u3051\u308b\u30b5\u30f3\u30d7\u30eb\u753b\u50cf<\/p>\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u9069\u5f53\u306b\u30ad\u30e3\u30d7\u30c1\u30e3\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/upload_sample_image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"upload_sample_image\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/upload_sample_image_thumb.png\" alt=\"upload_sample_image\" width=\"355\" height=\"218\" border=\"0\" \/><\/a><\/p>\n<p>\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u30da\u30fc\u30b8<\/p>\n<p>\u4e0a\u8a18\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u30da\u30fc\u30b8\u3092\u8d77\u52d5<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88-2020-08-09-12.43.02.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2020-08-09 12.43.02\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88-2020-08-09-12.43.02_thumb.png\" alt=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2020-08-09 12.43.02\" width=\"355\" height=\"364\" border=\"0\" \/><\/a><\/p>\n<h3>\u00a0<\/h3>\n<h3>\u4e0a\u8a18\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u306e\u3001Cloud Vision \u306b\u30b5\u30f3\u30d7\u30eb\u30d5\u30a1\u30a4\u30eb\u3092\u6307\u5b9a\u3057\u3001submit<\/h3>\n<p>\u7d50\u679c\u304c\u8fd4\u3063\u3066\u304d\u305f\uff01OK\uff01 \u306a\u304c\u304b\u3063\u305f\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88-2020-08-09-12.43.14.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2020-08-09 12.43.14\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88-2020-08-09-12.43.14_thumb.png\" alt=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2020-08-09 12.43.14\" width=\"449\" height=\"319\" border=\"0\" \/><\/a><\/p>\n<h3>GCP\u306e\u30ed\u30b0<\/h3>\n<p>\u30ed\u30b0\u306b\u3082\u3001\u60f3\u5b9a\u3057\u305f\u7d50\u679c\u304c\u306f\u304d\u3060\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/log.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"log\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/log_thumb.png\" alt=\"log\" width=\"449\" height=\"208\" border=\"0\" \/><\/a><\/p>\n<h2>4.tsc\u30a8\u30e9\u30fc\u5bfe\u5fdc<\/h2>\n<p>\u6700\u7d42\u7684\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u5bfe\u5fdc\u3092\u884c\u3046\u5fc5\u8981\u306f\u306a\u304b\u3063\u305f(\u5192\u982d\u306e\u8a18\u8ff0\u53c2\u7167)\u306e\u3060\u304c\u3001\u30e1\u30e2\u3002<\/p>\n<h3>(1) Cannot find name \u2018AsyncIterator\u2019<\/h3>\n<p><a href=\"https:\/\/github.com\/apollographql\/graphql-subscriptions\/issues\/83\">https:\/\/github.com\/apollographql\/graphql-subscriptions\/issues\/83<\/a><\/p>\n<p>tsconfig.json<\/p>\n<pre class=\"prettyprint\"> \u201ccompilerOptions\u201d: {\n \u201clib\u201d: [\n \u201cesnext.asynciterable\u201d\n ],<\/pre>\n<h3>(2) require(\u2026) is not a function<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/33007878\/nodejs-typeerror-require-is-not-a-function\">https:\/\/stackoverflow.com\/questions\/33007878\/nodejs-typeerror-require-is-not-a-function<\/a><\/p>\n<h2>5.\u53c2\u8003<\/h2>\n<ul>\n<li>Node.js<\/li>\n<ul>\n<li><a title=\"https:\/\/googleapis.dev\/nodejs\/vision\/latest\/\" href=\"https:\/\/googleapis.dev\/nodejs\/vision\/latest\/\">https:\/\/googleapis.dev\/nodejs\/vision\/latest\/<\/a><\/li>\n<\/ul>\n<li>Firebase admin SDK<\/li>\n<ul>\n<li><a title=\"https:\/\/firebase.google.com\/docs\/storage\/admin\/start?hl=ja\" href=\"https:\/\/firebase.google.com\/docs\/storage\/admin\/start?hl=ja\">https:\/\/firebase.google.com\/docs\/storage\/admin\/start?hl=ja<\/a><\/li>\n<\/ul>\n<li>Samples<\/li>\n<ul>\n<li><a title=\"https:\/\/stackoverflow.com\/questions\/46511848\/how-to-upload-image-file-via-http-google-cloud-function\" href=\"https:\/\/stackoverflow.com\/questions\/46511848\/how-to-upload-image-file-via-http-google-cloud-function\">https:\/\/stackoverflow.com\/questions\/46511848\/how-to-upload-image-file-via-http-google-cloud-function<\/a><\/li>\n<li><a title=\"https:\/\/github.com\/googleapis\/nodejs-vision\/blob\/master\/samples\/detect.v1p1beta1.js\" href=\"https:\/\/github.com\/googleapis\/nodejs-vision\/blob\/master\/samples\/detect.v1p1beta1.js\">https:\/\/github.com\/googleapis\/nodejs-vision\/blob\/master\/samples\/detect.v1p1beta1.js<\/a><\/li>\n<li><a title=\"https:\/\/cloud.google.com\/functions\/docs\/tutorials\/ocr?hl=ja\" href=\"https:\/\/cloud.google.com\/functions\/docs\/tutorials\/ocr?hl=ja\">https:\/\/cloud.google.com\/functions\/docs\/tutorials\/ocr?hl=ja<\/a><\/li>\n<\/ul>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Google Cloud Functions \u3078\u30d5\u30a1\u30a4\u30eb\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057Google Cloud Storage \u3078\u4fdd\u5b58\u3067\u304d\u305f\u306e\u3067\u3001\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u305f\u306e\u3067\u3001\u305d\u306e\u30d5\u30a1\u30a4\u30eb\u3092 Cloud Vision API\u306b\u304b\u3051\u3066OCR\u3092\u884c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":907,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[104,112,100,116],"tags":[],"class_list":["post-841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-firebase","category-google-cloud-functions","category-google-cloud-platform","category-google-cloud-vision"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=841"}],"version-history":[{"count":4,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/841\/revisions"}],"predecessor-version":[{"id":908,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/841\/revisions\/908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/907"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}