{"id":859,"date":"2020-08-11T16:28:51","date_gmt":"2020-08-11T07:28:51","guid":{"rendered":"https:\/\/www.typea.info\/blog\/?p=859"},"modified":"2020-10-31T00:12:22","modified_gmt":"2020-10-30T15:12:22","slug":"angular-cloud-functions-ocr","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/11\/angular-cloud-functions-ocr\/","title":{"rendered":"Angular \u304b\u3089 Base64\u30a8\u30f3\u30b3\u30fc\u30c9\u3057\u305f\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092 Cloud Functions \u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3057\u3066 Cloud Vision API \u3067OCR"},"content":{"rendered":"<p><a href=\"https:\/\/www.amazon.co.jp\/Node-js%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3-%E7%AC%AC2%E7%89%88-Mario-Casciaro\/dp\/4873118735\/ref=as_li_ss_il?__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&amp;dchild=1&amp;keywords=Node&amp;qid=1597130822&amp;sr=8-5&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=83aa122080185c08b8088294312a0f33&amp;language=ja_JP\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4873118735&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=4873118735\" alt=\"\" width=\"1\" height=\"1\" border=\"0\"><\/p>\n<p><a href=\"https:\/\/cloud.google.com\/functions\/docs\/tutorials\/http?hl=ja\">\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb<\/a>\u306b\u305a\u3070\u308a\u300c<a href=\"https:\/\/cloud.google.com\/functions\/docs\/tutorials\/ocr?hl=ja\">\u5149\u5b66\u5f0f\u6587\u5b57\u8a8d\u8b58\uff08OCR\uff09\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb<\/a>\u300d\u304c\u3042\u308b\u3093\u3060\u3051\u3069\u3001\u4e0b\u56f3\u306e\u3088\u3046\u306b\u3001\u304b\u306a\u308a\u3044\u308d\u3044\u308d\u306a\u6a5f\u80fd\u3092\u7db2\u7f85\u7684\u306b\u7d39\u4ecb\u3057\u305f\u3044\u304c\u305f\u3081\u306a\u306e\u304b\u3001\u8907\u96d1\u3059\u304e\u308b\u3088\u3046\u306b\u898b\u3048\u308b(\u8a66\u3059\u306e\u304c\u9762\u5012)\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/vision_tutorial_concept.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"vision_tutorial_concept\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/vision_tutorial_concept_thumb.png\" alt=\"vision_tutorial_concept\" width=\"436\" height=\"267\" border=\"0\"><\/a><\/p>\n<p>\u3053\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u304c\u5143\u30cd\u30bf\u306e\u305f\u3081\u304b\u3001\u30b0\u30b0\u3063\u3066\u51fa\u3066\u304f\u308b OCR\u306e\u30b5\u30f3\u30d7\u30eb\u3082\u3044\u3063\u305f\u3093Cloud Storage\u306b\u4fdd\u5b58\u3057\u3066\u304b\u3089\u3001\u30c8\u30ea\u30ac\u30fc\u3067Functions\u3092\u52d5\u304b\u3057\u3066 Vision API\u3092\u547c\u3073\u51fa\u3059\u30b5\u30f3\u30d7\u30eb\u306f\u305d\u3053\u305d\u3053\u898b\u3064\u304b\u308b\u306e\u3060\u3051\u308c\u3069\u3001\u30b7\u30f3\u30d7\u30eb\u306b Cloud Functions \u3092\u5229\u7528\u3057\u3066\u3001OCR\u306e\u7d50\u679c\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306a\u306e\u306f\u898b\u3064\u304b\u3089\u306a\u3044(\u63a2\u3057\u65b9\u304c\u308f\u308b\u3044\uff1f)\u3002<\/p>\n<p>\u6599\u91d1(os.tmpdir\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u4e00\u6642\u7684\u306b\u66f8\u304d\u8fbc\u3080\u3088\u308aCloud Storage\u306e\u307b\u3046\u304c\u5b89\u3044\uff1f)\u3084\u3001\u6574\u5408\u6027\u7dad\u6301\u7684\u306a\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308b\u306e\u304b\u3057\u3089\uff1f<\/p>\n<p>\u3068\u601d\u3063\u305f\u3089\u3001<a href=\"https:\/\/cloud.google.com\/functions\/docs\/writing\/http?hl=ja\">\u7406\u7531\u66f8\u3044\u3066\u3042\u3063\u305f<\/a>\u3002<\/p>\n<p><a title=\"https:\/\/cloud.google.com\/functions\/docs\/writing\/http?hl=ja\" href=\"https:\/\/cloud.google.com\/functions\/docs\/writing\/http?hl=ja\">https:\/\/cloud.google.com\/functions\/docs\/writing\/http?hl=ja<\/a><\/p>\n<blockquote><p>\u6ce8: Cloud Functions \u3067\u306f HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u306e\u672c\u6587\u306e\u30b5\u30a4\u30ba\u304c 10 MB \u306b\u5236\u9650\u3055\u308c\u308b\u305f\u3081\u3001\u3053\u306e\u4e0a\u9650\u3092\u8d85\u3048\u308b\u30ea\u30af\u30a8\u30b9\u30c8\u306f\u3001\u95a2\u6570\u306e\u5b9f\u884c\u524d\u306b\u62d2\u5426\u3055\u308c\u307e\u3059\u30021 \u3064\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u30b5\u30a4\u30ba\u4e0a\u9650\u3092\u8d85\u3048\u3066\u6c38\u7d9a\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u306f\u3001Cloud Storage \u306b\u76f4\u63a5\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3059\u308b\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/ja.wikipedia.org\/wiki\/Base64\">Base64\u306b\u30a8\u30f3\u30b3\u30fc\u30c9\u3059\u308b\u3068\u3001\u30b5\u30a4\u30ba\u306f133%\u7a0b\u5ea6\u306b\u306a\u308b<\/a>\u306e\u3067\u3001\u4e0a\u9650\u304c10MB\u3060\u3068\u3001\u5b9f\u8cea\u306f7.5MB\u7a0b\u5ea6\u304b\u3001\u3001<\/p>\n<p>\u4eca\u56de\u306e\u8981\u4ef6\u306b\u306f\u30b5\u30a4\u30ba\u306f\u5341\u5206(\u304b\u3064\u6c38\u7d9a\u5316\u3055\u305b\u308b\u5fc5\u8981\u3082\u306a\u3044)\u306a\u306e\u3067\u3001\u4ee5\u4e0b\u691c\u8a3c\u3092\u7d4c\u3066\u305d\u308d\u3048\u305f\u7d20\u6750\u3067\u3001\u5358\u7d14\u306b\u30d5\u30a1\u30a4\u30eb\u3092Functions\u306b\u9001\u3063\u3066OCR\u304b\u3051\u3066\u7d50\u679c\u3092JSON\u3067\u8fd4\u3059\u5b9f\u88c5\u3092\u8a66\u3059\u3002<\/p>\n<ol>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/03\/17\/google-cloud-functinos\/\">Functions\u8a66\u3059<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/03\/22\/cloud_functions_for_firebase_dev_env\/\">Functions\u74b0\u5883\u69cb\u7bc9<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/03\/29\/cloud-functions-firestore-bind\/\">Firestore\u9023\u643a<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/02\/firebase-cli-google-cloud-functions\/\">\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/09\/google-cloud-function-vision-api-ocr\/\">Cloud Vision\u9023\u643a<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/11\/angular-cloud-functions\/\">Cloud Functions \u30a2\u30d7\u30ea\u547c\u3073\u51fa\u3057<\/a><\/li>\n<\/ol>\n<h2>1.Angular Page<\/h2>\n<p>\u30d5\u30a1\u30a4\u30eb\u9078\u629e\u306e\u30dc\u30bf\u30f3\u3068\u3001\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u30dc\u30bf\u30f3\u3001\u7d50\u679c\u3092\u8868\u793a\u3059\u308b\u30c6\u30ad\u30b9\u30c8\u30a8\u30ea\u30a2\u3092\u914d\u7f6e\u3002<\/p>\n<pre class=\"prettyprint\">&lt;textarea cols=\"60\" rows=\"5\" [(ngModel)]=\"ocrText\"&gt;&lt;\/textarea&gt;\n&lt;input type=\"file\" (change)=\"onFileChanged($event)\"&gt;\n&lt;button (click)=\"onUpload()\"&gt;Upload!&lt;\/button&gt;\n<\/pre>\n<h2>2.\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8<\/h2>\n<ul>\n<li>FileReader#readAsDataURL() \u3067\u3001\u9078\u629e\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u3001Base64\u306b\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3059\u308b\u3002\n<ul>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/02\/firebase-cli-google-cloud-functions\/\">\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<\/a> \u3067\u306f\u3001\u30de\u30eb\u30c1\u30d1\u30fc\u30c8\u3067\u9001\u4fe1\u3057\u3066\u3044\u305f\u304c\u3001Functions \u30a2\u30d7\u30ea\u547c\u3073\u51fa\u3057\u3067\u306f\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u3092JSON\u3067\u6e21\u3059\u5fc5\u8981\u304c\u3042\u308a\u305d\u3046\u306a\u306e\u3067\u3001Base64\u30a8\u30f3\u30b3\u30fc\u30c9\u3057\u305f\u6587\u5b57\u5217\u3068\u3057\u3066\u5f15\u304d\u6e21\u3059\u3002<\/li>\n<li><\/li>\n<\/ul>\n<\/li>\n<li>AngularFireFunctions#httpsCallable() \u3067\u3001\u547c\u3073\u51fa\u3057\u53ef\u80fdFunctions\u3092\u751f\u6210\u3002<\/li>\n<\/ul>\n<pre class=\"prettyprint\">import { Component, OnInit } from '@angular\/core';\nimport { AngularFireFunctions } from '@angular\/fire\/functions';\nimport { Observable, from } from 'rxjs';\n\n@Component({\n  selector: 'app-management',\n  templateUrl: '.\/management.component.html',\n  styleUrls: ['.\/management.component.scss']\n})\nexport class ManagementComponent implements OnInit {\n  visionsCallable: any;\n  selectedFile: File;\n  ocrText: string = '';\n\n  constructor(private fns: AngularFireFunctions) { \n    this.visionsCallable = fns.httpsCallable('sampleOnCallVisions');\n  }\n\n  ngOnInit(): void {\n  }\n\n  onFileChanged(event) {\n    this.selectedFile = event.target.files[0];\n  }\n\n  async onUpload() {\n    const reader = new FileReader();\n    const promise = new Promise(function(resolve, reject){\n      reader.onload = (function(){\n          return function(e){\n            \/\/ data:text\/plain;base64,xxxxx\n            var fileBase64 = e.target.result.split(',')[1];\n            resolve(fileBase64);\n          };\n      })();\n    });\n    \n    reader.readAsDataURL(this.selectedFile);\n    const fileBase64 = await promise.then();\n    console.log(fileBase64);\n\n    const observer = this.visionsCallable(\n      {\n        filename: this.selectedFile.name,\n        base64encodedFile: fileBase64\n      }) as Observable&lt;any&gt;;\n\n    try {\n      const res = await observer.toPromise();\n      this.ocrText = res.result;\n    } catch(e) {\n      console.log(e);\n    }\n  }\n}\n<\/pre>\n<h2>3.Functions<\/h2>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>\u3053\u306e\u8fba\u3067\u8a66\u3057\u305f\u3053\u3068\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u5b9f\u88c5\n<ol><!--StartFragment--><\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ol>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/03\/29\/cloud-functions-firestore-bind\/\">Firestore\u9023\u643a<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/02\/firebase-cli-google-cloud-functions\/\">\u30d5\u30a1\u30a4\u30eb\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<\/a><\/li>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2020\/08\/09\/google-cloud-function-vision-api-ocr\/\">Cloud<br \/>\nVision\u9023\u643a<\/a><\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><!--EndFragment--><\/p>\n<h3>index.ts<\/h3>\n<pre class=\"prettyprint\">export const sampleOnCallVisions = functions.https.onCall( async (data, context) =&gt; {\n    const filename: string = data?.filename as string;\n    const base64encodedFile: string = data?.base64encodedFile as string;\n\n    if (filename == null || typeof filename !== 'string' ||\n        base64encodedFile == null || typeof base64encodedFile !== 'string') {\n        throw new functions.https.HttpsError(\n            'invalid-argument','filename and  base64encodedFile are required.');\n    }\n    if (!context.auth) {\n        throw new functions.https.HttpsError(\n            'failed-precondition', 'not authenticated.');\n    }\n\n    const tmpdir = os.tmpdir();\n    const filepath = path.join(tmpdir, filename);\n\n    console.log(`Filepath ${filepath}`);\n\n    const visionClient = new vision.ImageAnnotatorClient();\n    fs.writeFileSync(filepath, base64encodedFile, {encoding: 'base64'});\n    \n    const [textDetections] = await visionClient.textDetection(filepath);\n    console.log(textDetections);\n    const [annotation]:any = textDetections.textAnnotations;\n    console.log(annotation);\n\n    const text = annotation ? annotation.description : '';\n    console.log(\"## OCR ## \" + text);\n\n    return {result: text};\n});<\/pre>\n<h2>4.\u5b9f\u884c<\/h2>\n<h3>4.1 \u30b5\u30f3\u30d7\u30eb\u30a4\u30e1\u30fc\u30b8<\/h3>\n<p>\u3044\u3056\u5b9f\u884c\u3002\u4ee5\u4e0b\u306e\u30b5\u30f3\u30d7\u30eb\u30a4\u30e1\u30fc\u30b8\u3092OCR\u306b\u304b\u3051\u308b\u3002<\/p>\n<h3><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/sample_image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"sample_image\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/sample_image_thumb.png\" alt=\"sample_image\" width=\"466\" height=\"146\" border=\"0\"><\/a><\/h3>\n<h3>4.2 \u5b9f\u884c\u7d50\u679c<\/h3>\n<p class=\"prettyprint\">\u9078\u629e\u3057\u305f\u30d5\u30a1\u30a4\u30eb\u304c\u3001OCR\u304b\u304b\u3063\u3066\u3001\u7d50\u679c\u304c\u8868\u793a\u3055\u308c\u305fOKOK!!<\/p>\n<pre class=\"prettyprint\"><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/ocr_client.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; background-image: none;\" title=\"ocr_client\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/2020\/08\/ocr_client_thumb.png\" alt=\"ocr_client\" width=\"472\" height=\"452\" border=\"0\"><\/a><\/pre>\n<h3 class=\"prettyprint\">\u53c2\u8003<\/h3>\n<ul>\n<li>\n<p class=\"prettyprint\"><a title=\"http:\/\/tomotaka.hatenablog.com\/entry\/20151108\/1446973166\" href=\"http:\/\/tomotaka.hatenablog.com\/entry\/20151108\/1446973166\">http:\/\/tomotaka.hatenablog.com\/entry\/20151108\/1446973166<\/a><\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306b\u305a\u3070\u308a\u300c\u5149\u5b66\u5f0f\u6587\u5b57\u8a8d\u8b58\uff08OCR\uff09\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u300d\u304c\u3042\u308b\u3093\u3060\u3051\u3069\u3001\u4e0b\u56f3\u306e\u3088\u3046\u306b\u3001\u304b\u306a\u308a\u3044\u308d\u3044\u308d\u306a\u6a5f\u80fd\u3092\u7db2\u7f85\u7684\u306b\u7d39\u4ecb\u3057\u305f\u3044\u304c\u305f\u3081\u306a\u306e\u304b\u3001\u8907\u96d1\u3059\u304e\u308b\u3088\u3046\u306b\u898b\u3048\u308b(\u8a66\u3059\u306e\u304c\u9762\u5012)\u3002 \u3053\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u304c\u5143\u30cd\u30bf\u306e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":903,"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":[102,104,112,116],"tags":[],"class_list":["post-859","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular","category-firebase","category-google-cloud-functions","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\/859","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=859"}],"version-history":[{"count":6,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/859\/revisions"}],"predecessor-version":[{"id":934,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/859\/revisions\/934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media\/903"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}