Spaces:
Sleeping
Sleeping
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) | |