Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8691

Vue.jsでつくったサイトにfirebaseでユーザ認証してこっそり人の顔年齢を試して遊ぶ

$
0
0

概要

Vue.jsで作ったサイトをFirebaseにユーザー認証をお願いして、ログインしないと入れないページにして、人の顔年齢(Computer Vision API)で遊んで、freenomとNetlifyで独自ドメインで公開する

できたもの

スクリーンショット 2020-05-28 1.01.04.png
写真を入れると年齢をかえしてくれます。

環境

macOS Catalina 
Visual Studio Code 1.45.1
Node.js: v13.11.0
npm:6.14.5
Vue:@vue/cli 4.3.1

大まかな流れ

Vue.jsとFirebaseでユーザー認証ありのプロジェクトをつくる
   ↓
Computer Vision APIをつかって顔認識のぺーじをつくる
   ↓
freenomでドメインを取る
   ↓
Netlifyに freenomドメインをあててDeploy

メインの構成はこんな感じです

App.vue
main.js
assets
components
 - Signup.vue
 - HelloWorld.vue
 - Signin.vue
router
 - index.js

Vue.jsとFirebaseでユーザー認証ありのプロジェクトをつくる

こちらがめちゃくちゃわかりやすかったです。
こちらを参考にVue.jsとFirebaseでユーザー認証ありのプロジェクトをつくる

Vue.js + Firebase を使って爆速でユーザ認証を実装する - Qiita

