upload 2 files
Browse files- app.py +31 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# 事前訓練済みの画像分類モデルをロード
|
| 6 |
+
model = tf.keras.applications.MobileNetV2(weights="imagenet")
|
| 7 |
+
|
| 8 |
+
# 画像をモデルの入力形式に変換する関数
|
| 9 |
+
def preprocess_image(image):
|
| 10 |
+
image = tf.image.resize(image, (224, 224))
|
| 11 |
+
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
|
| 12 |
+
return np.expand_dims(image, axis=0)
|
| 13 |
+
|
| 14 |
+
# 予測を実行し、結果を返す関数
|
| 15 |
+
def predict(image):
|
| 16 |
+
image = preprocess_image(image)
|
| 17 |
+
predictions = model.predict(image)
|
| 18 |
+
decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=1)
|
| 19 |
+
return decoded_predictions[0][0][1] # ラベルを返す
|
| 20 |
+
|
| 21 |
+
# Gradioインターフェースの定義
|
| 22 |
+
iface = gr.Interface(
|
| 23 |
+
fn=predict,
|
| 24 |
+
inputs=gr.inputs.Image(type="numpy"),
|
| 25 |
+
outputs="text",
|
| 26 |
+
live=True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# アプリケーションを起動
|
| 30 |
+
iface.launch()
|
| 31 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
tensorflow
|
| 3 |
+
|