Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
from PIL import Image
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
model = tf.keras.models.load_model("animal_classifier.keras")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
class_names = sorted(os.listdir(train_dir))
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
img_array = tf.keras.utils.img_to_array(img)
|
| 19 |
img_array = np.expand_dims(img_array, axis=0)
|
| 20 |
-
img_array =
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
import json
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
+
# Load model and class names from local repo files
|
| 8 |
model = tf.keras.models.load_model("animal_classifier.keras")
|
| 9 |
|
| 10 |
+
with open("class_names.json", "r") as f:
|
| 11 |
+
class_names = json.load(f)
|
|
|
|
| 12 |
|
| 13 |
+
def predict(image: Image.Image):
|
| 14 |
+
image = image.resize((224, 224))
|
| 15 |
+
img_array = np.array(image)
|
|
|
|
| 16 |
img_array = np.expand_dims(img_array, axis=0)
|
| 17 |
+
img_array = img_array / 255.0 # normalize if used in training
|
| 18 |
|
| 19 |
+
preds = model.predict(img_array)
|
| 20 |
+
pred_class = np.argmax(preds, axis=1)[0]
|
| 21 |
+
return class_names[pred_class]
|
| 22 |
|
| 23 |
+
demo = gr.Interface(fn=predict,
|
| 24 |
+
inputs=gr.Image(type="pil"),
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="MobileNetV2 Animal Classifier",
|
| 27 |
+
description="Upload an image to classify the animal.")
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
demo.launch()
|