概要
普段は耳鼻科の開業医をしています。
前回の記事はこちら
GCP Cloud AutoML Vision を使った鼓膜画像分類
今回は作成したAutoML Visionの鼓膜画像分類モデルをNode.jsから使ってみました。
作成
1.プロジェクトの作成
こちらを参考にしました
Cloud AutoML: Node.js Client
index.js
require('dotenv').config();//.envを読み込むconstautoml=require('@google-cloud/automl');constfs=require('fs');// Create client for prediction service.constclient=newautoml.PredictionServiceClient();/**
* TODO(developer): Uncomment the following line before running the sample.
*/constprojectId=`The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;constcomputeRegion=`region-name, e.g. "us-central1"`;constmodelId=`id of the model, e.g. “ICN723541179344731436”`;constfilePath=`local text file path of content to be classified, e.g. "./resources/flower.png"`;constscoreThreshold=`value between 0.0 and 1.0, e.g. "0.5"`;// Get the full path of the model.constmodelFullId=client.modelPath(projectId,computeRegion,modelId);// Read the file content for prediction.constcontent=fs.readFileSync(filePath,'base64');constparams={};if(scoreThreshold){params.score_threshold=scoreThreshold;}// Set the payload by giving the content and type of the file.constpayload={};payload.image={imageBytes:content};asyncfunctiontest(){// params is additional domain-specific parameters.// currently there is no additional parameters supported.const[response]=awaitclient.predict({name:modelFullId,payload:payload,params:params,});console.log(`Prediction results:`);response.payload.forEach(result=>{console.log(`Predicted class name: ${result.displayName}`);console.log(`Predicted class score: ${result.classification.score}`);});}test();
2.AutoML用認証キーの作成
コンソール画面左上のナビゲーションメニューから『APIとサービス』➡『認証情報』
『JSON』➡『作成』を選択
JSONファイルがダウンロードされます
3.AutoML用認証キーのつなぎ込み
ダウンロードされたJSONファイルをindex.jsと同じフォルダーに入れる
.envを作成
GOOGLE_APPLICATION_CREDENTIALS=./ここにダウンロードされたJSONファイル名を記入
必要なパッケージをインストール
$ npm install @google-cloud/automl
$ npm install dotenv
4.認証情報を設定
index.jsを以下のように書き換える
index.js
constprojectId="自分のプロジェクト名";constcomputeRegion="us-central1";constmodelId="ICNで始まる番号";constfilePath="テストしたいローカル画像のフルパス"constscoreThreshold="0.5";
今回テストしたローカル画像はこちら
急性中耳炎の画像です
フルパスは以下のように区切らないとうまく動きませんでした(windows10)。
constfilePath="C:\\Users\\***\\data\\test\\aom\\WIN_20190529_08_40_52_Pro.jpg";
5.デプロイ
今回デプロイし終わるまで20~30分ほどかかりました。
ちなみにデプロイしたままにすると1日3000円ほど課金されますので。使わないときはデプロイを解除しましょう。
モデルのデプロイ解除の方法はこちら
テスト
index.jsを実行します。
Prediction class name:aom(急性中耳炎)と正しく判定されています。
Prediction class score(信頼度 0.0〜1.0の値が入ります)は1なのでかなり自信があるようです。
考察
Node.jsからAutoML Vision の鼓膜画像分類モデルを使うことができました。
次はLINE Botに組み込んでみたいと思います。