Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -299,7 +299,7 @@ class ForgeryDetector:
|
|
| 299 |
# Calculate IoU, Precision, Recall from the refined mask and probability map
|
| 300 |
if num_detections > 0:
|
| 301 |
# Use high-confidence areas from probability map as "predicted positive"
|
| 302 |
-
high_conf_mask = (
|
| 303 |
predicted_positive = np.sum(refined_mask > 0)
|
| 304 |
high_conf_positive = np.sum(high_conf_mask > 0)
|
| 305 |
|
|
@@ -412,18 +412,15 @@ class ForgeryDetector:
|
|
| 412 |
detector = ForgeryDetector()
|
| 413 |
|
| 414 |
|
| 415 |
-
def detect_forgery(file
|
| 416 |
-
"""Gradio interface function
|
| 417 |
try:
|
| 418 |
-
|
| 419 |
-
input_source = file if file is not None else webcam
|
| 420 |
-
|
| 421 |
-
if input_source is None:
|
| 422 |
empty_html = "<div style='padding:12px; border:1px solid #d9534f; border-radius:8px;'>❌ <b>No file uploaded.</b></div>"
|
| 423 |
return None, None, empty_html
|
| 424 |
|
| 425 |
# Get file path
|
| 426 |
-
file_path =
|
| 427 |
|
| 428 |
# Detect forgeries
|
| 429 |
overlay, metrics_gauge, results_html = detector.detect(file_path)
|
|
@@ -468,16 +465,10 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 468 |
with gr.Row():
|
| 469 |
with gr.Column(scale=1):
|
| 470 |
gr.Markdown("### Upload Document")
|
| 471 |
-
input_file = gr.
|
| 472 |
-
label="
|
| 473 |
-
file_types=["image", ".pdf"],
|
| 474 |
-
type="filepath"
|
| 475 |
-
)
|
| 476 |
-
input_webcam = gr.Image(
|
| 477 |
-
label="Or Capture from Webcam",
|
| 478 |
type="filepath",
|
| 479 |
-
sources=["webcam"]
|
| 480 |
-
visible=True
|
| 481 |
)
|
| 482 |
|
| 483 |
with gr.Row():
|
|
@@ -564,14 +555,14 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 564 |
# Event handlers
|
| 565 |
analyze_btn.click(
|
| 566 |
fn=detect_forgery,
|
| 567 |
-
inputs=[input_file
|
| 568 |
outputs=[output_image, metrics_gauge, output_html]
|
| 569 |
)
|
| 570 |
|
| 571 |
clear_btn.click(
|
| 572 |
-
fn=lambda: (None, None, None,
|
| 573 |
inputs=None,
|
| 574 |
-
outputs=[input_file,
|
| 575 |
)
|
| 576 |
|
| 577 |
|
|
|
|
| 299 |
# Calculate IoU, Precision, Recall from the refined mask and probability map
|
| 300 |
if num_detections > 0:
|
| 301 |
# Use high-confidence areas from probability map as "predicted positive"
|
| 302 |
+
high_conf_mask = (prob_map > 0.7).astype(np.uint8)
|
| 303 |
predicted_positive = np.sum(refined_mask > 0)
|
| 304 |
high_conf_positive = np.sum(high_conf_mask > 0)
|
| 305 |
|
|
|
|
| 412 |
detector = ForgeryDetector()
|
| 413 |
|
| 414 |
|
| 415 |
+
def detect_forgery(file):
|
| 416 |
+
"""Gradio interface function"""
|
| 417 |
try:
|
| 418 |
+
if file is None:
|
|
|
|
|
|
|
|
|
|
| 419 |
empty_html = "<div style='padding:12px; border:1px solid #d9534f; border-radius:8px;'>❌ <b>No file uploaded.</b></div>"
|
| 420 |
return None, None, empty_html
|
| 421 |
|
| 422 |
# Get file path
|
| 423 |
+
file_path = file if isinstance(file, str) else file
|
| 424 |
|
| 425 |
# Detect forgeries
|
| 426 |
overlay, metrics_gauge, results_html = detector.detect(file_path)
|
|
|
|
| 465 |
with gr.Row():
|
| 466 |
with gr.Column(scale=1):
|
| 467 |
gr.Markdown("### Upload Document")
|
| 468 |
+
input_file = gr.Image(
|
| 469 |
+
label="Document (Image or PDF)",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
type="filepath",
|
| 471 |
+
sources=["upload", "webcam"]
|
|
|
|
| 472 |
)
|
| 473 |
|
| 474 |
with gr.Row():
|
|
|
|
| 555 |
# Event handlers
|
| 556 |
analyze_btn.click(
|
| 557 |
fn=detect_forgery,
|
| 558 |
+
inputs=[input_file],
|
| 559 |
outputs=[output_image, metrics_gauge, output_html]
|
| 560 |
)
|
| 561 |
|
| 562 |
clear_btn.click(
|
| 563 |
+
fn=lambda: (None, None, None, "<i>No analysis yet. Upload a document and click Analyze.</i>"),
|
| 564 |
inputs=None,
|
| 565 |
+
outputs=[input_file, output_image, metrics_gauge, output_html]
|
| 566 |
)
|
| 567 |
|
| 568 |
|