Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
|
@@ -54,7 +54,12 @@ def predict(image):
|
|
| 54 |
x_max = int(bbox[2] * width)
|
| 55 |
y_max = int(bbox[3] * height)
|
| 56 |
print(f"Prediction: {classification} with confidence {confidence:.2f}")
|
| 57 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Function to draw bounding box on the image
|
| 60 |
def draw_bounding_box(image, bbox, label):
|
|
@@ -67,24 +72,6 @@ def draw_bounding_box(image, bbox, label):
|
|
| 67 |
draw.text(text_position, label, fill="red")
|
| 68 |
return image
|
| 69 |
|
| 70 |
-
# Combined predict function that accepts two inputs: an uploaded image and a selected example image.
|
| 71 |
-
def combined_predict(uploaded_image, selected_image_path):
|
| 72 |
-
print("Combined predict function called.")
|
| 73 |
-
if uploaded_image is not None:
|
| 74 |
-
print("Using uploaded image.")
|
| 75 |
-
image = uploaded_image
|
| 76 |
-
elif selected_image_path is not None and selected_image_path != "":
|
| 77 |
-
print("Using selected example image:", selected_image_path)
|
| 78 |
-
try:
|
| 79 |
-
image = Image.open(selected_image_path)
|
| 80 |
-
except Exception as e:
|
| 81 |
-
print("Error opening selected image:", e)
|
| 82 |
-
return "Error loading image", "", "", None
|
| 83 |
-
else:
|
| 84 |
-
print("No image provided.")
|
| 85 |
-
return "No image provided", "", "", None
|
| 86 |
-
return predict(image)
|
| 87 |
-
|
| 88 |
# List of example image file paths (ensure these images are uploaded to your Space)
|
| 89 |
example_image_paths = [
|
| 90 |
"example1.png",
|
|
@@ -93,29 +80,38 @@ example_image_paths = [
|
|
| 93 |
"example4.png"
|
| 94 |
]
|
| 95 |
|
| 96 |
-
# Build the Gradio interface
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("# Seamount Detection")
|
| 99 |
-
gr.Markdown("**Instructions:** Either upload your own image
|
| 100 |
-
|
| 101 |
with gr.Row():
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
submit_btn = gr.Button("Predict")
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
output_image = gr.Image(label="Image with Bounding Box")
|
| 111 |
|
| 112 |
submit_btn.click(
|
| 113 |
-
|
| 114 |
-
inputs=
|
| 115 |
outputs=[output_class, output_confidence, output_bbox, output_image]
|
| 116 |
)
|
| 117 |
|
| 118 |
-
# DEBUG: Launch the app
|
| 119 |
print("Launching app...")
|
| 120 |
if __name__ == "__main__":
|
| 121 |
demo.launch()
|
|
|
|
| 54 |
x_max = int(bbox[2] * width)
|
| 55 |
y_max = int(bbox[3] * height)
|
| 56 |
print(f"Prediction: {classification} with confidence {confidence:.2f}")
|
| 57 |
+
return (
|
| 58 |
+
classification,
|
| 59 |
+
confidence,
|
| 60 |
+
(x_min, y_min, x_max, y_max),
|
| 61 |
+
draw_bounding_box(image, (x_min, y_min, x_max, y_max), classification)
|
| 62 |
+
)
|
| 63 |
|
| 64 |
# Function to draw bounding box on the image
|
| 65 |
def draw_bounding_box(image, bbox, label):
|
|
|
|
| 72 |
draw.text(text_position, label, fill="red")
|
| 73 |
return image
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# List of example image file paths (ensure these images are uploaded to your Space)
|
| 76 |
example_image_paths = [
|
| 77 |
"example1.png",
|
|
|
|
| 80 |
"example4.png"
|
| 81 |
]
|
| 82 |
|
| 83 |
+
# Build the Gradio interface using Blocks
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
gr.Markdown("# Seamount Detection")
|
| 86 |
+
gr.Markdown("**Instructions:** Either upload your own image or select one of the example images below. Clicking an example will populate the input field.")
|
| 87 |
+
|
| 88 |
with gr.Row():
|
| 89 |
+
# Single image input component
|
| 90 |
+
uploaded_image = gr.Image(type="pil", label="Upload an Image")
|
| 91 |
+
|
| 92 |
+
# Examples component displays the images as clickable thumbnails.
|
| 93 |
+
gr.Examples(
|
| 94 |
+
examples=[[path] for path in example_image_paths],
|
| 95 |
+
inputs=uploaded_image,
|
| 96 |
+
label="Or select an example image"
|
| 97 |
+
)
|
| 98 |
|
| 99 |
submit_btn = gr.Button("Predict")
|
| 100 |
|
| 101 |
+
with gr.Row():
|
| 102 |
+
output_class = gr.Text(label="Classification")
|
| 103 |
+
output_confidence = gr.Text(label="Confidence Score")
|
| 104 |
+
output_bbox = gr.Text(label="Bounding Box (x_min, y_min, x_max, y_max)")
|
| 105 |
+
|
| 106 |
output_image = gr.Image(label="Image with Bounding Box")
|
| 107 |
|
| 108 |
submit_btn.click(
|
| 109 |
+
predict,
|
| 110 |
+
inputs=uploaded_image,
|
| 111 |
outputs=[output_class, output_confidence, output_bbox, output_image]
|
| 112 |
)
|
| 113 |
|
| 114 |
+
# DEBUG: Launch the app
|
| 115 |
print("Launching app...")
|
| 116 |
if __name__ == "__main__":
|
| 117 |
demo.launch()
|