src/components/Signin.vue
<template><divclass="signin"><h2>Sign in</h2><b-containerclass="bv-example-row"><b-rowclass="justify-content-md-center"><b-forminline><b-inputid="inline-form-input-name"v-model="username"class="mb-2 mr-sm-2 mb-sm-0"placeholder="Username"></b-input><b-inputtype="password"id="text-password"v-model="password"aria-describedby="Password"placeholder="Password"></b-input><!-- <button@click="signIn">Signin</button> -->
    <b-buttonvariant="outline-primary"@click="signIn">Signin</b-button></b-form></b-row><b-row><b-col><p>You don't have an account?
      <router-linkto="/signup">create account now!!</router-link></p></b-col></b-row></b-container></div></template><script>importfirebasefrom'firebase'exportdefault{name:'Signin',data:function(){return{username:'',password:''}},methods:{signIn:function(){firebase.auth().signInWithEmailAndPassword(this.username,this.password).then(user=>{// alert('Success!')this.$router.push('/')},err=>{alert(err.message)})}}}</script><style>.signin{line-height:5;}.signinh2{margin:2remauto;}button.btn.btn-outline-primary{margin:01rem;}</style>
src/components/Signup.vue
<template><divclass="signup"><h2>Sign up</h2><inputtype="text"placeholder="Username"v-model="username"><inputtype="password"placeholder="Password"v-model="password"><button@click="signUp">Register</button><p>Do you have an account?
      <router-linkto="/signin">sign in now!!</router-link></p></div></template><script>importfirebasefrom'firebase'exportdefault{name:'Signup',data(){return{username:'',password:''}},methods:{signUp:function(){firebase.auth().createUserWithEmailAndPassword(this.username,this.password).then(user=>{alert('Create account: ',user.email)}).catch(error=>{alert(error.message)})}}}</script>

Computer Vision APIをつかって顔認識のぺーじをつくる

こちらを参考に顔認識のページをつくる
Azure画像認識系の機能をいくつか試してみた(Face API / Custom Vision API / Computer Vision API) - Qiita

src/components/HelloWorld.vue
<template><divclass="hello"><pclass="lh5">{{msg}}</p><divid="appFaceAPI-File"><divclass="row"><divclass="col"><h1>その写真の人が何歳に見えるかおしえてあげよう</h1></div></div><divclass="row"><divclass="col"><divclass="form-group"><!-- https://bootstrap-vue.org/docs/components/form-file --><div><!-- Styled --><b-form-filev-model="file":state="Boolean(file)"placeholder="Choose a file or drop it here..."drop-placeholder="Drop file here..."@change="handlerFileChange"></b-form-file><divclass="mt-3">Selected file: {{file?file.name:''}}</div></div></div></div></div><divclass="row"><divclass="col"><pre><code>{{response}}</code></pre></div></div></div><divclass="siout"><b-buttonvariant="secondary"@click="signOut">Sign out</b-button></div></div></template><script>importfirebasefrom'firebase'importaxiosfrom'axios'exportdefault{name:'HelloWorld',data:function(){return{response:'',fill:null,msg:'良い写真ある?',name:firebase.auth().currentUser.email}},methods:{signOut:function(){firebase.auth().signOut().then(()=>{this.$router.push('/signin')})},handlerFileChange:asyncfunction(e){console.log('handlerFileChange')constfiles=e.target.files||e.dataTransfer.filesconstfile=files[0]letcontentBuffer=awaitthis.readFileAsync(file)// console.log(contentBuffer);this.sendCognitiveAsFile(contentBuffer)},readFileAsync:function(file){returnnewPromise((resolve,reject)=>{letreader=newFileReader()reader.onload=()=>{resolve(reader.result)}reader.onerror=rejectreader.readAsArrayBuffer(file)})},sendCognitiveAsFile:asyncfunction(contentBuffer){// const FACE_API_ENDPOINT_URL = '***'constqs=require('querystring')constparams=qs.stringify({'returnFaceId':'true','returnFaceLandmarks':'false','returnFaceAttributes':'age,gender,headPose,smile,facialHair,glasses,'+'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'})constFACE_API_ENDPOINT_URL=`{取得したエンドポイント}/face/v1.0/detect?${params}`constconfig={url:FACE_API_ENDPOINT_URL,method:'post',headers:{'Content-type':'application/octet-stream','Ocp-Apim-Subscription-Key':'***'},data:contentBuffer}// axiostry{// POSTリクエストで送るconstresponseAzure=awaitaxios.request(config)console.log('post OK')// データ送信が成功するとレスポンスが来るconsole.log(responseAzure.data)// ageを取得constlookage=responseAzure.data[0].faceAttributes.agethis.response=`${lookage}歳にみえるね`}catch(error){console.error(error)}}}}</script><style>.colh1{font-size:100%;}div.form-group{max-width:380px;margin:auto;width:80%;}.lh5{line-height:5;}.siout{text-align:right;padding:1rem;position:fixed;top:1vh;right:3rem;}#app{position:relative;}</style>

freenomでドメインを取る

こちらを参考にまずは好きなドメインを取得
爆速!Vercelとfreenomで独自ドメインのサイトを無料で作成する - Qiita

NetlifyにDeploy

GitHubのリポジトリを作成して、作成したプロジェクトをPushしましょう。
Netlifyのページへ行き、[Get started in seconds]ボタンを押してスクリーンショット 2020-05-27 23.22.11.png
GitHubと連携しましょう
スクリーンショット 2020-05-27 23.21.54.png
Netlify の管理画面に入れたら、右上にある「New site from Git」を選択します。
スクリーンショット 2020-05-27 23.27.52.png
GitHubを選びます
先ほど作ったリポジトリを選んでDeploy settingsを埋めていきます
スクリーンショット 2020-05-27 23.30.17.png
Deploy siteボタンを押せば完了!

*ここでエラーになった人!(わたし!)
Gitの作り方によってはPublish先がdistだけでは展開できず、エラーにあることがあります。
Gitの置いてある位置からみているようなので、
works/fbaseApp/distとかになる場合もあります。

Deployが進んだら、ご丁寧に次の手順が出ています
スクリーンショット 2020-05-27 23.31.47.png
2を押しましょう
スクリーンショット 2020-05-27 23.42.22.png
ここに、freenomで作ったドメインを入れます
とりあえずエラーっぽい感じですが気にせずここを押すと
スクリーンショット 2020-05-27 23.56.17.png

Alternative: point A record to 1**.**.*.**

こういったものが書かれているのでこの番号をだしたまま
つぎにfreenomの管理画面からManage Freenom DNSにいきます
スクリーンショット 2020-05-27 23.50.24.png
ここに先ほどのNetlifyから取得した番号を入れる
スクリーンショット 2020-05-28 0.31.16.png

これでしばらく待てばfreenomでとったドメインにへんこうされる!

https://what-age.tk/
ユーザー登録するとどなたでもご覧いただけます。

きになるところ

こんなに表示は遅いものなのだろうか・・・
リダイレクトがうまくいっていないような・・・


Viewing all articles
Browse latest Browse all 8691

Trending Articles