File size: 662 Bytes
a60082f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | """
2025 実装 — HuggingFace Transformers を使った画像分類
実質 5 行で前処理・推論・後処理がすべて完結する。
"""
from transformers import pipeline
# モデルのロード(初回のみダウンロード)
classifier = pipeline(
"image-classification",
model="google/vit-base-patch16-224",
)
def classify(image) -> list[dict]:
"""
Parameters
----------
image : PIL.Image | str
PIL 画像オブジェクト、またはファイルパス文字列
Returns
-------
list[dict]
[{"label": str, "score": float}, ...] 上位 5 件
"""
return classifier(image, top_k=5)
|