Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load the trained model (make sure the file is in the same directory)
|
| 9 |
+
model = tf.keras.models.load_model("animal_classifier.keras")
|
| 10 |
+
|
| 11 |
+
# Define class names (same order as training)
|
| 12 |
+
train_dir = "split_animals/train"
|
| 13 |
+
class_names = sorted(os.listdir(train_dir))
|
| 14 |
+
|
| 15 |
+
# Prediction function
|
| 16 |
+
def predict_image(img):
|
| 17 |
+
img = img.resize((224, 224))
|
| 18 |
+
img_array = tf.keras.utils.img_to_array(img)
|
| 19 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 20 |
+
img_array = preprocess_input(img_array)
|
| 21 |
+
|
| 22 |
+
prediction = model.predict(img_array)
|
| 23 |
+
predicted_class = class_names[np.argmax(prediction)]
|
| 24 |
+
return f"Predicted Animal: {predicted_class}"
|
| 25 |
+
|
| 26 |
+
# Launch Gradio app
|
| 27 |
+
app = gr.Interface(fn=predict_image, inputs=gr.Image(type="pil"), outputs="text", title="🧠🐾 TF-WildNet: Animal Classifier")
|
| 28 |
+
app.launch()
|