{"id":2616,"date":"2021-04-03T09:25:36","date_gmt":"2021-04-03T00:25:36","guid":{"rendered":"https:\/\/blue-bear.jp\/kb\/?p=2616"},"modified":"2021-04-03T09:33:03","modified_gmt":"2021-04-03T00:33:03","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%86%e3%82%ad%e3%82%b9%e3%83%88%e6%83%85%e5%a0%b1%e3%82%92%e6%8a%bd%e5%87%ba%e3%81%99%e3%82%8b","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%86%e3%82%ad%e3%82%b9%e3%83%88%e6%83%85%e5%a0%b1%e3%82%92%e6%8a%bd%e5%87%ba%e3%81%99%e3%82%8b\/","title":{"rendered":"[PHP]Google Cloud Vision API\u3092\u4f7f\u7528\u3057\u3066\u753b\u50cf\u306e\u30c6\u30ad\u30b9\u30c8\u60c5\u5831\u3092\u62bd\u51fa\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>Google Cloud Vision API\u306f\u753b\u50cf\u3092API\u306b\u51e6\u7406\u3055\u305b\u3001\u753b\u50cf\u306e\u4e2d\u306e\u30c6\u30ad\u30b9\u30c8\u3084\u753b\u50cf\u306b\u3042\u308b\u7269\u4f53\u306e\u7a2e\u985e\u306a\u3069\u3092JSON\u306a\u3069\u3067\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30b5\u30fc\u30d3\u30b9<\/p>\n<p>\u3053\u3053\u3067\u306fPHP\u3092\u4f7f\u7528\u3057\u3066Google Cloud Vision API\u3092\u4f7f\u7528\u3057\u3066\u753b\u50cf\u306e\u30c6\u30ad\u30b9\u30c8\u60c5\u5831\u3092\u62bd\u51fa\u3059\u308b\u65b9\u6cd5\u3092\u8a18\u8ff0\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<\/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><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<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<p>&nbsp;<\/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>\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\n$imagePath = \"\u753b\u50cf\u306e\u30d1\u30b9(URL\u3067\u3082\u53ef\u80fd)\"; \r\n$imageAnnotator = new ImageAnnotatorClient(); \r\n$image = file_get_contents($imagePath); \r\n$response = $imageAnnotator-&gt;textDetection($image); \r\n$texts = $response-&gt;getTextAnnotations(); \r\nforeach ($texts as $text) {\r\n unset($result);\r\n $result = $text-&gt;getDescription();\r\n \/\/\u6574\u5f62\u3001\u65e5\u672c\u8a9e\u3067\u306f\u3088\u304f\u300c\u301c\u300d\u304c\u534a\u89d2\u30c1\u30eb\u30c0\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\r\n $result = trim($result);\r\n $result = preg_replace(\"\/\\~\/\", \"\u30fc\", $result );\r\n $result = str_replace(array(\"\\r\\n\",\"\\r\",\"\\n\"), '', $result);\r\n $resultArr[] = $result;\r\n} \r\n\/\/\u8fd4\u308a\u5024\u304c\u7d50\u69cb\u30d0\u30e9\u30d0\u30e9\u306a\u306e\u3067\u6587\u5b57\u6570\u304c\u591a\u3044\u9806\u306b\u4e26\u3073\u66ff\u3048 \r\narray_multisort( array_map( \"strlen\", $resultArr ), SORT_DESC, $resultArr ) ; \r\n$imageAnnotator-&gt;close(); \r\n\/\/\u6700\u3082\u9577\u3044\u6587\u5b57\u5217\u3060\u3051\u3092\u53d6\u5f97 \r\necho $resultArr[0];<\/pre>\n<p>\uff11\u3064\u306e\u753b\u50cf\u3067\u3082\u8907\u6570\u306e\u8fd4\u308a\u5024\u304c\u5e30\u3063\u3066\u304f\u308b\u306e\u3067\u3001\u6700\u3082\u9577\u3044\u6587\u5b57\u3092\u9078\u629e\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002<\/p>\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>\u306a\u304b\u306a\u304b\u7d30\u304b\u304f\u62fe\u3063\u3066\u304f\u308c\u308b\u304c\u3001\u7e26\u8aad\u307f\u304c\u8aad\u3081\u306a\u304b\u3063\u305f\u308a\u3059\u308b<\/p>\n<p>\u65e5\u672c\u8a9e\u3088\u308a\u3082\u82f1\u8a9e\u306e\u8b58\u5b57\u7387\u306e\u65b9\u304c\u9ad8\u3044\u3002\u7d50\u69cb\u5c0f\u3055\u306a\u6587\u5b57\u3082\u8aad\u307f\u8fbc\u3093\u3067\u304f\u308c\u3066\u3044\u3066\u4ee5\u5916\u306a\u6587\u7ae0\u3092\u767a\u898b\u3057\u3066\u304f\u308c\u305f\u308a\u3082\u3059\u308b<\/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":[246],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/2616"}],"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=2616"}],"version-history":[{"count":3,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/2616\/revisions"}],"predecessor-version":[{"id":2621,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/posts\/2616\/revisions\/2621"}],"wp:attachment":[{"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/media?parent=2616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/categories?post=2616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue-bear.jp\/kb\/wp-json\/wp\/v2\/tags?post=2616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}