feat: Enhance Gradio interface with custom styling and improved layout for signature detection
Browse files- app.py +89 -23
- assets/images/example_2.jpg +0 -0
- assets/images/example_5.jpg +0 -0
- assets/images/example_6.jpg +0 -0
app.py
CHANGED
|
@@ -49,7 +49,6 @@ class SignatureDetector:
|
|
| 49 |
def __init__(self, model_path):
|
| 50 |
self.model_path = model_path
|
| 51 |
self.classes = ["signature"]
|
| 52 |
-
self.color_palette = np.random.uniform(0, 255, size=(len(self.classes), 3))
|
| 53 |
self.input_width = 640
|
| 54 |
self.input_height = 640
|
| 55 |
|
|
@@ -78,6 +77,7 @@ class SignatureDetector:
|
|
| 78 |
|
| 79 |
def draw_detections(self, img, box, score, class_id):
|
| 80 |
x1, y1, w, h = box
|
|
|
|
| 81 |
color = self.color_palette[class_id]
|
| 82 |
|
| 83 |
cv2.rectangle(img, (int(x1), int(y1)), (int(x1 + w), int(y1 + h)), color, 2)
|
|
@@ -156,29 +156,95 @@ def create_gradio_interface():
|
|
| 156 |
# Initialize the detector
|
| 157 |
detector = SignatureDetector(MODEL_PATH)
|
| 158 |
|
| 159 |
-
# Create Gradio interface
|
| 160 |
-
iface = gr.Interface(
|
| 161 |
-
fn=detector.detect,
|
| 162 |
-
inputs=[
|
| 163 |
-
gr.Image(label="Upload your Document", type="pil"),
|
| 164 |
-
gr.Slider(minimum=0.0, maximum=1.0, value=0.2, step=0.05,
|
| 165 |
-
label="Confidence Threshold",
|
| 166 |
-
info="Adjust the minimum confidence score required for detection"),
|
| 167 |
-
gr.Slider(minimum=0.0, maximum=1.0, value=0.5, step=0.05,
|
| 168 |
-
label="IoU Threshold",
|
| 169 |
-
info="Adjust the Intersection over Union threshold for NMS")
|
| 170 |
-
],
|
| 171 |
-
outputs=gr.Image(label="Detection Results"),
|
| 172 |
-
title="Signature Detector",
|
| 173 |
-
description="Upload an image to detect signatures using YOLOv8. Use the sliders to adjust detection sensitivity.",
|
| 174 |
-
examples=[
|
| 175 |
-
["assets/images/example_1.jpg", 0.2, 0.5],
|
| 176 |
-
["assets/images/example_2.jpg", 0.2, 0.5],
|
| 177 |
-
["assets/images/example_3.jpg", 0.2, 0.5],
|
| 178 |
-
["assets/images/example_4.jpg", 0.2, 0.5]
|
| 179 |
-
]
|
| 180 |
-
)
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
return iface
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
|
|
|
| 49 |
def __init__(self, model_path):
|
| 50 |
self.model_path = model_path
|
| 51 |
self.classes = ["signature"]
|
|
|
|
| 52 |
self.input_width = 640
|
| 53 |
self.input_height = 640
|
| 54 |
|
|
|
|
| 77 |
|
| 78 |
def draw_detections(self, img, box, score, class_id):
|
| 79 |
x1, y1, w, h = box
|
| 80 |
+
self.color_palette = np.random.uniform(0, 255, size=(len(self.classes), 3))
|
| 81 |
color = self.color_palette[class_id]
|
| 82 |
|
| 83 |
cv2.rectangle(img, (int(x1), int(y1)), (int(x1 + w), int(y1 + h)), color, 2)
|
|
|
|
| 156 |
# Initialize the detector
|
| 157 |
detector = SignatureDetector(MODEL_PATH)
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
css = """
|
| 161 |
+
.custom-button {
|
| 162 |
+
background-color: #b0ffb8 !important;
|
| 163 |
+
color: black !important;
|
| 164 |
+
}
|
| 165 |
+
.custom-button:hover {
|
| 166 |
+
background-color: #b0ffb8b3 !important;
|
| 167 |
+
}
|
| 168 |
+
"""
|
| 169 |
+
|
| 170 |
+
with gr.Blocks(
|
| 171 |
+
theme = gr.themes.Soft(
|
| 172 |
+
primary_hue="indigo",
|
| 173 |
+
secondary_hue="gray",
|
| 174 |
+
neutral_hue="gray"
|
| 175 |
+
),
|
| 176 |
+
css=css
|
| 177 |
+
) as iface:
|
| 178 |
+
gr.Markdown(
|
| 179 |
+
"""
|
| 180 |
+
<div style="display: flex; align-items: center;">
|
| 181 |
+
<img src="https://cdn.prod.website-files.com/65155fabb679475d43638cde/65396826ed65fb2d37f242cf_tech4humans.png" alt="logo" style="width: 50px; height: 50px; margin-right: 15px; vertical-align: middle;">
|
| 182 |
+
<span style="font-size: 24px; font-weight: bold;">Tech4Humans - Detector de Assinaturas</span>
|
| 183 |
+
</div>
|
| 184 |
+
""",
|
| 185 |
+
elem_id="header"
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
with gr.Row():
|
| 190 |
+
with gr.Column(): # Coluna para a imagem de entrada e controles
|
| 191 |
+
input_image = gr.Image(label="Faça o upload do seu documento", type="pil")
|
| 192 |
+
|
| 193 |
+
with gr.Row(): # Linha para os botões
|
| 194 |
+
clear_btn = gr.ClearButton([input_image], value="Limpar")
|
| 195 |
+
submit_btn = gr.Button("Detectar", elem_classes="custom-button")
|
| 196 |
+
|
| 197 |
+
confidence_threshold = gr.Slider(
|
| 198 |
+
minimum=0.0,
|
| 199 |
+
maximum=1.0,
|
| 200 |
+
value=0.2,
|
| 201 |
+
step=0.05,
|
| 202 |
+
label="Limiar de Confiança",
|
| 203 |
+
info="Ajuste a pontuação mínima de confiança necessária para detecção."
|
| 204 |
+
)
|
| 205 |
+
iou_threshold = gr.Slider(
|
| 206 |
+
minimum=0.0,
|
| 207 |
+
maximum=1.0,
|
| 208 |
+
value=0.5,
|
| 209 |
+
step=0.05,
|
| 210 |
+
label="Limiar de IoU",
|
| 211 |
+
info="Ajuste o limiar de Interseção sobre União para NMS."
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
output_image = gr.Image(label="Resultados da Detecção") # Em outra coluna
|
| 215 |
+
|
| 216 |
+
clear_btn.add(output_image)
|
| 217 |
+
|
| 218 |
+
gr.Examples(
|
| 219 |
+
examples=[
|
| 220 |
+
["assets/images/example_1.jpg"],
|
| 221 |
+
["assets/images/example_2.jpg"],
|
| 222 |
+
["assets/images/example_3.jpg"],
|
| 223 |
+
["assets/images/example_4.jpg"],
|
| 224 |
+
["assets/images/example_5.jpg"],
|
| 225 |
+
["assets/images/example_6.jpg"]
|
| 226 |
+
],
|
| 227 |
+
inputs=input_image,
|
| 228 |
+
outputs=output_image,
|
| 229 |
+
fn=detector.detect,
|
| 230 |
+
label="Exemplos",
|
| 231 |
+
cache_examples=False
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
submit_btn.click(
|
| 236 |
+
fn=detector.detect,
|
| 237 |
+
inputs=[input_image, confidence_threshold, iou_threshold],
|
| 238 |
+
outputs=output_image,
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
gr.Markdown(
|
| 242 |
+
"""
|
| 243 |
+
---
|
| 244 |
+
**Desenvolvido por [Tech4Humans](https://www.tech4h.com.br/)** | **Modelo:** [YOLOv8s](https://huggingface.co/tech4humans/yolov8s-signature-detector) | **Datasets:** [Tobacco800](https://paperswithcode.com/dataset/tobacco-800), [signatures-xc8up](https://universe.roboflow.com/roboflow-100/signatures-xc8up)
|
| 245 |
+
"""
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
return iface
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
assets/images/example_2.jpg
CHANGED
|
|
assets/images/example_5.jpg
ADDED
|
assets/images/example_6.jpg
ADDED
|