Download - RからGoogle Cloud Vision API を利用する
![Page 1: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/1.jpg)
からGoogle
CloudVisionAPIを使う
Tokyo.R #67
簑田 高志
![Page 2: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/2.jpg)
目次
1. 自己紹介
2. Google Cloud Vision APIとは
3. 使い始める前の準備
4. Rでの設定
5. 実際にうごかしてみる
6. まとめ
![Page 3: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/3.jpg)
• 名前 簑田 高志• Twitter aad34210• ブログ http://pracmper.blogspot.com/
• 仕事 ???E-commerceのビジネスアナリスネット広告のビジネスアナリスト
• 出版書籍 みんなのR(共同翻訳)データサイエンティストのための最新
知識と実践 Rではじめよう! [モダン]なデータ分析
自己紹介
![Page 4: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/4.jpg)
2. Google Cloud Vision APIとは
![Page 5: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/5.jpg)
こんなことありませんか?
• 写真に写っている場所がどこかを判定したい
答え:フランス モン・サン・ミッシェル 出展:http://farm2.static.flickr.com/1317/1314684944_29ac3169d2.jpg
![Page 6: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/6.jpg)
こんなことありませんか?
• 写真に写っている場所がどこかを判定したい
答え:アメリカ 自由の女神 出展: http://farm1.static.flickr.com/142/325436657_ef71ab8285.jpg
![Page 7: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/7.jpg)
こんなことありませんか?
• 写真に写っている場所がどこかを判定したい
答え:エジプト ギザの大ピラミッド 出展: http://www.wallpaperlink.com/bin/0707/03586.html
![Page 8: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/8.jpg)
こんなことありませんか?
• ロゴ抜き出したい…(なんのロゴか抜きたい)
マクドナルドのロゴ抜き出したい出展: https://tabelog.com/kanagawa/A1401/A140101/14029083/dtlphotolst/4/smp2/
![Page 9: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/9.jpg)
こんなことありませんか?
• ロゴ抜き出したい…(何社あるのこれ…)
出展: http://flovv.github.io/Brand-Logos/
![Page 10: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/10.jpg)
こんなことありませんか?
• 写っているテキストを抜き出したい
出展:https://store.shopping.yahoo.co.jp/top1-price/20160916-mens-socks4.html
![Page 11: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/11.jpg)
でも…大量の画像の”目視”だとツラミしかない
出展: http://flovv.github.io/Brand-Logos/
![Page 12: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/12.jpg)
コンピュータにやらせてみましょう!
出展:https://www.pakutaso.com/20130608171post-2947.html
![Page 13: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/13.jpg)
しかもRから!
![Page 14: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/14.jpg)
Google Cloud Visionですよ
出展: https://cloud.google.com/vision/?hl=ja
![Page 15: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/15.jpg)
Google Cloud Visionとは?
• Google Cloud Vision API は、使いやすい REST API にパワフルな機械学習モデルが組み込まれており、画像の内容を認識するアプリケーションの開発を可能にします。Google Cloud Vision API は、膨大な数のカテゴリ(「ヨット」や「ライオン」、「エッフェル塔」など)に各画像を素早く分類する機能や、画像内の個々の物体や人の顔を検出する機能、画像内に含まれているテキストを検出して読み取る機能を備えています。また、画像カタログのメタデータ作成、不適切なコンテンツの管理、画像の感情分析を通じた新しいマーケティング手法の導入が可能になります。リクエストでアップロードされた画像を分析することも、Google Cloud
Storage の画像ストレージに統合することもできます。
画像の中身を解析してくれるAPI
出展: https://cloud.google.com/vision/?hl=ja
(Google Cloud Vision APIより)
![Page 16: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/16.jpg)
どんなことができるか?
•ラベル検出•ロゴ検出•ランドマーク検出•文字認識(OCR)•顔検出•画像属性• Web検出
![Page 17: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/17.jpg)
ツラミに勝てる!
出展: https://www.pakutaso.com/20130113011post-2313.html
![Page 18: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/18.jpg)
3. 使い始める前の準備
![Page 19: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/19.jpg)
必要なもの
• Google Cloud Platform アカウント
• クレジットカード(支払いするため)
• Cloud Vision APIの認証情報
• R / RStudio
![Page 20: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/20.jpg)
Google Cloud Platform 認証設定
• Google Cloud Platform アカウントが利用できるようになったら、以下の画面が表示されます。
• 検索から「Cloud Vision API」を検索
※2018/01/20時点
![Page 21: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/21.jpg)
Google Cloud Platform 認証設定
• 「有効にする」ボタンをクリックする
※2018/01/20時点
![Page 22: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/22.jpg)
Google Cloud Platform 認証設定
• APIとサービスの画面が表示される• 再度バーの「認証情報」をクリック
※2018/01/20時点
![Page 23: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/23.jpg)
Google Cloud Platform 認証設定
• 認証情報を作成。• 今回はOAuthクライアントIDを作成
※2018/01/20時点
![Page 24: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/24.jpg)
Google Cloud Platform 認証設定
• 認証情報を作成終わったらJSONをダウンロードしておく
※2018/01/20時点
![Page 25: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/25.jpg)
4. Rでの設定
![Page 26: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/26.jpg)
Rのパッケージのインストール
• 必要なパッケージをインストールする• RoogleVision パッケージを使う
##packages
install.packages("tidyverse")
install.packages("leaflet")
install.packages("devtools")
install.packages("jsonlite")
devtools::install_github("flovv/RoogleVision")
source("http://bioconductor.org/biocLite.R")
biocLite("EBImage")
##library
library(tidyverse)
library(leaflet)
library(RoogleVision)
library(EBImage)
library(jsonlite)
![Page 27: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/27.jpg)
認証
• さきほどGoogle Cloud Platform側で設定した認証情報を使ってR側で認証• ブラウザが立ち上がりアカウントの紐付けを行う
creds = fromJSON(’hoge/foo/bar/~~.json‘) ## 認証JSONファイルのパス
options("googleAuthR.client_id" = creds$web$client_id)
options("googleAuthR.client_secret" = creds$web$client_secret)
options("googleAuthR.scopes.selected" =
c("https://www.googleapis.com/auth/cloud-platform"))
googleAuthR::gar_auth()
![Page 28: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/28.jpg)
5. 実際にうごかしてみる
![Page 29: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/29.jpg)
APIアクセス関数
• 一つの関数を使う。パラメータ内で複数機能の切り替え
getGoogleVisionResponse(imagePath,
feature = "LABEL_DETECTION",
numResults = 5)
※ feature =
(FACE_DETECTION(顔認識), LANDMARK_DETECTION(場所認識), LOGO_DETECTION(ロゴ認識), LABEL_DETECTION(ラベル認識), TEXT_DETECTION(テキスト))
![Page 30: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/30.jpg)
場所の特定
• 自由の女神 を特定してみる
出展: http://farm1.static.flickr.com/142/325436657_ef71ab8285.jpg
![Page 31: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/31.jpg)
場所の特定
• 自由の女神 を特定してみる
gcv_result <- getGoogleVisionResponse('statue_of_liberty.jpg', feature = 'LANDMARK_DETECTION')
head(gcv_result)
mid description score vertices
locations1 /m/072p8 Statue of Liberty 0.9103001 98, 252, 252, 98, 40, 40, 486, 486 40.68926, -74.04448
![Page 32: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/32.jpg)
場所の特定
• ピラミッドを特定 してみる
出展: http://www.wallpaperlink.com/bin/0707/03586.html
![Page 33: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/33.jpg)
場所の特定
• ピラミッドを特定してみる
gcv_result <- getGoogleVisionResponse(Pyramid.jpg', feature = 'LANDMARK_DETECTION')
head(gcv_result)
mid description score
vertices locations1 /m/036mk Great Pyramid of Giza 0.6733159 12, 947, 947, 12, 434, 434, 536, 536 29.97394, 31.13111
![Page 34: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/34.jpg)
場所の特定(マッピング)
• ピラミッドを地図上にマッピングしてみる。
latt <- gcv_result$locations[[1]][[1]][[1]]
lon <- gcv_result$locations[[1]][[1]][[2]]
m <- leaflet() %>% addProviderTiles(providers$CartoDB.Positron)
%>% setView(lng = lon, lat = latt, zoom = 5)
%>% addMarkers(lng = lon, lat = latt)
m
![Page 35: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/35.jpg)
場所の特定(マッピング)
• ピラミッドを地図上にマッピングしてみる。
latt <- gcv_result$locations[[1]][[1]][[1]]
lon <- gcv_result$locations[[1]][[1]][[2]]
m <- leaflet() %>% addProviderTiles(providers$CartoDB.Positron)
%>% setView(lng = lon, lat = latt, zoom = 5)
%>% addMarkers(lng = lon, lat = latt)
m
![Page 36: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/36.jpg)
ロゴの特定
• たくさんあるロゴを抽出
出展: http://flovv.github.io/Brand-Logos/
![Page 37: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/37.jpg)
ロゴの特定
gcv_result <- getGoogleVisionResponse(brandlogos.png', feature = LOGO_DETECTION ’ , numResults = 100)
head(gcv_result)
mid description score vertices1
/m/011spz0k The Guardian 0.5160552 213, 345, 345, 213, 173, 173, 443, 4432 /m/05b8qj Ray-Ban 0.4531192 213, 439, 439, 213, 92, 92, 255, 2553 /m/02q67s7 Colgate 0.4450404 478, 566, 566, 478, 13, 13, 44, 444 /m/03p39x1 Tesco Lotus 0.4292877 40, 140, 140, 40, 353, 353, 390, 3905 <NA> Heinz 0.4222758 693, 772, 772, 693, 16, 16, 42, 426 /g/1x5qtdq7 Cadbury Adams 0.4195638 290, 374, 374, 290, 296, 296, 320, 3207 /m/03lzc9 Special K 0.4150487 471, 568, 568, 471, 411, 411, 449, 4498 <NA> Walmart 0.4131395 372, 453, 453, 372, 353, 353, 384, 384
• たくさんのロゴは特定できないもよう…。(8個まで特定できた)• numResults で結果をコントロールできる
![Page 38: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/38.jpg)
ロゴの特定
• 特定のロゴを抽出
出展: https://tabelog.com/kanagawa/A1401/A140101/14029083/dtlphotolst/4/smp2/
![Page 39: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/39.jpg)
ロゴの特定
gcv_result <- getGoogleVisionResponse(macdonald_logo.jpeg', feature = LOGO_DETECTION ’)
head(gcv_result)
error1
No features detected!
• たくさんのロゴは特定できないもよう…。(8個まで特定できた)• numResults で結果をコントロールできる
残念!!!
![Page 40: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/40.jpg)
テキスト抜き出し
• 画像中のテキストを抜き出したい
出展:https://store.shopping.yahoo.co.jp/top1-price/20160916-mens-socks4.html
![Page 41: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/41.jpg)
テキスト抜き出し
• 画像中のテキストを抜き出したい
gcv_result <- getGoogleVisionResponse(’ socks_image_text.jpeg', feature = TEXT_DETECTION ’)
head(gcv_result)
![Page 42: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/42.jpg)
6. まとめ
![Page 43: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/43.jpg)
まとめ
1. Google Cloud Vision APIを使えば画像の「内容」を検出できる。
2. GCPの認証を取得
3. RoogleVision パッケージを使う
4. 場所の特定、ロゴの検出、テキスト検出ができることを確認できた。
5. 大量の画像もこのAPIを使えば、作業が楽になりそう。
![Page 44: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/44.jpg)
参考資料
• https://github.com/cloudyr/RoogleVision• https://www.r-bloggers.com/google-vision-api-in-r-rooglevision/• http://code.markedmondson.me/googleAuthR/articles/google-
authentication-types.html
![Page 45: RからGoogle Cloud Vision API を利用する](https://reader033.vdocuments.pub/reader033/viewer/2022052419/5aad4cd27f8b9a3a238b463f/html5/thumbnails/45.jpg)
ご静聴ありがとうございました