Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
[JAWS-SG#02] 個人開発やハッカソンで役立つ、便利APIのつくりかた
Search
Keisuke Mori
May 16, 2020
Programming
0
150
[JAWS-SG#02] 個人開発やハッカソンで役立つ、便利APIのつくりかた
Keisuke Mori
May 16, 2020
Tweet
Share
More Decks by Keisuke Mori
See All by Keisuke Mori
AWSを使う上で意識しておきたい、クラウドセキュリティ超入門(駆け足版)
kkmory
1
410
rubyonjets_with_aws_for_research
kkmory
0
580
AWSで動画変換&配信基盤を作った話
kkmory
0
250
Slackであそぼ!!
kkmory
0
130
Git-study-Hackz-treasure
kkmory
1
270
飯塚ブロックチェーン勉強会 #1
kkmory
0
140
Swarm: The distributed storage platform
kkmory
1
800
Kindai_Blockchain_study01
kkmory
0
260
Other Decks in Programming
See All in Programming
Reduxモダナイズ 〜コードのモダン化を通して、将来のライブラリ移行に備える〜
pvcresin
2
680
階層構造を表現するデータ構造とリファクタリング 〜1年で10倍成長したプロダクトの変化と課題〜
yuhisatoxxx
3
910
Let's Write a Train Tracking Algorithm
twocentstudios
0
220
そのpreloadは必要?見過ごされたpreloadが技術的負債として爆発した日
mugitti9
2
3k
止められない医療アプリ、そっと Swift 6 へ
medley
1
120
Model Pollution
hschwentner
1
180
XP, Testing and ninja testing ZOZ5
m_seki
2
290
Advance Your Career with Open Source
ivargrimstad
0
330
LLMとPlaywright/reg-suitを活用した jQueryリファクタリングの実際
kinocoboy2
4
670
メモリ不足との戦い〜大量データを扱うアプリでの実践例〜
kwzr
1
860
overlayPreferenceValue で実現する ピュア SwiftUI な AdMob ネイティブ広告
uhucream
0
110
CSC305 Lecture 03
javiergs
PRO
0
230
Featured
See All Featured
Six Lessons from altMBA
skipperchong
28
4k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Documentation Writing (for coders)
carmenintech
75
5k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
The Cult of Friendly URLs
andyhume
79
6.6k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
How GitHub (no longer) Works
holman
315
140k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
Optimising Largest Contentful Paint
csswizardry
37
3.4k
Designing for Performance
lara
610
69k
Typedesign – Prime Four
hannesfritz
42
2.8k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
32
2.2k
Transcript
個人開発やハッカソンで役立つ、 便利 API のつくりかた Kindai University / Hack’z Inc. Keisuke
Mori
Keisuke Mori 近畿大学産業理工学部(福岡キャンパス) - 情報学科 4回生 - 暗号技術, ブロックチェーン, プライバシー技術
- ソフトウェア開発の演習では Cloud9 使ってました @moririn772 サーバーサイド/インフラ系 - Go, Ruby, AWS がお気に入り - JAWS-SG 運営メンバー(今日は配信担当)
ݸਓ։ൃϋοΧιϯͰཱͭɺศར API ͷͭ͘Γ͔ͨ Today’s TALK
ΰʔϧσϯΟʔΫɺԿͯ͠·͔ͨ͠ʁ Q.
A. ΦϯϥΠϯϋοΧιϯʹग़ͯ·ͨ͠ https://connpass.com/event/174573/ɹ
ͳʹ࡞ͬͨΜͰ͔͢ ☺☺ Q.
A. ͜ΜͳͷΛ࡞Γ·ͨ͠
作った背景テンプレートをタグ付けしてみんなに共有 (タグを付けて検索しやすく) A. ࡞ͬͨςϯϓϨʔτڞ༗Ͱ͖·͢
Ͳ͏ͬͯ࡞ͬͨΜͰ͔͢ Q.
A. ͜Μͳײ͡Ͱ࡞Γ·ͨ͠ Application Server Image Hosting Service Client Tag Suggestion
Service API Gateway Lambda Translate Recognition Cloud SQL ( MySQL ) Cloudfront S3 React (Next) CI/CD etc… GAE ( Go ) Ұ෦ϋοΧιϯظؒʹऴΘΓ·ͤΜͰͨ͠…
ࠓ͜͜ͷΛ͠·͢ʂ Application Server Image Hosting Service Client Tag Suggestion Service
API Gateway Lambda Translate Recognition Cloud SQL ( MySQL ) Cloudfront S3 React (Next) CI/CD etc… GAE ( Go )
ͬͨ AWS ͷαʔϏε Amazon API Gateway Amazon Rekognition AWS Lambda
Amazon Translate ؆୯ʹ API ͷ࡞, ެ։, อक, ϞχλϦϯά, อޢΛߦ͏͜ͱ͕ Ͱ͖Δ αʔόʔϨε࣮ߦڥ ͍ΖΜͳݴޠͰॻ͚Δ Ruby, Go, Python, Java … ػցֶशͷઐࣝΛඞཁͱͤͣ ը૾ಈըͷମ, إ, γʔϯ Λ ಛఆͰ͖ΔɻΧελϜϥϕϧΛɹ ͏ͱɺ෦ͷྨͳͲՄೳ χϡʔϥϧػց༁αʔϏε 54 ݴޠʹରԠ
Tag Suggestion Service … 作った背景テンプレートをタグ付けして共有 → つけられそうなタグを背景画像から推測して提示する機能
Tag Suggestion Service … Tag Suggestion Service API Gateway Lambda
Translate Recognition Client
Tag Suggestion Service … Tag Suggestion Service API Gateway Lambda
Translate Recognition Client 背景に使われている素材(既にS3にPUT済)のURLを、 エンドポイントに投げる https://~~~/default/detect-image-label?image-uri=*************
Tag Suggestion Service … Tag Suggestion Service API Gateway Lambda
Translate Rekognition Client API Gateway から Lambda に処理を流す。 Lambda関数(handrer.request_handler)を発火させ、 AWS-SDK から Rekognition のAPI を叩く。
Tag Suggestion Service … Tag Suggestion Service API Gateway Lambda
Translate Rekognition Client Rekognition の “DetectLabels” オペレーションを使用。 画像をもとにラベルが検出され、結果が返る。
Tag Suggestion Service … Tag Suggestion Service API Gateway Lambda
Translate Rekognition Client タグが英語で返ってきてしまい、UX的によくないので Amazon Translate の "Translate Text" オペレーションを 使用し、タグを 英語→日本語 に翻訳する。
Tag Suggestion Service … Tag Suggestion Service API Gateway Lambda
Translate Rekognition Client これまでが処理の全貌 あとはクライアント側にレスポンスを返すだけ。 “body” : [ “飛行機”, “輸送”, “航空機”, “旅客機”, “車両” ]
Tag Suggestion Service … Tag Suggestion Service API Gateway Lambda
Translate Rekognition Client 推測されたタグが表示される。 ユーザーはタグを選択し、テンプレートを公開する。
ͲΕ͘Β͍Ͱ࡞ͬͨΜͰ͔͢ ⏰ ⏰ Q.
A. 1࣌ؒ͘Β͍Ͱͭ͘Ε·ͨ͠ * ˎ ಈ࡞༏ઌͰ࡞ͬͨͷͰςετॻ͍ͯͳ͍͠ɺσϓϩΠZIPͰݻΊΔΓํͩ͠ɺ ɹࡶΜͬͯποίϛ͕ೖΓͦ͏Ͱ͕͢ಈ͘ͷͰϤγʂʂ ɹ ϋοΧιϯݸਓ։ൃʮಈ͘͜ͱʯ͕େͳͷͰʂʂʢݴ͍༁ʣ ɹ ͋ͱͰSAMͰ࡞Γ͠·͠ΐ͏…
ݸਓ։ൃϋοΧιϯͰཱͭɺศར API ͷͭ͘Γ͔ͨ ʙ Amazon Rekognition Λ༻ͨ͠ը૾ͷλά͚APIฤ ʙ
ιʔείʔυ༧Ί༻ҙ͓͖ͯ͠·ͨ͠ $ git clone \ https://github.com/moririn772/tag-suggestion-api.git $ #ͱ͍͏ͷෆͳͷͰɺGitHubϦϙδτϦެ։ͯ͠·͢
Bundle install $ cd $ bundle install —path=vendor/bundle
Ͱ͖·ͨ͠ʂ $ ll > 5 13 10:47 Gemfile > 5
13 10:17 Gemfile.lock > 5 13 11:18 Readme.md > 5 13 10:49 handler.rb > 5 13 10:17 vendor
ZIPͰݻΊ·͢ $ zip -r handler.zip handler.rb vendor
ΆͪΆͪ…ʢLambdaʣ
ΆͪΆͪ…ʢAPI Gatewayʣ
ΆͪΆͪ…ʢIAMʣ
Ͱ͖…Δͷ͔…ʂʂ
None
؆୯ʹ͜ͷϨϕϧͷػೳ͕࣮Ͱ͖Δͷ͘͢͝Ͷʁ
εϐʔυ࣮
ɹϚωʔδυαʔϏεΛͲΜͲΜ͍ͬͯͬͯɺ ͍͍ײ͡ͷΞτϓοτΛ͍ͯ͜͠͏
खΛಈ͔ͨ͠ΒɺࠓͦΕΛ ొஃϒϩάͰΈΜͳʹγΣΞ͍ͯ͜͠͏
ݸਓ։ൃϋοΧιϯͰཱͭɺศར API ͷͭ͘Γ͔ͨ ʙ Amazon Rekognition Λ༻ͨ͠ը૾ͷλά͚APIฤ ʙ Thank you
!!
͓·͚ɿίʔυղઆ
Gemfile Λฤूͯ͠ɺbundle install ͠·͢ # frozen_string_literal: true source 'https://rubygems.org' git_source(:github)
{ |repo_name| "https://github.com/#{repo_name}" } gem 'aws-sdk-rekognition' gem 'aws-sdk-translate' Gemfile $ bundle install —path=vendor/bundle
ґଘ gem Λ require ͠·͢ # frozen_string_literal: true require 'net/http'
require 'json' require 'aws-sdk-rekognition' require 'aws-sdk-translate' require 'base64' require 'open-uri' def request_handler(event:, context:) image_uri = event['queryStringParameters']['image-uri'] resp = detect_label(URI.open(image_uri).read) { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: JSON.generate(resp) } end handler.rb
ґଘ gem Λ require ͠·͢ # frozen_string_literal: true require 'net/http'
require 'json' require 'aws-sdk-rekognition' require 'aws-sdk-translate' require 'base64' require 'open-uri' handler.rb
Rekognition ʹϥϕϧΛ͚ͭͯΒ͏ؔ def detect_label(data) client = Aws::Rekognition::Client.new attrs = {
image: { bytes: data }, max_labels: 10 } response = client.detect_labels(attrs) # Filter labels = response.labels.select { |c| c.confidence >= 66 } # Translate tag, Generate response array labels.map { |l| translate(l[:name].downcase) } end handler.rb
ϥϕϧΛຊޠʹͯ͘͠ΕΔؔ def translate(target) client = Aws::Translate::Client.new resp = client.translate_text( text:
target, source_language_code: 'en', target_language_code: 'ja' ) resp.translated_text end handler.rb
ϦΫΤετͷϋϯυϥʔؔΛ࣮͠·͢ def request_handler(event:, context:) # ΫΤϦύϥϝʔλ͜͏ͬͯऔΔ image_uri = event['queryStringParameters']['image-uri'] resp
= detect_label(URI.open(image_uri).read) { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: JSON.generate(resp) } end handler.rb
DONE !!! def request_handler(event:, context:) # ΫΤϦύϥϝʔλ͜͏ͬͯऔΔ image_uri = event['queryStringParameters']['image-uri']
resp = detect_label(URI.open(image_uri).read) { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: JSON.generate(resp) } end handler.rb