hb-setosys commited on
Commit
1540714
·
verified ·
1 Parent(s): dc62c6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import numpy as np
3
  import tensorflow as tf
4
- from tensorflow.keras.preprocessing.image import load_img, img_to_array
5
  from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
6
  from tensorflow.keras.models import load_model
7
  import gradio as gr
@@ -35,7 +35,8 @@ model = load_model("setosys_dogs_model.h5")
35
 
36
  # Preprocessing function
37
  def preprocess_image(image):
38
- img = load_img(image, target_size=(224, 224)) # Resize image to 224x224
 
39
  img_array = img_to_array(img)
40
  img_array = preprocess_input(img_array) # EfficientNetV2 preprocessing
41
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
@@ -48,12 +49,12 @@ def predict_dog_breed(image):
48
  class_idx = np.argmax(predictions) # Get index of the class with the highest probability
49
  breed = class_labels[class_idx] # Map the index to the breed name
50
  confidence = predictions[0][class_idx] # Confidence score
51
- return breed, confidence
52
 
53
  # Create a Gradio interface
54
  iface = gr.Interface(fn=predict_dog_breed,
55
- inputs=gr.Image(type="pil"), # Updated input type to 'gr.Image'
56
- outputs=[gr.Textbox(), gr.Number()], # Updated output to Textbox and Number
57
  live=True)
58
 
59
  # Launch the Gradio app
 
1
  import os
2
  import numpy as np
3
  import tensorflow as tf
4
+ from tensorflow.keras.preprocessing.image import img_to_array
5
  from tensorflow.keras.applications.efficientnet_v2 import preprocess_input
6
  from tensorflow.keras.models import load_model
7
  import gradio as gr
 
35
 
36
  # Preprocessing function
37
  def preprocess_image(image):
38
+ # No need to use load_img, the image is already a PIL image from Gradio
39
+ img = image.resize((224, 224)) # Resize image to 224x224
40
  img_array = img_to_array(img)
41
  img_array = preprocess_input(img_array) # EfficientNetV2 preprocessing
42
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
 
49
  class_idx = np.argmax(predictions) # Get index of the class with the highest probability
50
  breed = class_labels[class_idx] # Map the index to the breed name
51
  confidence = predictions[0][class_idx] # Confidence score
52
+ return [breed, confidence] # Return as a list
53
 
54
  # Create a Gradio interface
55
  iface = gr.Interface(fn=predict_dog_breed,
56
+ inputs=gr.Image(type="pil"), # Gradio automatically handles PIL images
57
+ outputs=gr.JSON(), # Outputs will be a JSON response (list of results)
58
  live=True)
59
 
60
  # Launch the Gradio app