Spaces:
Runtime error
Runtime error
Commit
·
b9ac4ff
1
Parent(s):
9c3f062
V0.1
Browse files
app.py
CHANGED
|
@@ -37,7 +37,7 @@ except ImportError:
|
|
| 37 |
|
| 38 |
# Define model paths
|
| 39 |
DEFAULT_DAMAGE_MODEL_PATH = "./model_final.pth"
|
| 40 |
-
DEFAULT_DEEPFAKE_MODEL_PATH = "./
|
| 41 |
|
| 42 |
# Sample images for demo (add your own paths)
|
| 43 |
SAMPLE_IMAGES = [
|
|
@@ -45,6 +45,9 @@ SAMPLE_IMAGES = [
|
|
| 45 |
"./test5.png",
|
| 46 |
]
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
def verify_detectron2_installation():
|
| 49 |
"""Verify that Detectron2 is properly installed"""
|
| 50 |
results = {
|
|
@@ -384,9 +387,13 @@ def visualize_results(image, damage_outputs, deepfake_results, damage_threshold)
|
|
| 384 |
return np.array(image, dtype=np.uint8)
|
| 385 |
|
| 386 |
def process_image(input_image, damage_model_path, deepfake_model_path, damage_threshold,
|
| 387 |
-
deepfake_threshold, skip_damage, device_str):
|
| 388 |
"""Process an image through the detection pipeline"""
|
|
|
|
|
|
|
|
|
|
| 389 |
progress_info = []
|
|
|
|
| 390 |
|
| 391 |
# Check model files
|
| 392 |
if not skip_damage and damage_model_path:
|
|
@@ -407,12 +414,12 @@ def process_image(input_image, damage_model_path, deepfake_model_path, damage_th
|
|
| 407 |
if len(img.shape) == 3 and img.shape[2] == 3:
|
| 408 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 409 |
else:
|
| 410 |
-
return None, "Error: Unsupported image format"
|
| 411 |
|
| 412 |
if img is None:
|
| 413 |
-
return None, "Error: Could not read the image"
|
| 414 |
except Exception as e:
|
| 415 |
-
return None, f"Error loading image: {str(e)}"
|
| 416 |
|
| 417 |
# Setup device
|
| 418 |
device = setup_device(device_str)
|
|
@@ -492,7 +499,23 @@ def process_image(input_image, damage_model_path, deepfake_model_path, damage_th
|
|
| 492 |
if len(result_img.shape) == 3 and result_img.shape[2] == 3:
|
| 493 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
|
| 494 |
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
|
| 497 |
def auto_install_dependencies():
|
| 498 |
"""Attempt to install dependencies if needed"""
|
|
@@ -547,6 +570,9 @@ def create_gradio_interface():
|
|
| 547 |
secondary_hue="orange",
|
| 548 |
)
|
| 549 |
|
|
|
|
|
|
|
|
|
|
| 550 |
with gr.Blocks(title="Car Damage & Deepfake Detector", theme=theme) as app:
|
| 551 |
gr.Markdown("""
|
| 552 |
# 🚗 Car Damage Fraud Detector with Vision Transformer
|
|
@@ -556,6 +582,8 @@ def create_gradio_interface():
|
|
| 556 |
2. Verify if the damage is real or artificially generated (deepfake)
|
| 557 |
|
| 558 |
*This app uses a Vision Transformer (ViT) model for deepfake detection.*
|
|
|
|
|
|
|
| 559 |
""")
|
| 560 |
|
| 561 |
# Main Interface Tab
|
|
@@ -568,6 +596,9 @@ def create_gradio_interface():
|
|
| 568 |
process_btn = gr.Button("Analyze Image", variant="primary")
|
| 569 |
clear_btn = gr.Button("Clear", variant="secondary")
|
| 570 |
|
|
|
|
|
|
|
|
|
|
| 571 |
with gr.Accordion("Advanced Settings", open=False):
|
| 572 |
skip_damage = gr.Checkbox(label="Skip Damage Detection", value=False)
|
| 573 |
damage_threshold = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.05,
|
|
|
|
| 37 |
|
| 38 |
# Define model paths
|
| 39 |
DEFAULT_DAMAGE_MODEL_PATH = "./model_final.pth"
|
| 40 |
+
DEFAULT_DEEPFAKE_MODEL_PATH = "./vit_deepfake_best_20250422_222444.pth"
|
| 41 |
|
| 42 |
# Sample images for demo (add your own paths)
|
| 43 |
SAMPLE_IMAGES = [
|
|
|
|
| 45 |
"./test5.png",
|
| 46 |
]
|
| 47 |
|
| 48 |
+
# Maximum number of tries allowed
|
| 49 |
+
MAX_TRIES = 3
|
| 50 |
+
|
| 51 |
def verify_detectron2_installation():
|
| 52 |
"""Verify that Detectron2 is properly installed"""
|
| 53 |
results = {
|
|
|
|
| 387 |
return np.array(image, dtype=np.uint8)
|
| 388 |
|
| 389 |
def process_image(input_image, damage_model_path, deepfake_model_path, damage_threshold,
|
| 390 |
+
deepfake_threshold, skip_damage, device_str, usage_count):
|
| 391 |
"""Process an image through the detection pipeline"""
|
| 392 |
+
# Increment usage count and check if limit reached
|
| 393 |
+
usage_count = usage_count + 1
|
| 394 |
+
|
| 395 |
progress_info = []
|
| 396 |
+
progress_info.append(f"Usage: {usage_count}/{MAX_TRIES}")
|
| 397 |
|
| 398 |
# Check model files
|
| 399 |
if not skip_damage and damage_model_path:
|
|
|
|
| 414 |
if len(img.shape) == 3 and img.shape[2] == 3:
|
| 415 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 416 |
else:
|
| 417 |
+
return None, "Error: Unsupported image format", usage_count
|
| 418 |
|
| 419 |
if img is None:
|
| 420 |
+
return None, "Error: Could not read the image", usage_count
|
| 421 |
except Exception as e:
|
| 422 |
+
return None, f"Error loading image: {str(e)}", usage_count
|
| 423 |
|
| 424 |
# Setup device
|
| 425 |
device = setup_device(device_str)
|
|
|
|
| 499 |
if len(result_img.shape) == 3 and result_img.shape[2] == 3:
|
| 500 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
|
| 501 |
|
| 502 |
+
# Add usage information at the bottom of the image
|
| 503 |
+
if usage_count >= MAX_TRIES:
|
| 504 |
+
# Add a "Usage limit reached" message to the bottom of the image in red
|
| 505 |
+
cv2.putText(result_img, f"USAGE LIMIT REACHED: {usage_count}/{MAX_TRIES}",
|
| 506 |
+
(10, result_img.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
| 507 |
+
else:
|
| 508 |
+
# Add a usage counter to the bottom of the image
|
| 509 |
+
cv2.putText(result_img, f"Usage: {usage_count}/{MAX_TRIES}",
|
| 510 |
+
(10, result_img.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 511 |
+
|
| 512 |
+
# Add usage info to the progress text
|
| 513 |
+
if usage_count >= MAX_TRIES:
|
| 514 |
+
progress_info.append("\n⚠️ You have reached the maximum number of tries allowed ⚠️")
|
| 515 |
+
else:
|
| 516 |
+
progress_info.append(f"\nRemaining tries: {MAX_TRIES - usage_count}")
|
| 517 |
+
|
| 518 |
+
return result_img, "\n".join(progress_info), usage_count
|
| 519 |
|
| 520 |
def auto_install_dependencies():
|
| 521 |
"""Attempt to install dependencies if needed"""
|
|
|
|
| 570 |
secondary_hue="orange",
|
| 571 |
)
|
| 572 |
|
| 573 |
+
# Initialize usage counter
|
| 574 |
+
usage_counter = gr.State(0)
|
| 575 |
+
|
| 576 |
with gr.Blocks(title="Car Damage & Deepfake Detector", theme=theme) as app:
|
| 577 |
gr.Markdown("""
|
| 578 |
# 🚗 Car Damage Fraud Detector with Vision Transformer
|
|
|
|
| 582 |
2. Verify if the damage is real or artificially generated (deepfake)
|
| 583 |
|
| 584 |
*This app uses a Vision Transformer (ViT) model for deepfake detection.*
|
| 585 |
+
|
| 586 |
+
⚠️ **Note: You have a maximum of 3 tries to analyze images.**
|
| 587 |
""")
|
| 588 |
|
| 589 |
# Main Interface Tab
|
|
|
|
| 596 |
process_btn = gr.Button("Analyze Image", variant="primary")
|
| 597 |
clear_btn = gr.Button("Clear", variant="secondary")
|
| 598 |
|
| 599 |
+
# Usage limit display
|
| 600 |
+
usage_display = gr.Markdown("**Usage: 0/3**")
|
| 601 |
+
|
| 602 |
with gr.Accordion("Advanced Settings", open=False):
|
| 603 |
skip_damage = gr.Checkbox(label="Skip Damage Detection", value=False)
|
| 604 |
damage_threshold = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.05,
|