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

RaspberryPiで撮った写真をCustom Vision Serviceで画像判定してどの猫がいるかLINEに返す

$
0
0

はじめに

LINEBotからRaspberryPiで写真を撮ってLINEにおくる!で作ったLINEBotにAI機能を追加してどの猫がいるか教えてくれるものを作りました。

概要

LINEBotからRaspberryPiを動かして写真を撮り、Gyazoに送って画像判定で何が映っているかと撮影した写真をLINEに送るものです。
ppt.png

画像判定

画像判定はMicrosoft Custom Vision Serviceを使用。
茶色と白黒とグレーの3匹のねこを学習させ、mixとして色々な猫を追加学習させて、3匹のどの猫に近いかを教えてくれます。
スクリーンショット 2020-05-13 22.28.03.png
3匹以外の猫を判定するとmixが一番高い値で出ます!(catタグは全ての猫に追加)
スクリーンショット 2020-05-13 22.26.38.png

できたもの

使い方

RaspberryPiを猫がよくいる場所に置きます。
猫が見たいので LINEBotに「ねこ」と入れます。
(「ねこ」以外のワードには「何が見たいの?」と返してきます。)
写真を撮るのに時間がかかるので、「写真撮ってくるから待っててねー」、とつなぎのワードが入ります。
写真を撮って送るときに Azure Cognitive Vision ServicesのCustom Vision Serviceを使用してどの猫がいるかを画像を分類して、10秒ほどすると猫の種類と写真が送られてきます。

環境

MacBook Pro macOS Mojave
 Visual Studio Code 1.44.0
Azure Cognitive Vision Services
 Custom Vision Service
RaspberryPi 3B
 Release: 10
 Codename: buster
 Node.js v12.16.3
 npm v6.14.4
 ngrok
Pi camera
洗濯バサミ

コード

node.js
'use strict';constexpress=require('express');constline=require('@line/bot-sdk');constaxios=require('axios');constPORT=process.env.PORT||3000;constGyazo=require('gyazo-api');constgyazoclient=newGyazo('***');constconfig={channelSecret:'***',channelAccessToken:'***'};constapp=express();letwhichCat;app.get('/',(req,res)=>res.send('Hello LINE BOT!(GET)'));//ブラウザ確認用(無くても問題ない)app.post('/webhook',line.middleware(config),(req,res)=>{console.log(req.body.events);Promise.all(req.body.events.map(handleEvent)).then((result)=>res.json(result));});constclient=newline.Client(config);asyncfunctionhandleEvent(event){if(event.type!=='message'||event.message.type!=='text'){returnPromise.resolve(null);}letmes=event.message.text;if(event.message.text.match("ねこ")){awaitclient.replyMessage(event.replyToken,{type:"text",text:'写真を撮ってくるからちょっと待っててね'});awaitnyanpi();returnnyancoPic(event.source.userId);}else{returnclient.replyMessage(event.replyToken,{type:"text",text:'何が見たいのかな?'});}}constnyanpi=async(userId)=>{constPiCamera=require('pi-camera');constmyCamera=newPiCamera({mode:'photo',output:`${__dirname}/nyan.jpg`,width:640,height:480,nopreview:true,});awaitmyCamera.snap()awaitgyazoclient.upload('./nyan.jpg',{title:"my picture",desc:"upload from nodejs"})}//Custom Vision ServiceconstsendCustomVision=async(imageURL)=>{constCUSTOM_VISION_API_ENDPOINT_URL='***';// Prediction-Keyの値を Prediction-Key ヘッダーに入れる// JSONで送るので Content-type ヘッダーに application/json 指定constconfigCustomVisionAPI={url:CUSTOM_VISION_API_ENDPOINT_URL,method:'post',headers:{'Content-type':'application/json','Prediction-Key':'aff45523d4e4432aa530b4743874d0f5'},data:{url:imageURL}};// axiosの送信設定letresponseCustomVision;try{// POSTリクエストで送るresponseCustomVision=awaitaxios.request(configCustomVisionAPI);console.log("post OK");// データ送信が成功するとレスポンスが来るconsole.log(responseCustomVision.data);// 全てのタグにcatが含まれているので、近似値が高いものがcatタグの時は2番目を返すconstcatTag=responseCustomVision.data;if(catTag.predictions[0].tagName==="cat"){whichCat=catTag.predictions[1].tagName}else{whichCat=catTag.predictions[0].tagName;}}catch(error){console.log("post Error");// ダメなときはエラーconsole.error(error);}}//gyazoから最新の写真URLを取得constnyancoPic=async(userId)=>{constresponse=awaitgyazoclient.list()constgyazoimgUrl=response.data[0].url;//画像判定に送るconstsendcstmvison=awaitsendCustomVision(gyazoimgUrl);//タグの名前を猫の見た目に置き換えif(whichCat==="chairo"){whichCat="ちゃいろ";}elseif(whichCat==="shirokuro"){whichCat="しろくろ";}else{whichCat="グレー";}returnclient.pushMessage(userId,[{type:'image',originalContentUrl:gyazoimgUrl,previewImageUrl:gyazoimgUrl},{type:"text",text:`${whichCat}の猫がいるよ`}]);}app.listen(PORT);console.log(`Server running at ${PORT}`);

参考サイト

Computer Vision サービスを使用した画像処理 - Learn | Microsoft Docs

感想

AIが簡単に実装できた
グレーがシンガプーラという猫種で他のことめちゃくちゃ顔が同じなのでシンガプーラ判定やってみたい。
ngrokなので早く何かにdeployしたい。


Viewing all articles
Browse latest Browse all 8913

Trending Articles