{"id":2619,"date":"2021-04-03T09:37:44","date_gmt":"2021-04-03T00:37:44","guid":{"rendered":"https:\/\/blue-bear.jp\/kb\/?p=2619"},"modified":"2021-04-03T09:37:44","modified_gmt":"2021-04-03T00:37:44","slug":"phpgoogle-cloud-vision-api%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%97%e3%81%a6%e7%94%bb%e5%83%8f%e3%81%ae%e3%83%a9%e3%83%99%e3%83%ab%e3%82%92%e5%8f%96%e5%be%97%e3%81%97%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%81%ab","status":"publish","type":"post","link":"https:\/\/blue-bear.jp\/kb\/phpgoogle-cloud-vision-api%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%97%e3%81%a6%e7%94%bb%e5%83%8f%e3%81%ae%e3%83%a9%e3%83%99%e3%83%ab%e3%82%92%e5%8f%96%e5%be%97%e3%81%97%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%81%ab\/","title":{"rendered":"[PHP]Google Cloud Vision API\u3092\u4f7f\u7528\u3057\u3066\u753b\u50cf\u306e\u30e9\u30d9\u30eb\u3092\u53d6\u5f97\u3057\u65e5\u672c\u8a9e\u306b\u7ffb\u8a33\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>Google Cloud Vision API\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u753b\u50cf\u304c\u3069\u306e\u3088\u3046\u306a\u753b\u50cf\u306a\u306e\u304b\u3068\u3044\u3046\u30e9\u30d9\u30eb\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u6b8b\u5ff5\u306a\u304c\u3089\u8fd4\u308a\u5024\u304c\u82f1\u8a9e\u306a\u306e\u3067Google Cloud Translate API\u3067\u65e5\u672c\u8a9e\u5316\u3059\u308b<\/p>\n<h2>Composer\u3067\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<pre>composer require google\/apiclient\r\ncomposer require google\/cloud-translate<\/pre>\n<p>\u3092\u5b9f\u884c\u3057\u3066\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b9f\u884c\u3057\u305f\u5834\u6240\u306b\/vendor\u3068\u3044\u3046\u30d5\u30a9\u30eb\u30c0\u304c\u3067\u304d\u308b\u306e\u3067\u3001\u3053\u308c\u3089\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5b9f\u884c\u3059\u308bPHP\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u8fd1\u304f\u306b\u914d\u7f6e\u3057\u305f\u307b\u3046\u304c\u3088\u3044\u3002<\/p>\n<p>\u30b3\u30de\u30f3\u30c9\u304c\u5909\u308f\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u306e\u3067\u3001\u516c\u5f0f\u3092\u53c2\u7167\u3059\u308b\u3053\u3068\u2193<\/p>\n<p>VISION API\u2193<\/p>\n<p><a href=\"https:\/\/cloud.google.com\/vision\/docs\/libraries#client-libraries-install-php\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/cloud.google.com\/vision\/docs\/libraries#client-libraries-install-php<\/a><\/p>\n<p>Translate API\u2193<\/p>\n<p><a href=\"https:\/\/cloud.google.com\/translate\/docs\/setup\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/cloud.google.com\/translate\/docs\/setup<\/a><\/p>\n<h2>Google Cloud Console\u3067\u306e\u4f5c\u696d<\/h2>\n<h3>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b<\/h3>\n<p><a href=\"https:\/\/console.cloud.google.com\/projectselector2\/home\/dashboard\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/console.cloud.google.com\/projectselector2\/home\/dashboard<\/a><\/p>\n<p>\u306b\u79fb\u52d5\u3057\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002\u8ab2\u91d1\u3082\u6709\u52b9\u306b\u3057\u3066\u304a\u304f\u3053\u3068<\/p>\n<h3>VISON API\u3092\u6709\u52b9\u5316\u3059\u308b<\/h3>\n<p><a href=\"https:\/\/console.cloud.google.com\/flows\/enableapi?apiid=vision.googleapis.com\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/console.cloud.google.com\/flows\/enableapi?apiid=vision.googleapis.com<\/a><\/p>\n<p>\u306b\u79fb\u52d5\u3057\u3001VISON API\u3092\u6709\u52b9\u5316\u3059\u308b<\/p>\n<h3>Translate API\u3092\u6709\u52b9\u5316\u3059\u308b<\/h3>\n<p><a href=\"https:\/\/console.cloud.google.com\/flows\/enableapi?apiid=translate.googleapis.com\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/console.cloud.google.com\/flows\/enableapi?apiid=translate.googleapis.com<\/a><\/p>\n<p>\u306b\u79fb\u52d5\u3057\u3001Translate API\u3092\u6709\u52b9\u5316\u3059\u308b<\/p>\n<h3>\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u30ad\u30fc\u3092\u751f\u6210\u30fb\u53d6\u5f97<\/h3>\n<p><a href=\"https:\/\/console.cloud.google.com\/apis\/credentials\/serviceaccountkey\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/console.cloud.google.com\/apis\/credentials\/serviceaccountkey<\/a><\/p>\n<p>\u3092\u958b\u304f\u3068\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u30ad\u30fc\u753b\u9762\u306b\u79fb\u52d5\u3059\u308b<\/p>\n<p>\uff0a\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306f\u4f5c\u6210\u6e08\u307f\u3068\u3057\u3066\u3044\u308b\u3002<\/p>\n<p id=\"zaitAIr\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2617 \" src=\"https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2021\/04\/img_6067b084975da.png\" alt=\"\" width=\"398\" height=\"290\" srcset=\"https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2021\/04\/img_6067b084975da.png 992w, https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2021\/04\/img_6067b084975da-300x218.png 300w, https:\/\/blue-bear.jp\/kb\/wp-content\/uploads\/2021\/04\/img_6067b084975da-768x559.png 768w\" sizes=\"(max-width: 398px) 100vw, 398px\" \/><\/p>\n<p>\u4e0a\u56f3\u306e\u3088\u3046\u306b\u5165\u529b<\/p>\n<ul>\n<li>\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\uff1d\u65b0\u3057\u3044\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8<\/li>\n<li>\u30a2\u30ab\u30a6\u30f3\u30c8\u540d\uff1d\u4efb\u610f<\/li>\n<li>\u30ed\u30fc\u30eb\uff1d\u4e0d\u8981<\/li>\n<li>\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8ID\uff1d\u81ea\u52d5\u3067\u4f5c\u6210\u3055\u308c\u308b<\/li>\n<li>\u30ad\u30fc\u30bf\u30a4\u30d7\uff1dJSON<\/li>\n<\/ul>\n<p>\u3067\u4f5c\u6210\u3092\u62bc\u4e0b\u3059\u308b<\/p>\n<p>\u30ed\u30fc\u30eb\u304c\u306a\u3044\u3068\u3044\u3046\u8b66\u544a\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u308b\u304c\u554f\u984c\u306a\u3057\u3002<\/p>\n<p>\u4f5c\u6210\u3059\u308b\u3068JSON\u30d5\u30a1\u30a4\u30eb\u304c\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u308c\u308b\u306e\u3067\u30b5\u30fc\u30d0\u4e0a\u306b\u914d\u7f6e\u3059\u308b\u3002PHP\u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u53c2\u7167\u3057\u3084\u3059\u3044\u3068\u3053\u308d\u304c\u3088\u3044\u3002<\/p>\n<h3>\u30b5\u30fc\u30d0\u4e0a\u3067\u306fGOOGLE_APPLICATION_CREDENTIALS\u5909\u6570\u306f\u8a2d\u5b9a\u3057\u306a\u304f\u3066\u3088\u3044<\/h3>\n<p>VISON API\u306e\u516c\u5f0f\u30da\u30fc\u30b8\u306b\u306fGOOGLE_APPLICATION_CREDENTIALS\u5909\u6570\u3092\u30b5\u30fc\u30d0\u4e0a\u306b\u8a2d\u5b9a\u3059\u308b\u3068\u3088\u3044\u3068\u66f8\u304b\u308c\u3066\u3044\u308b\u304c\u3001Google Cloud Vision API\u3067\u306f\u3046\u307e\u304f\u52d5\u4f5c\u305b\u305a<\/p>\n<pre>Fatal error: Uncaught DomainException: Could not load the default credentials.<\/pre>\n<p>\u3068\u3044\u3046\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4e2d\u3067GOOGLE_APPLICATION_CREDENTIALS\u5909\u6570\u3092\u8a2d\u5b9a\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b<\/p>\n<p>&nbsp;<\/p>\n<h2>PHP\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4f5c\u6210<\/h2>\n<pre>\/\/auth\u30a8\u30e9\u30fc\u56de\u907f\u306b\u5909\u6570\u3092\u66f8\u3044\u3066\u304a\u304f putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__ . '\/xxxxx.json'); require_once ( __DIR__ . '\/vendor\/autoload.php');\r\nuse Google\\Cloud\\Vision\\V1\\ImageAnnotatorClient;\r\nuse Google\\Cloud\\Translate\\TranslateClient;\r\n\r\n$imagePath = \"\u753b\u50cf\u306e\u30d1\u30b9(URL\u3067\u3082\u53ef)\";\r\n$translate = new TranslateClient();\r\n$targetLanguage = \"ja\"; \/\/\u65e5\u672c\u8a9e\u306b\u7ffb\u8a33\r\n\r\n$imageAnnotator = new ImageAnnotatorClient();\r\n\r\n$fileName = $imagePath;\r\n\r\n$image = file_get_contents($fileName);\r\n\r\n$response = $imageAnnotator-&gt;labelDetection($image);\r\n$labels = $response-&gt;getLabelAnnotations();\r\n\r\nif ($labels) {\r\n foreach ($labels as $label) {\r\n  \/\/\u30e9\u30d9\u30eb\u3092\u9010\u6b21\u7ffb\u8a33\r\n  $translated = $translate-&gt;translate($label-&gt;getDescription(), [\r\n'target' =&gt; $targetLanguage,\r\n  ]);\r\n  \/\/\u6587\u5b57\u5217\u304c\u307b\u3057\u304b\u3063\u305f\u306e\u3067\u300c\u3001\u300d\u3092\u3064\u3051\u3066\u8ffd\u52a0\u3057\u3066\u3044\u3063\u3066\u3044\u308b\r\n  $result .= $translated['text'].\"\u3001\";\r\n }\r\n} else {\r\n $result = \"\u30e9\u30d9\u30eb\u306a\u3057\";\r\n}\r\n\r\necho $result;\r\n<\/pre>\n<p>\u516c\u5f0f\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u3053\u3061\u3089<\/p>\n<p><a href=\"https:\/\/cloud.google.com\/vision\/docs\/ocr\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/cloud.google.com\/vision\/docs\/ocr<\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>\u611f\u60f3<\/h2>\n<p>\u30e9\u30d9\u30eb\u306f\u62fe\u3063\u3066\u304f\u308c\u308b\u306f\u62fe\u3063\u3066\u304f\u308c\u308b\u304c\u3001\u306a\u304b\u306a\u304b\u3053\u3061\u3089\u304c\u60f3\u5b9a\u3057\u3066\u3044\u308b\u7d30\u304b\u3044\u30e9\u30d9\u30ea\u30f3\u30b0\u3067\u306f\u306a\u3044\u306e\u3067\u3001\u4f7f\u7528\u3092\u65ad\u5ff5\u3057\u3066\u3044\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google Cloud Vision <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/2619"}],"collection":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/comments?post=2619"}],"version-history":[{"count":1,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/2619\/revisions"}],"predecessor-version":[{"id":2622,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/2619\/revisions\/2622"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=2619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=2619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=2619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}