feat: aumentar limite de métricas recentes e adicionar campos de tempo médio e último tempo de inferência na interface
Browse files
app.py
CHANGED
|
@@ -85,7 +85,7 @@ class MetricsStorage:
|
|
| 85 |
)
|
| 86 |
conn.commit()
|
| 87 |
|
| 88 |
-
def get_recent_metrics(self, limit=
|
| 89 |
"""Get the most recent metrics from the database"""
|
| 90 |
with sqlite3.connect(self.db_path) as conn:
|
| 91 |
cursor = conn.cursor()
|
|
@@ -103,7 +103,7 @@ class MetricsStorage:
|
|
| 103 |
cursor.execute("SELECT COUNT(*) FROM inference_metrics")
|
| 104 |
return cursor.fetchone()[0]
|
| 105 |
|
| 106 |
-
def get_average_time(self, limit=
|
| 107 |
"""Get the average inference time from the most recent entries"""
|
| 108 |
with sqlite3.connect(self.db_path) as conn:
|
| 109 |
cursor = conn.cursor()
|
|
@@ -123,10 +123,10 @@ class SignatureDetector:
|
|
| 123 |
self.input_height = 640
|
| 124 |
|
| 125 |
# Initialize ONNX Runtime session
|
| 126 |
-
self.session = ort.InferenceSession(
|
| 127 |
-
|
|
|
|
| 128 |
)
|
| 129 |
-
self.session.set_providers(['OpenVINOExecutionProvider'], [{'device_type' : 'CPU'}])
|
| 130 |
|
| 131 |
self.metrics_storage = MetricsStorage()
|
| 132 |
|
|
@@ -154,7 +154,7 @@ class SignatureDetector:
|
|
| 154 |
metrics = self.get_metrics()
|
| 155 |
|
| 156 |
if not metrics["times"]: # Se não houver dados
|
| 157 |
-
return None, None, None, None
|
| 158 |
|
| 159 |
# Criar plots data
|
| 160 |
hist_data = pd.DataFrame({"Tempo (ms)": metrics["times"]})
|
|
@@ -178,6 +178,8 @@ class SignatureDetector:
|
|
| 178 |
f"Total de Inferências: {metrics['total_inferences']}",
|
| 179 |
hist_fig,
|
| 180 |
line_fig,
|
|
|
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
def create_plots(self, hist_data, line_data):
|
|
@@ -388,11 +390,16 @@ def create_gradio_interface():
|
|
| 388 |
color: #1f2937 !important;
|
| 389 |
margin-bottom: 1rem !important;
|
| 390 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
"""
|
| 392 |
|
| 393 |
def process_image(image, conf_thres, iou_thres):
|
| 394 |
if image is None:
|
| 395 |
-
return None, None, None, None
|
| 396 |
|
| 397 |
output_image, metrics = detector.detect(image, conf_thres, iou_thres)
|
| 398 |
|
|
@@ -421,11 +428,13 @@ def create_gradio_interface():
|
|
| 421 |
),
|
| 422 |
hist_fig,
|
| 423 |
line_fig,
|
|
|
|
|
|
|
| 424 |
)
|
| 425 |
|
| 426 |
def process_folder(files_path, conf_thres, iou_thres):
|
| 427 |
if not files_path:
|
| 428 |
-
return None, None, None, None
|
| 429 |
|
| 430 |
valid_extensions = [".jpg", ".jpeg", ".png"]
|
| 431 |
image_files = [
|
|
@@ -433,7 +442,7 @@ def create_gradio_interface():
|
|
| 433 |
]
|
| 434 |
|
| 435 |
if not image_files:
|
| 436 |
-
return None, None, None, None
|
| 437 |
|
| 438 |
for img_file in image_files:
|
| 439 |
image = Image.open(img_file)
|
|
@@ -532,6 +541,17 @@ def create_gradio_interface():
|
|
| 532 |
|
| 533 |
with gr.Column(scale=1):
|
| 534 |
line_plot = gr.Plot(label="Histórico de Tempos", container=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
with gr.Row(elem_classes="container"):
|
| 537 |
|
|
@@ -565,20 +585,41 @@ def create_gradio_interface():
|
|
| 565 |
detect_single_btn.click(
|
| 566 |
fn=process_image,
|
| 567 |
inputs=[input_image, confidence_threshold, iou_threshold],
|
| 568 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
)
|
| 570 |
|
| 571 |
detect_folder_btn.click(
|
| 572 |
fn=process_folder,
|
| 573 |
inputs=[input_folder, confidence_threshold, iou_threshold],
|
| 574 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
)
|
| 576 |
|
| 577 |
# Carregar métricas iniciais ao carregar a página
|
| 578 |
iface.load(
|
| 579 |
fn=detector.load_initial_metrics,
|
| 580 |
inputs=None,
|
| 581 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
)
|
| 583 |
|
| 584 |
return iface
|
|
|
|
| 85 |
)
|
| 86 |
conn.commit()
|
| 87 |
|
| 88 |
+
def get_recent_metrics(self, limit=80):
|
| 89 |
"""Get the most recent metrics from the database"""
|
| 90 |
with sqlite3.connect(self.db_path) as conn:
|
| 91 |
cursor = conn.cursor()
|
|
|
|
| 103 |
cursor.execute("SELECT COUNT(*) FROM inference_metrics")
|
| 104 |
return cursor.fetchone()[0]
|
| 105 |
|
| 106 |
+
def get_average_time(self, limit=80):
|
| 107 |
"""Get the average inference time from the most recent entries"""
|
| 108 |
with sqlite3.connect(self.db_path) as conn:
|
| 109 |
cursor = conn.cursor()
|
|
|
|
| 123 |
self.input_height = 640
|
| 124 |
|
| 125 |
# Initialize ONNX Runtime session
|
| 126 |
+
self.session = ort.InferenceSession(MODEL_PATH)
|
| 127 |
+
self.session.set_providers(
|
| 128 |
+
["OpenVINOExecutionProvider"], [{"device_type": "CPU"}]
|
| 129 |
)
|
|
|
|
| 130 |
|
| 131 |
self.metrics_storage = MetricsStorage()
|
| 132 |
|
|
|
|
| 154 |
metrics = self.get_metrics()
|
| 155 |
|
| 156 |
if not metrics["times"]: # Se não houver dados
|
| 157 |
+
return None, None, None, None, None, None
|
| 158 |
|
| 159 |
# Criar plots data
|
| 160 |
hist_data = pd.DataFrame({"Tempo (ms)": metrics["times"]})
|
|
|
|
| 178 |
f"Total de Inferências: {metrics['total_inferences']}",
|
| 179 |
hist_fig,
|
| 180 |
line_fig,
|
| 181 |
+
f"{metrics['avg_time']:.2f}",
|
| 182 |
+
f"{metrics['times'][-1]:.2f}",
|
| 183 |
)
|
| 184 |
|
| 185 |
def create_plots(self, hist_data, line_data):
|
|
|
|
| 390 |
color: #1f2937 !important;
|
| 391 |
margin-bottom: 1rem !important;
|
| 392 |
}
|
| 393 |
+
.metrics-row {
|
| 394 |
+
display: flex !important;
|
| 395 |
+
gap: 1rem !important;
|
| 396 |
+
margin-top: 0.5rem !important;
|
| 397 |
+
}
|
| 398 |
"""
|
| 399 |
|
| 400 |
def process_image(image, conf_thres, iou_thres):
|
| 401 |
if image is None:
|
| 402 |
+
return None, None, None, None, None, None
|
| 403 |
|
| 404 |
output_image, metrics = detector.detect(image, conf_thres, iou_thres)
|
| 405 |
|
|
|
|
| 428 |
),
|
| 429 |
hist_fig,
|
| 430 |
line_fig,
|
| 431 |
+
f"{metrics['avg_time']:.2f}",
|
| 432 |
+
f"{metrics['times'][-1]:.2f}",
|
| 433 |
)
|
| 434 |
|
| 435 |
def process_folder(files_path, conf_thres, iou_thres):
|
| 436 |
if not files_path:
|
| 437 |
+
return None, None, None, None, None, None
|
| 438 |
|
| 439 |
valid_extensions = [".jpg", ".jpeg", ".png"]
|
| 440 |
image_files = [
|
|
|
|
| 442 |
]
|
| 443 |
|
| 444 |
if not image_files:
|
| 445 |
+
return None, None, None, None, None, None
|
| 446 |
|
| 447 |
for img_file in image_files:
|
| 448 |
image = Image.open(img_file)
|
|
|
|
| 541 |
|
| 542 |
with gr.Column(scale=1):
|
| 543 |
line_plot = gr.Plot(label="Histórico de Tempos", container=True)
|
| 544 |
+
with gr.Row(elem_classes="metrics-row"):
|
| 545 |
+
avg_inference_time = gr.Textbox(
|
| 546 |
+
label="Tempo Médio de Inferência (ms)",
|
| 547 |
+
show_copy_button=True,
|
| 548 |
+
container=True,
|
| 549 |
+
)
|
| 550 |
+
last_inference_time = gr.Textbox(
|
| 551 |
+
label="Último Tempo de Inferência (ms)",
|
| 552 |
+
show_copy_button=True,
|
| 553 |
+
container=True,
|
| 554 |
+
)
|
| 555 |
|
| 556 |
with gr.Row(elem_classes="container"):
|
| 557 |
|
|
|
|
| 585 |
detect_single_btn.click(
|
| 586 |
fn=process_image,
|
| 587 |
inputs=[input_image, confidence_threshold, iou_threshold],
|
| 588 |
+
outputs=[
|
| 589 |
+
output_image,
|
| 590 |
+
total_inferences,
|
| 591 |
+
hist_plot,
|
| 592 |
+
line_plot,
|
| 593 |
+
avg_inference_time,
|
| 594 |
+
last_inference_time,
|
| 595 |
+
],
|
| 596 |
)
|
| 597 |
|
| 598 |
detect_folder_btn.click(
|
| 599 |
fn=process_folder,
|
| 600 |
inputs=[input_folder, confidence_threshold, iou_threshold],
|
| 601 |
+
outputs=[
|
| 602 |
+
output_image,
|
| 603 |
+
total_inferences,
|
| 604 |
+
hist_plot,
|
| 605 |
+
line_plot,
|
| 606 |
+
avg_inference_time,
|
| 607 |
+
last_inference_time,
|
| 608 |
+
],
|
| 609 |
)
|
| 610 |
|
| 611 |
# Carregar métricas iniciais ao carregar a página
|
| 612 |
iface.load(
|
| 613 |
fn=detector.load_initial_metrics,
|
| 614 |
inputs=None,
|
| 615 |
+
outputs=[
|
| 616 |
+
output_image,
|
| 617 |
+
total_inferences,
|
| 618 |
+
hist_plot,
|
| 619 |
+
line_plot,
|
| 620 |
+
avg_inference_time,
|
| 621 |
+
last_inference_time,
|
| 622 |
+
],
|
| 623 |
)
|
| 624 |
|
| 625 |
return iface
|