LoginSignup
1
1

More than 5 years have passed since last update.

Cognitive Service の Custom Vision API に画像を渡し結果を受け取って何の画像かを返却する Azure Function のコード

Last updated at Posted at 2017-07-25

Cognitive Service の Custom Vision API と Azure Function の勉強のため、作ってみたコードです。
コードだけ貼り付けただけでは意味ないかもしれないから、今後 Custom Vision API の使い方と Azure Function の作り方などもメモしたいと思いますが、今回はとりあえず Azure Function のコードのみ!

index.js
module.exports = function (context, req) {

    // Custom Vision API 呼び出し用のパラメータ設定
    var options = {
        url: '[Custom Vision API Prediction の URL]',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Prediction-Key': '[Custom Vision API Prediction の Key]'
        },
        body: JSON.stringify({
            "Url": req.body   // Custom Vision API に渡す画像の URL
        })
    };

    var request = require('request');
    request(options, function (error, response, body) {

        var data = JSON.parse(body);

        // Custom Vision API の解析結果が 6 割 より大きかったら適合、小さかったら不適合にする
        if (data.Predictions[0].Probability > 0.6) {
            context.log(data.Predictions[0].Tag);
            context.res = {
                body: data.Predictions[0].Tag  // 適合した画像のタグ(ここに何の画像かを示す名称が入ってる)を返却
            };
        } else {
            context.res = {
                body: "該当なし"  // 不適合なので該当なしを返却
            };
        }

        context.done();
    }); 
};
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1