Spaces:
Sleeping
Sleeping
File size: 2,856 Bytes
f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f 07b113f f158d4f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import gradio as gr
import numpy as np
from PIL import Image
import cv2
# -----------------------------
# STEP 1: FACE DETECTION + ROUTING (VERSION 2 - FOUNDATION)
# -----------------------------
# This version does NOT yet classify deepfakes.
# It ONLY decides:
# 1) Does the image contain a human face?
# 2) Route it to the correct analysis pipeline
# -----------------------------
# Load OpenCV Haar Cascade (lightweight & HF-friendly)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
def detect_faces(image_np):
"""
Detect faces using OpenCV Haar Cascade
Returns number of faces detected
"""
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(60, 60)
)
return faces
def route_image(image):
if image is None:
return "No image provided", "N/A"
image_np = np.array(image)
faces = detect_faces(image_np)
# ROUTING LOGIC
if len(faces) > 0:
route = "π§ Face Detected"
explanation = (
"This image contains one or more human faces.\n"
"β It will be analyzed using **deepfake face-detection models**\n"
"(XceptionNet / EfficientNet in next step)."
)
else:
route = "πΌοΈ No Face Detected"
explanation = (
"No human face detected in this image.\n"
"β It will be analyzed using **generic AI-image detection models**\n"
"(GAN / diffusion detection)."
)
return route, explanation
# -----------------------------
# GRADIO UI
# -----------------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π§ Image Analysis Router (Version 2 β Step 1)
This step determines **how the image should be analyzed**:
- π€ Face present β Deepfake detection pipeline
- πΌοΈ No face β Generic AI-image detection pipeline
*(No deepfake classification is performed yet.)*
""")
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Upload Any Image")
analyze_btn = gr.Button("Analyze Image π")
with gr.Column():
route_output = gr.Textbox(label="Routing Decision")
explanation_output = gr.Textbox(label="Explanation", lines=5)
analyze_btn.click(
fn=route_image,
inputs=image_input,
outputs=[route_output, explanation_output]
)
gr.Markdown("""
### β
Why this step matters
- Prevents misuse of face-only deepfake models
- Reduces false positives
- Makes the system work for **ANY image**
π **Next step:** Integrate XceptionNet / EfficientNet classifiers.
""")
# Launch
demo.launch()
|