|
import glob
|
|
import gradio as gr
|
|
from ultralytics import YOLO
|
|
from PIL import Image
|
|
|
|
|
|
model = YOLO("best.onnx", task="detect")
|
|
|
|
|
|
def predict(image, confidence_threshold):
|
|
|
|
results = model(image, conf=confidence_threshold)
|
|
|
|
|
|
result_image = results[0].plot()[:, :, ::-1]
|
|
return result_image
|
|
|
|
|
|
def clear():
|
|
return None, None
|
|
|
|
|
|
app_title = "🐟Fish Detector (Grayscale) ONNX Gradio Demo"
|
|
app_description = """
|
|
Upload an image to detect fish using an ONNX-optimized YOLO model. Adjust the confidence threshold to refine detection sensitivity.
|
|
|
|
**Links:**
|
|
- [Model on Hugging Face](https://huggingface.co/akridge/yolo11-fish-detector-grayscale)
|
|
- [Dataset on Hugging Face](https://huggingface.co/datasets/akridge/MOUSS_fish_segment_dataset_grayscale)
|
|
"""
|
|
|
|
|
|
examples = glob.glob("images/*.[jp][pn]g")
|
|
|
|
|
|
custom_css = """
|
|
.gradio-container h1 {
|
|
font-size: 2.5em; /* Increase title font size */
|
|
}
|
|
.gradio-container p {
|
|
font-size: 1.25em; /* Increase paragraph font size */
|
|
}
|
|
.gradio-container .gr-button {
|
|
font-size: 1.25em; /* Increase button text size */
|
|
}
|
|
.gradio-container .gr-input, .gradio-container .gr-slider {
|
|
font-size: 1.25em; /* Increase input and slider text size */
|
|
}
|
|
"""
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Ocean(), css=custom_css, title="Fish Detector (Grayscale) ONNX Gradio Demo") as interface:
|
|
gr.Markdown(f"<h1 style='text-align: center;'>{app_title}</h1>")
|
|
gr.Markdown(app_description)
|
|
|
|
|
|
with gr.Row():
|
|
image_input = gr.Image(type="pil", label="Upload an Image")
|
|
result_output = gr.Image(type="numpy", label="Detection Results")
|
|
|
|
|
|
with gr.Column():
|
|
confidence_slider = gr.Slider(
|
|
minimum=0.0, maximum=1.0, value=0.35,
|
|
label="Detection Confidence Threshold"
|
|
)
|
|
|
|
|
|
with gr.Row():
|
|
clear_button = gr.Button("Clear", variant="secondary")
|
|
run_button = gr.Button("Run Detection", variant="primary")
|
|
|
|
|
|
gr.Examples(
|
|
examples=examples,
|
|
inputs=image_input,
|
|
outputs=result_output,
|
|
examples_per_page=10,
|
|
label="Sample Images"
|
|
)
|
|
|
|
|
|
run_button.click(
|
|
fn=predict,
|
|
inputs=[image_input, confidence_slider],
|
|
outputs=result_output
|
|
)
|
|
|
|
clear_button.click(
|
|
fn=clear,
|
|
inputs=None,
|
|
outputs=[image_input, result_output]
|
|
)
|
|
|
|
|
|
interface.launch()
|
|
|