hb-setosys commited on
Commit
0a2550a
·
verified ·
1 Parent(s): 1237637

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -37
app.py CHANGED
@@ -1,52 +1,37 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- import numpy as np
4
- from tensorflow.keras.preprocessing import image
5
  from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
6
- from PIL import Image
7
 
8
- # Load the trained model
9
- MODEL_PATH = "setosys_dogs_model.h5"
10
- model = tf.keras.models.load_model(MODEL_PATH)
11
 
12
- # Get class labels from the model (assuming the model has a 'class_indices' attribute)
13
- class_labels = list(model.class_indices.keys()) # Fetch class labels from the model
14
 
15
- # Image preprocessing function using EfficientNetV2S
16
- def preprocess_image(img: Image.Image) -> np.ndarray:
17
- """Preprocess the image to match the model's input requirements."""
18
- img = img.resize((224, 224)) # Resize image to model input size
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(img: Image.Image) -> dict:
26
- """Predict the breed of the dog in the uploaded image."""
27
- img_array = preprocess_image(img)
28
  predictions = model.predict(img_array)
29
-
30
- # Check the shape of the predictions to make sure the output is correct
31
- print("Predictions Shape:", predictions.shape)
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
- # Create Gradio interface
42
- interface = gr.Interface(
43
- fn=predict_dog_breed,
44
- inputs=gr.Image(type="pil"),
45
- outputs=gr.Label(),
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
- if __name__ == "__main__":
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