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