ML5.classify

def setup
  createCanvas(300, 440)
  background(255)

  noStroke
  textFont("monospace")
  textSize(18)
  fill("black")
  text("認識中...", 100, 350)

  img = loadImage('dog_akita.jpg')
  image(img, 0, 0, 300, 300)
  ML5.classify(img, 5) do |results|
    fill(255)
    rect(0, 300, width, height - 300)   # 文字表示クリア
    fill("red")
    textStyle(BOLD)
    results.each_with_index do |r, i|
      # 信頼度とラベルを表示
      text(format("%4.1f%%  %s", r.confidence * 100, r.label), 10, 330 + i * 22)
    end
  end
end

ml5.jsリファレンス(参考情報)

[ml5.js] ImageClassifier

概要

画像の内容を認識します。

書式

ML5.classify(img, number) do |results| ... end

引数

引数名内容備考オプションデフォルト値
img認識する対象の画像Imageオブジェクト
p5.Element
number取得する認識結果の個数
...認識結果に対する処理

戻値

なし

備考

ブロック引数「results」は下記のようなデータ構造になっています。

  [                      # 引数 numberで指定した個数の要素(ハッシュ)を持つ配列
    {                    #   要素0
      confidence: r,     #     信頼度
      label: str         #     ラベル
    ],
    {                    #   要素1
      confidence: r,     #     信頼度
      label: str         #     ラベル
    ],
    ...
    ...
    ...
    {                    #   要素N
      confidence: r,     #     信頼度
      label: str         #     ラベル
    ],
  ]

関連