Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
from tensorflow.keras.applications
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load pretrained EfficientNetB0
|
| 6 |
-
model = EfficientNetB0(weights="imagenet")
|
| 7 |
|
| 8 |
def predict(img):
|
| 9 |
-
#
|
| 10 |
-
img =
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
x =
|
| 15 |
x = np.expand_dims(x, axis=0)
|
| 16 |
x = preprocess_input(x)
|
| 17 |
|
| 18 |
-
# Predict
|
| 19 |
preds = model.predict(x)
|
| 20 |
-
decoded = decode_predictions(preds, top=
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return results
|
| 25 |
|
| 26 |
-
# Gradio
|
| 27 |
-
|
| 28 |
fn=predict,
|
| 29 |
inputs=gr.Image(type="pil"),
|
| 30 |
-
outputs=
|
| 31 |
-
title="πΎ
|
| 32 |
-
description="Upload an image of an animal or insect, and the model will
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
from tensorflow.keras.applications import EfficientNetB0
|
| 4 |
+
from tensorflow.keras.applications.efficientnet import preprocess_input, decode_predictions
|
| 5 |
+
from tensorflow.keras.preprocessing import image
|
| 6 |
|
| 7 |
+
# Load pretrained EfficientNetB0
|
| 8 |
+
model = EfficientNetB0(weights="imagenet", include_top=True, input_shape=(224, 224, 3))
|
| 9 |
|
| 10 |
def predict(img):
|
| 11 |
+
# Ensure image is RGB
|
| 12 |
+
if img.mode != "RGB":
|
| 13 |
+
img = img.convert("RGB")
|
| 14 |
|
| 15 |
+
img = img.resize((224, 224))
|
| 16 |
+
x = image.img_to_array(img)
|
| 17 |
x = np.expand_dims(x, axis=0)
|
| 18 |
x = preprocess_input(x)
|
| 19 |
|
|
|
|
| 20 |
preds = model.predict(x)
|
| 21 |
+
decoded = decode_predictions(preds, top=1)[0][0]
|
| 22 |
+
label = decoded[1]
|
| 23 |
+
confidence = float(decoded[2])
|
| 24 |
+
return f"π Prediction: {label} ({confidence:.2f} confidence)"
|
|
|
|
| 25 |
|
| 26 |
+
# Gradio Interface
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
fn=predict,
|
| 29 |
inputs=gr.Image(type="pil"),
|
| 30 |
+
outputs="text",
|
| 31 |
+
title="πΎ Animal & Insect Identifier",
|
| 32 |
+
description="Upload an image of an animal or insect, and the model will identify it."
|
| 33 |
)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|