feat: suporte para processamento de pastas de imagens e OpenVinoRuntime
Browse files
app.py
CHANGED
|
@@ -1,21 +1,22 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import pandas as pd
|
| 4 |
import time
|
|
|
|
|
|
|
|
|
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
| 6 |
import onnxruntime as ort
|
| 7 |
-
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import os
|
| 10 |
-
import sqlite3
|
| 11 |
-
from datetime import datetime
|
| 12 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 13 |
|
| 14 |
# Model info
|
| 15 |
REPO_ID = "tech4humans/yolov8s-signature-detector"
|
| 16 |
-
FILENAME = "
|
| 17 |
MODEL_DIR = "model"
|
| 18 |
MODEL_PATH = os.path.join(MODEL_DIR, "model.onnx")
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def download_model():
|
|
@@ -55,7 +56,7 @@ def download_model():
|
|
| 55 |
|
| 56 |
|
| 57 |
class MetricsStorage:
|
| 58 |
-
def __init__(self, db_path=
|
| 59 |
self.db_path = db_path
|
| 60 |
self.setup_database()
|
| 61 |
|
|
@@ -123,7 +124,7 @@ class SignatureDetector:
|
|
| 123 |
|
| 124 |
# Initialize ONNX Runtime session
|
| 125 |
self.session = ort.InferenceSession(
|
| 126 |
-
MODEL_PATH, providers=["
|
| 127 |
)
|
| 128 |
|
| 129 |
self.metrics_storage = MetricsStorage()
|
|
@@ -421,6 +422,23 @@ def create_gradio_interface():
|
|
| 421 |
line_fig,
|
| 422 |
)
|
| 423 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
with gr.Blocks(
|
| 425 |
theme=gr.themes.Soft(
|
| 426 |
primary_hue="indigo", secondary_hue="gray", neutral_hue="gray"
|
|
@@ -443,13 +461,29 @@ def create_gradio_interface():
|
|
| 443 |
with gr.Row(equal_height=True, elem_classes="main-container"):
|
| 444 |
# Coluna da esquerda para controles e informações
|
| 445 |
with gr.Column(scale=1):
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
|
| 454 |
with gr.Group():
|
| 455 |
confidence_threshold = gr.Slider(
|
|
@@ -474,6 +508,7 @@ def create_gradio_interface():
|
|
| 474 |
|
| 475 |
with gr.Accordion("Exemplos", open=True):
|
| 476 |
gr.Examples(
|
|
|
|
| 477 |
examples=[
|
| 478 |
["assets/images/example_{i}.jpg".format(i=i)]
|
| 479 |
for i in range(
|
|
@@ -523,14 +558,21 @@ def create_gradio_interface():
|
|
| 523 |
"""
|
| 524 |
)
|
| 525 |
|
| 526 |
-
|
|
|
|
| 527 |
|
| 528 |
-
|
| 529 |
fn=process_image,
|
| 530 |
inputs=[input_image, confidence_threshold, iou_threshold],
|
| 531 |
outputs=[output_image, total_inferences, hist_plot, line_plot],
|
| 532 |
)
|
| 533 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
# Carregar métricas iniciais ao carregar a página
|
| 535 |
iface.load(
|
| 536 |
fn=detector.load_initial_metrics,
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sqlite3
|
|
|
|
| 3 |
import time
|
| 4 |
+
|
| 5 |
+
import cv2
|
| 6 |
+
import gradio as gr
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
+
import numpy as np
|
| 9 |
import onnxruntime as ort
|
| 10 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
+
from PIL import Image
|
| 13 |
|
| 14 |
# Model info
|
| 15 |
REPO_ID = "tech4humans/yolov8s-signature-detector"
|
| 16 |
+
FILENAME = "yolov8s.onnx"
|
| 17 |
MODEL_DIR = "model"
|
| 18 |
MODEL_PATH = os.path.join(MODEL_DIR, "model.onnx")
|
| 19 |
+
DATABASE_PATH = os.path.join(os.getcwd(), "db", "metrics.db")
|
| 20 |
|
| 21 |
|
| 22 |
def download_model():
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
class MetricsStorage:
|
| 59 |
+
def __init__(self, db_path=DATABASE_PATH):
|
| 60 |
self.db_path = db_path
|
| 61 |
self.setup_database()
|
| 62 |
|
|
|
|
| 124 |
|
| 125 |
# Initialize ONNX Runtime session
|
| 126 |
self.session = ort.InferenceSession(
|
| 127 |
+
MODEL_PATH, providers=["OpenVINOExecutionProvider"]
|
| 128 |
)
|
| 129 |
|
| 130 |
self.metrics_storage = MetricsStorage()
|
|
|
|
| 422 |
line_fig,
|
| 423 |
)
|
| 424 |
|
| 425 |
+
def process_folder(files_path, conf_thres, iou_thres):
|
| 426 |
+
if not files_path:
|
| 427 |
+
return None, None, None, None
|
| 428 |
+
|
| 429 |
+
valid_extensions = [".jpg", ".jpeg", ".png"]
|
| 430 |
+
image_files = [
|
| 431 |
+
f for f in files_path if os.path.splitext(f.lower())[1] in valid_extensions
|
| 432 |
+
]
|
| 433 |
+
|
| 434 |
+
if not image_files:
|
| 435 |
+
return None, None, None, None
|
| 436 |
+
|
| 437 |
+
for img_file in image_files:
|
| 438 |
+
image = Image.open(img_file)
|
| 439 |
+
|
| 440 |
+
yield process_image(image, conf_thres, iou_thres)
|
| 441 |
+
|
| 442 |
with gr.Blocks(
|
| 443 |
theme=gr.themes.Soft(
|
| 444 |
primary_hue="indigo", secondary_hue="gray", neutral_hue="gray"
|
|
|
|
| 461 |
with gr.Row(equal_height=True, elem_classes="main-container"):
|
| 462 |
# Coluna da esquerda para controles e informações
|
| 463 |
with gr.Column(scale=1):
|
| 464 |
+
with gr.Tab("Imagem Única"):
|
| 465 |
+
input_image = gr.Image(
|
| 466 |
+
label="Faça o upload do seu documento", type="pil"
|
| 467 |
+
)
|
| 468 |
+
with gr.Row():
|
| 469 |
+
clear_single_btn = gr.ClearButton([input_image], value="Limpar")
|
| 470 |
+
detect_single_btn = gr.Button(
|
| 471 |
+
"Detectar", elem_classes="custom-button"
|
| 472 |
+
)
|
| 473 |
+
|
| 474 |
+
with gr.Tab("Pasta de Imagens"):
|
| 475 |
+
input_folder = gr.File(
|
| 476 |
+
label="Faça o upload de uma pasta com imagens",
|
| 477 |
+
file_count="directory",
|
| 478 |
+
type="filepath",
|
| 479 |
+
)
|
| 480 |
+
with gr.Row():
|
| 481 |
+
clear_folder_btn = gr.ClearButton(
|
| 482 |
+
[input_folder], value="Limpar"
|
| 483 |
+
)
|
| 484 |
+
detect_folder_btn = gr.Button(
|
| 485 |
+
"Detectar", elem_classes="custom-button"
|
| 486 |
+
)
|
| 487 |
|
| 488 |
with gr.Group():
|
| 489 |
confidence_threshold = gr.Slider(
|
|
|
|
| 508 |
|
| 509 |
with gr.Accordion("Exemplos", open=True):
|
| 510 |
gr.Examples(
|
| 511 |
+
label="Exemplos de Imagens",
|
| 512 |
examples=[
|
| 513 |
["assets/images/example_{i}.jpg".format(i=i)]
|
| 514 |
for i in range(
|
|
|
|
| 558 |
"""
|
| 559 |
)
|
| 560 |
|
| 561 |
+
clear_single_btn.add([output_image])
|
| 562 |
+
clear_folder_btn.add([output_image])
|
| 563 |
|
| 564 |
+
detect_single_btn.click(
|
| 565 |
fn=process_image,
|
| 566 |
inputs=[input_image, confidence_threshold, iou_threshold],
|
| 567 |
outputs=[output_image, total_inferences, hist_plot, line_plot],
|
| 568 |
)
|
| 569 |
|
| 570 |
+
detect_folder_btn.click(
|
| 571 |
+
fn=process_folder,
|
| 572 |
+
inputs=[input_folder, confidence_threshold, iou_threshold],
|
| 573 |
+
outputs=[output_image, total_inferences, hist_plot, line_plot],
|
| 574 |
+
)
|
| 575 |
+
|
| 576 |
# Carregar métricas iniciais ao carregar a página
|
| 577 |
iface.load(
|
| 578 |
fn=detector.load_initial_metrics,
|