Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from typing import Tuple
|
2 |
import gradio as gr
|
3 |
import supervision as sv
|
4 |
import numpy as np
|
@@ -6,13 +6,25 @@ from PIL import Image
|
|
6 |
from huggingface_hub import hf_hub_download
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Create annotators
|
18 |
LABEL_ANNOTATOR = sv.LabelAnnotator(text_color=sv.Color.BLACK)
|
@@ -20,9 +32,13 @@ BOX_ANNOTATOR = sv.BoxAnnotator()
|
|
20 |
|
21 |
def detect_and_annotate(
|
22 |
image: np.ndarray,
|
|
|
23 |
conf_threshold: float,
|
24 |
iou_threshold: float
|
25 |
) -> np.ndarray:
|
|
|
|
|
|
|
26 |
# Perform inference
|
27 |
results = model.predict(
|
28 |
image,
|
@@ -67,6 +83,12 @@ with gr.Blocks() as demo:
|
|
67 |
type='numpy'
|
68 |
)
|
69 |
with gr.Accordion("Detection Settings", open=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
with gr.Row():
|
71 |
conf_threshold = gr.Slider(
|
72 |
label="Confidence Threshold",
|
@@ -95,12 +117,13 @@ with gr.Blocks() as demo:
|
|
95 |
|
96 |
def process_image(
|
97 |
image: np.ndarray,
|
|
|
98 |
conf_threshold: float,
|
99 |
iou_threshold: float
|
100 |
) -> Tuple[np.ndarray, np.ndarray]:
|
101 |
if image is None:
|
102 |
return None, None
|
103 |
-
annotated_image = detect_and_annotate(image, conf_threshold, iou_threshold)
|
104 |
return image, annotated_image
|
105 |
|
106 |
def clear():
|
@@ -109,7 +132,7 @@ with gr.Blocks() as demo:
|
|
109 |
# Connect buttons to functions
|
110 |
detect_btn.click(
|
111 |
process_image,
|
112 |
-
inputs=[input_image, conf_threshold, iou_threshold],
|
113 |
outputs=[input_image, output_image]
|
114 |
)
|
115 |
clear_btn.click(
|
|
|
1 |
+
from typing import Tuple, Dict
|
2 |
import gradio as gr
|
3 |
import supervision as sv
|
4 |
import numpy as np
|
|
|
6 |
from huggingface_hub import hf_hub_download
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
+
# Define models
|
10 |
+
MODEL_OPTIONS = {
|
11 |
+
"YOLOv11-Nano": "medieval-yolov11n.pt",
|
12 |
+
"YOLOv11-Small": "medieval-yolov11s.pt",
|
13 |
+
"YOLOv11-Medium": "medieval-yolov11m.pt",
|
14 |
+
"YOLOv11-Large": "medieval-yolov11l.pt",
|
15 |
+
"YOLOv11-XLarge": "medieval-yolov11x.pt"
|
16 |
+
}
|
17 |
+
|
18 |
+
# Dictionary to store loaded models
|
19 |
+
models: Dict[str, YOLO] = {}
|
20 |
+
|
21 |
+
# Load all models
|
22 |
+
for name, model_file in MODEL_OPTIONS.items():
|
23 |
+
model_path = hf_hub_download(
|
24 |
+
repo_id="biglam/medieval-manuscript-yolov11",
|
25 |
+
filename=model_file
|
26 |
+
)
|
27 |
+
models[name] = YOLO(model_path)
|
28 |
|
29 |
# Create annotators
|
30 |
LABEL_ANNOTATOR = sv.LabelAnnotator(text_color=sv.Color.BLACK)
|
|
|
32 |
|
33 |
def detect_and_annotate(
|
34 |
image: np.ndarray,
|
35 |
+
model_name: str,
|
36 |
conf_threshold: float,
|
37 |
iou_threshold: float
|
38 |
) -> np.ndarray:
|
39 |
+
# Get the selected model
|
40 |
+
model = models[model_name]
|
41 |
+
|
42 |
# Perform inference
|
43 |
results = model.predict(
|
44 |
image,
|
|
|
83 |
type='numpy'
|
84 |
)
|
85 |
with gr.Accordion("Detection Settings", open=True):
|
86 |
+
model_selector = gr.Dropdown(
|
87 |
+
choices=list(MODEL_OPTIONS.keys()),
|
88 |
+
value=list(MODEL_OPTIONS.keys())[0],
|
89 |
+
label="Model",
|
90 |
+
info="Select YOLO model variant"
|
91 |
+
)
|
92 |
with gr.Row():
|
93 |
conf_threshold = gr.Slider(
|
94 |
label="Confidence Threshold",
|
|
|
117 |
|
118 |
def process_image(
|
119 |
image: np.ndarray,
|
120 |
+
model_name: str,
|
121 |
conf_threshold: float,
|
122 |
iou_threshold: float
|
123 |
) -> Tuple[np.ndarray, np.ndarray]:
|
124 |
if image is None:
|
125 |
return None, None
|
126 |
+
annotated_image = detect_and_annotate(image, model_name, conf_threshold, iou_threshold)
|
127 |
return image, annotated_image
|
128 |
|
129 |
def clear():
|
|
|
132 |
# Connect buttons to functions
|
133 |
detect_btn.click(
|
134 |
process_image,
|
135 |
+
inputs=[input_image, model_selector, conf_threshold, iou_threshold],
|
136 |
outputs=[input_image, output_image]
|
137 |
)
|
138 |
clear_btn.click(
|