Update app.py
Browse files
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
|
10 |
model = EfficientNetV2L(weights="imagenet")
|
11 |
|
12 |
def predict_image(image):
|
13 |
"""
|
14 |
-
Process the uploaded image and return the top
|
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=
|
26 |
|
27 |
-
# Format predictions as a
|
28 |
-
results =
|
29 |
return results
|
30 |
|
31 |
except Exception as e:
|
32 |
-
return
|
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.
|
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
|