delete api
Browse files
app.py
CHANGED
|
@@ -250,6 +250,9 @@
|
|
| 250 |
# if __name__ == "__main__":
|
| 251 |
# demo.launch()
|
| 252 |
|
|
|
|
|
|
|
|
|
|
| 253 |
import gradio as gr
|
| 254 |
import cv2
|
| 255 |
import numpy as np
|
|
@@ -571,16 +574,18 @@ class EnginePartDetector:
|
|
| 571 |
|
| 572 |
# Layer 3: Latent Space Matching (Cosine Similarity to centroids)
|
| 573 |
class_scores = []
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
|
|
|
|
|
|
| 584 |
status = f"β
CLASSIFIED AS: {best_class}" if matched else "β UNCERTAIN (below threshold)"
|
| 585 |
|
| 586 |
lines = [
|
|
@@ -626,6 +631,17 @@ class EnginePartDetector:
|
|
| 626 |
body.append(f" β’ {name}: {len(vectors)} samples")
|
| 627 |
return f"{header}\n" + "\n".join(body)
|
| 628 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 630 |
# Gradio Application
|
| 631 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -641,6 +657,12 @@ def add_sample(image, class_name):
|
|
| 641 |
def list_classes():
|
| 642 |
return detector.list_templates()
|
| 643 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
# Custom CSS for premium look
|
| 645 |
custom_css = """
|
| 646 |
.container { max-width: 1200px; margin: auto; }
|
|
@@ -678,6 +700,15 @@ with gr.Blocks(title="Engine Part CV System") as demo:
|
|
| 678 |
api_name="detect_part",
|
| 679 |
)
|
| 680 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
with gr.Tab("πΎ Train Latent Space"):
|
| 682 |
with gr.Row():
|
| 683 |
with gr.Column(scale=1):
|
|
|
|
| 250 |
# if __name__ == "__main__":
|
| 251 |
# demo.launch()
|
| 252 |
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
|
| 256 |
import gradio as gr
|
| 257 |
import cv2
|
| 258 |
import numpy as np
|
|
|
|
| 574 |
|
| 575 |
# Layer 3: Latent Space Matching (Cosine Similarity to centroids)
|
| 576 |
class_scores = []
|
| 577 |
+
if self.classes:
|
| 578 |
+
for name, vectors in self.classes.items():
|
| 579 |
+
# Calculate centroid (neighborhood center)
|
| 580 |
+
centroid = np.mean(vectors, axis=0)
|
| 581 |
+
sim = self._cosine(query_feat, centroid)
|
| 582 |
+
class_scores.append((name, sim))
|
| 583 |
+
|
| 584 |
+
class_scores.sort(key=lambda x: x[1], reverse=True)
|
| 585 |
+
best_class, best_score = class_scores[0]
|
| 586 |
+
matched = best_score >= threshold
|
| 587 |
+
else:
|
| 588 |
+
best_class, best_score, matched = "UNCONFIGURED", 0.0, False
|
| 589 |
status = f"β
CLASSIFIED AS: {best_class}" if matched else "β UNCERTAIN (below threshold)"
|
| 590 |
|
| 591 |
lines = [
|
|
|
|
| 631 |
body.append(f" β’ {name}: {len(vectors)} samples")
|
| 632 |
return f"{header}\n" + "\n".join(body)
|
| 633 |
|
| 634 |
+
def delete_class(self, class_name: str) -> bool:
|
| 635 |
+
"""Remove a class and all its samples from the detector."""
|
| 636 |
+
if class_name in self.classes:
|
| 637 |
+
del self.classes[class_name]
|
| 638 |
+
if class_name in self.class_rois:
|
| 639 |
+
del self.class_rois[class_name]
|
| 640 |
+
self._persist_data()
|
| 641 |
+
logger.info(f"Class '{class_name}' deleted from detector.")
|
| 642 |
+
return True
|
| 643 |
+
return False
|
| 644 |
+
|
| 645 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 646 |
# Gradio Application
|
| 647 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 657 |
def list_classes():
|
| 658 |
return detector.list_templates()
|
| 659 |
|
| 660 |
+
def delete_class_api(class_name: str):
|
| 661 |
+
success = detector.delete_class(class_name)
|
| 662 |
+
if success:
|
| 663 |
+
return f"β
Class '{class_name}' deleted successfully.", None
|
| 664 |
+
return f"β Class '{class_name}' not found.", None
|
| 665 |
+
|
| 666 |
# Custom CSS for premium look
|
| 667 |
custom_css = """
|
| 668 |
.container { max-width: 1200px; margin: auto; }
|
|
|
|
| 700 |
api_name="detect_part",
|
| 701 |
)
|
| 702 |
|
| 703 |
+
# Deletion API (Hidden from UI but accessible via API)
|
| 704 |
+
delete_btn_api = gr.Button("Delete Class (Internal)", visible=False)
|
| 705 |
+
delete_btn_api.click(
|
| 706 |
+
fn=delete_class_api,
|
| 707 |
+
inputs=[gr.Textbox(visible=False)], # Dummy input if needed, though often just call via API
|
| 708 |
+
outputs=[gr.Markdown()],
|
| 709 |
+
api_name="delete_class"
|
| 710 |
+
)
|
| 711 |
+
|
| 712 |
with gr.Tab("πΎ Train Latent Space"):
|
| 713 |
with gr.Row():
|
| 714 |
with gr.Column(scale=1):
|