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
画像の内容を認識します。
| 引数名 | 内容 | 備考 | オプション | デフォルト値 |
|---|---|---|---|---|
| img | 認識する対象の画像 | Imageオブジェクト p5.Element | ||
| number | 取得する認識結果の個数 | |||
| ... | 認識結果に対する処理 |
なし
ブロック引数「results」は下記のようなデータ構造になっています。
[ # 引数 numberで指定した個数の要素(ハッシュ)を持つ配列
{ # 要素0
confidence: r, # 信頼度
label: str # ラベル
],
{ # 要素1
confidence: r, # 信頼度
label: str # ラベル
],
...
...
...
{ # 要素N
confidence: r, # 信頼度
label: str # ラベル
],
]