NKASG's picture
Update app.py
fb53d2e verified
raw
history blame
2.1 kB
# import gradio as gr
# import tensorflow as tf
# from PIL import Image
# import numpy as np
# # Teachable Machineで作成したモデルのパス
# model_path = "keras_model.h5"
# # モデルの読み込み
# model = tf.keras.models.load_model(model_path)
# # クラスのラベル(Teachable Machineで指定したクラス名と同じ順序で設定)
# class_labels = [
# "Cotton",
# "Linen",
# "Silk",
# "Wool",
# "Polyester",
# "Nylon",
# "Rayon",
# "Fleece",
# "Leather",
# "Synth Leather"
# ]
# # GradioのUIの設定
# def classify_image(img):
# # 画像の前処理
# img = Image.fromarray((img * 255).astype(np.uint8))
# img = img.resize((224, 224))
# img_array = tf.keras.preprocessing.image.img_to_array(img)
# img_array = tf.expand_dims(img_array, 0) # バッチの次元を追加
# # モデルの予測
# predictions = model.predict(img_array)
# predicted_class = class_labels[np.argmax(predictions)]
# return predicted_class
# # Gradio UIの作成
# iface = gr.Interface(
# fn=classify_image,
# inputs=gr.Image(),
# outputs=gr.Textbox(),
# # live=True,
# )
# # UIの起動
# iface.launch(share=True)
from fastai.vision.all import *
import gradio as gr
learn = load_learner('export.pkl')
categories = ('Blouse', 'Dress', 'Pants', 'Shirt', 'Shorts')
title = "Clothing Identifier"
def classify_image(img):
pred, idx, probs = learn.predict(img)
highest_prob_index = probs.argmax() # Find the index of the highest probability
return categories[highest_prob_index] # Return the class with the highest probability
iface = gr.Interface(
fn=classify_image,
inputs=gr.Image(),
outputs=gr.Textbox(),
title = title,
examples = ['dress.jpg', 'shirt.jpg', 'pants.jpg', 'shorts.jpg'],
# live=True,
)
iface.launch(share=True)
# image = gr.Image(shape=(512, 512))
# label = gr.Label()
# examples = ['dress.jpg', 'shirt.jpg', 'pants.jpg', 'shorts.jpg']
# intf = gr.Interface(fn=classify_image, title=title, inputs=image, outputs=label, examples=examples)
# intf.launch(inline=False)