hb-setosys commited on
Commit
7168d1f
·
verified ·
1 Parent(s): c1a4833

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -6,12 +6,12 @@ from tensorflow.keras.preprocessing.image import img_to_array
6
  from PIL import Image
7
  import numpy as np
8
 
9
- # Load a stronger pretrained model (EfficientNetV2L)
10
  model = EfficientNetV2L(weights="imagenet")
11
 
12
  def predict_image(image):
13
  """
14
- Process the uploaded image and return the top 5 predictions as a list.
15
  """
16
  try:
17
  # Preprocess the image
@@ -22,23 +22,22 @@ def predict_image(image):
22
 
23
  # Get predictions
24
  predictions = model.predict(image_array)
25
- decoded_predictions = decode_predictions(predictions, top=5)[0]
26
 
27
- # Format predictions as a list of tuples (label, confidence)
28
- results = [(label, float(confidence)) for _, label, confidence in decoded_predictions]
29
  return results
30
 
31
  except Exception as e:
32
- return [("Error", str(e))]
33
 
34
- # Create the Gradio interface with gr.Dataframe for displaying predictions
35
  interface = gr.Interface(
36
  fn=predict_image,
37
  inputs=gr.Image(type="pil"), # Accepts an image input
38
- outputs=gr.Dataframe(headers=["Label", "Confidence"], type="array"), # Shows top 5 predictions as a table
39
- title="Image Classifier",
40
- description="Upload an image, and the model will predict what's in the image with higher accuracy.",
41
- examples=["dog.jpg", "cat.jpg", "building.jpg", "tree.jpg"], # Example images for testing
42
  )
43
 
44
  # Launch the Gradio app
 
6
  from PIL import Image
7
  import numpy as np
8
 
9
+ # Load the stronger pre-trained model (EfficientNetV2L)
10
  model = EfficientNetV2L(weights="imagenet")
11
 
12
  def predict_image(image):
13
  """
14
+ Process the uploaded image and return the top 3 predictions as a dictionary.
15
  """
16
  try:
17
  # Preprocess the image
 
22
 
23
  # Get predictions
24
  predictions = model.predict(image_array)
25
+ decoded_predictions = decode_predictions(predictions, top=3)[0]
26
 
27
+ # Format predictions as a dictionary (label -> confidence)
28
+ results = {label: float(confidence) for _, label, confidence in decoded_predictions}
29
  return results
30
 
31
  except Exception as e:
32
+ return {"Error": str(e)}
33
 
34
+ # Create the Gradio interface
35
  interface = gr.Interface(
36
  fn=predict_image,
37
  inputs=gr.Image(type="pil"), # Accepts an image input
38
+ outputs=gr.Label(num_top_classes=3), # Shows top 3 predictions with confidence
39
+ title="EfficientNetV2L Image Classifier",
40
+ description="Upload an image, and the model will predict what's in the image with higher accuracy."
 
41
  )
42
 
43
  # Launch the Gradio app