Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,30 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from ultralytics import YOLO
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
-
#
|
| 7 |
model_path = hf_hub_download(repo_id="mohamedrayyan/murakamodel", filename="best.pt")
|
| 8 |
model = YOLO(model_path)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
def detect_objects(image, confidence_threshold):
|
| 12 |
-
results = model(image, conf=confidence_threshold)
|
| 13 |
annotated_frame = results[0].plot()
|
| 14 |
-
|
| 15 |
-
# Add custom overlay
|
| 16 |
-
annotated_frame = cv2.putText(
|
| 17 |
-
annotated_frame,
|
| 18 |
-
"πͺΈ Coral Health Assessment",
|
| 19 |
-
(10, 30),
|
| 20 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
| 21 |
-
0.7,
|
| 22 |
-
(0, 150, 255),
|
| 23 |
-
2
|
| 24 |
-
)
|
| 25 |
return annotated_frame
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
gr.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
with gr.Column(scale=1):
|
| 41 |
-
gr.Markdown("### π‘ Upload Coral Image")
|
| 42 |
-
input_image = gr.Image(type="numpy", label="Drag & Drop Reef Image")
|
| 43 |
-
|
| 44 |
-
with gr.Accordion("βοΈ Analysis Settings", open=False):
|
| 45 |
-
confidence_slider = gr.Slider(
|
| 46 |
-
minimum=0.1,
|
| 47 |
-
maximum=1.0,
|
| 48 |
-
value=0.5,
|
| 49 |
-
label="Detection Sensitivity",
|
| 50 |
-
info="Lower = More Sensitive | Higher = More Conservative"
|
| 51 |
-
)
|
| 52 |
-
gr.Markdown("π‘ **Pro Tip:** Start with 0.5 sensitivity for balanced results!")
|
| 53 |
-
|
| 54 |
-
with gr.Row():
|
| 55 |
-
submit_button = gr.Button("π Analyze", variant="primary")
|
| 56 |
-
clear_button = gr.Button("π§Ή Clear", variant="secondary")
|
| 57 |
-
|
| 58 |
-
with gr.Column(scale=2):
|
| 59 |
-
gr.Markdown("### π Detection Results")
|
| 60 |
-
output_image = gr.Image(type="numpy", label="Health Assessment")
|
| 61 |
-
|
| 62 |
-
with gr.Group(visible=False) as stats_box:
|
| 63 |
-
gr.Markdown("**π Quick Statistics**")
|
| 64 |
-
healthy_count = gr.Number(label="Healthy Colonies")
|
| 65 |
-
bleached_count = gr.Number(label="Bleached Areas")
|
| 66 |
-
|
| 67 |
-
download_button = gr.Button("πΎ Download Report", variant="primary")
|
| 68 |
-
|
| 69 |
-
# Interactive Functions
|
| 70 |
-
def update_stats(results):
|
| 71 |
-
counts = {"healthy": 0, "bleached": 0}
|
| 72 |
-
for box in results[0].boxes:
|
| 73 |
-
if model.names[int(box.cls)] == "healthy":
|
| 74 |
-
counts["healthy"] += 1
|
| 75 |
-
else:
|
| 76 |
-
counts["bleached"] += 1
|
| 77 |
-
return counts["healthy"], counts["bleached"]
|
| 78 |
-
|
| 79 |
-
submit_button.click(
|
| 80 |
-
fn=detect_objects,
|
| 81 |
-
inputs=[input_image, confidence_slider],
|
| 82 |
-
outputs=output_image
|
| 83 |
-
).then(
|
| 84 |
-
fn=lambda: gr.Group(visible=True),
|
| 85 |
-
outputs=stats_box
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
clear_button.click(
|
| 89 |
-
lambda: [None, None, gr.Group(visible=False)],
|
| 90 |
-
outputs=[input_image, output_image, stats_box]
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
download_button.click(
|
| 94 |
-
fn=lambda x: (x, "coral_report.png"),
|
| 95 |
-
inputs=output_image,
|
| 96 |
-
outputs=gr.File(label="Download Report")
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
# π Launch
|
| 100 |
-
demo.launch()
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import cv2
|
| 3 |
import gradio as gr
|
| 4 |
from ultralytics import YOLO
|
| 5 |
+
import numpy as np
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
+
# Download and load model
|
| 9 |
model_path = hf_hub_download(repo_id="mohamedrayyan/murakamodel", filename="best.pt")
|
| 10 |
model = YOLO(model_path)
|
| 11 |
|
| 12 |
+
# Detection function with confidence threshold
|
| 13 |
def detect_objects(image, confidence_threshold):
|
| 14 |
+
results = model(image, conf=confidence_threshold) # Use confidence threshold
|
| 15 |
annotated_frame = results[0].plot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return annotated_frame
|
| 17 |
|
| 18 |
+
# Create Gradio interface with slider
|
| 19 |
+
gr.Interface(
|
| 20 |
+
fn=detect_objects,
|
| 21 |
+
inputs=[
|
| 22 |
+
gr.Image(type="numpy", label="Upload Coral Image"),
|
| 23 |
+
gr.Slider(0.0, 1.0, value=0.25, label="Confidence Threshold",
|
| 24 |
+
info="Lower = more sensitive, Higher = fewer false positives")
|
| 25 |
+
],
|
| 26 |
+
outputs=gr.Image(type="numpy", label="Analysis Results"),
|
| 27 |
+
title="πͺΈ Muraka - AI Doctor for Corals",
|
| 28 |
+
description="Upload coral images to assess health status. Adjust confidence threshold to control detection sensitivity.",
|
| 29 |
+
allow_flagging="never"
|
| 30 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|