Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,37 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
-
import
|
4 |
-
from tensorflow.keras.preprocessing import image
|
5 |
from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
|
6 |
-
|
7 |
|
8 |
-
# Load the trained model
|
9 |
-
|
10 |
-
model = tf.keras.models.load_model(MODEL_PATH)
|
11 |
|
12 |
-
# Get class labels from
|
13 |
-
class_labels =
|
14 |
|
15 |
-
#
|
16 |
-
def preprocess_image(
|
17 |
-
|
18 |
-
|
19 |
-
img_array = np.array(img)
|
20 |
img_array = preprocess_input(img_array) # EfficientNetV2 preprocessing
|
21 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
22 |
return img_array
|
23 |
|
24 |
# Prediction function
|
25 |
-
def predict_dog_breed(
|
26 |
-
|
27 |
-
img_array = preprocess_image(img)
|
28 |
predictions = model.predict(img_array)
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
class_idx = np.argmax(predictions) # Index of the highest prediction probability
|
34 |
-
confidence = float(np.max(predictions)) # Confidence score
|
35 |
-
|
36 |
-
# Get predicted breed and its confidence score
|
37 |
-
predicted_breed = class_labels[class_idx] if class_idx < len(class_labels) else "Unknown"
|
38 |
-
|
39 |
-
return {predicted_breed: confidence}
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
title="Dog Breed Classifier",
|
47 |
-
description="Upload an image of a dog to predict its breed."
|
48 |
-
)
|
49 |
|
50 |
# Launch the Gradio app
|
51 |
-
|
52 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
|
|
4 |
from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
|
5 |
+
import numpy as np
|
6 |
|
7 |
+
# Load the trained model (replace with correct path to your model)
|
8 |
+
model = tf.keras.models.load_model("setosys_dogs_model.h5")
|
|
|
9 |
|
10 |
+
# Get class labels dynamically from model (from class_indices)
|
11 |
+
class_labels = {v: k for k, v in model.class_indices.items()}
|
12 |
|
13 |
+
# Preprocessing function for EfficientNetV2 model
|
14 |
+
def preprocess_image(image_path):
|
15 |
+
img = load_img(image_path, target_size=(224, 224)) # Resize image to 224x224
|
16 |
+
img_array = img_to_array(img)
|
|
|
17 |
img_array = preprocess_input(img_array) # EfficientNetV2 preprocessing
|
18 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
19 |
return img_array
|
20 |
|
21 |
# Prediction function
|
22 |
+
def predict_dog_breed(image):
|
23 |
+
img_array = preprocess_image(image)
|
|
|
24 |
predictions = model.predict(img_array)
|
25 |
+
class_idx = np.argmax(predictions) # Get index of the class with the highest probability
|
26 |
+
breed = class_labels[class_idx] # Get label using the index
|
27 |
+
confidence = predictions[0][class_idx]
|
28 |
+
return breed, confidence
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# Gradio interface definition
|
31 |
+
iface = gr.Interface(fn=predict_dog_breed,
|
32 |
+
inputs=gr.inputs.Image(type="filepath"),
|
33 |
+
outputs=["text", "number"],
|
34 |
+
live=True)
|
|
|
|
|
|
|
35 |
|
36 |
# Launch the Gradio app
|
37 |
+
iface.launch(share=True) # `share=True` gives you a public link
|
|