Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,8 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
import cv2
|
| 5 |
-
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 8 |
|
| 9 |
-
# Download cascade classifier if not present.
|
| 10 |
-
cascade_filename = "haarcascade_frontalface_default.xml"
|
| 11 |
-
if not os.path.exists(cascade_filename):
|
| 12 |
-
os.system(
|
| 13 |
-
"wget -O "
|
| 14 |
-
f"{cascade_filename} "
|
| 15 |
-
"https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml"
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
# Load OpenCV's Haar Cascade face detector.
|
| 19 |
-
face_cascade = cv2.CascadeClassifier(cascade_filename)
|
| 20 |
-
|
| 21 |
# Load the Hugging Face model and processor for deepfake detection.
|
| 22 |
processor = AutoImageProcessor.from_pretrained("Smogy/SMOGY-Ai-images-detector")
|
| 23 |
model = AutoModelForImageClassification.from_pretrained("Smogy/SMOGY-Ai-images-detector")
|
|
@@ -32,51 +17,13 @@ def detect_deepfake(image: Image.Image) -> str:
|
|
| 32 |
conf = probs[0, idx].item()
|
| 33 |
return f"The image is {label} with confidence {conf:.2f}"
|
| 34 |
|
| 35 |
-
def swap_faces(source: Image.Image, target: Image.Image) -> Image.Image:
|
| 36 |
-
# Convert to OpenCV BGR
|
| 37 |
-
src = cv2.cvtColor(np.array(source), cv2.COLOR_RGB2BGR)
|
| 38 |
-
tgt = cv2.cvtColor(np.array(target), cv2.COLOR_RGB2BGR)
|
| 39 |
-
|
| 40 |
-
# Convert to grayscale for detection
|
| 41 |
-
gray_src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
| 42 |
-
gray_tgt = cv2.cvtColor(tgt, cv2.COLOR_BGR2GRAY)
|
| 43 |
-
|
| 44 |
-
# Detect faces (x, y, w, h)
|
| 45 |
-
src_faces = face_cascade.detectMultiScale(gray_src, scaleFactor=1.1, minNeighbors=5)
|
| 46 |
-
tgt_faces = face_cascade.detectMultiScale(gray_tgt, scaleFactor=1.1, minNeighbors=5)
|
| 47 |
-
|
| 48 |
-
if len(src_faces) == 0 or len(tgt_faces) == 0:
|
| 49 |
-
return None
|
| 50 |
-
|
| 51 |
-
# Take the first detected face
|
| 52 |
-
x_s, y_s, w_s, h_s = src_faces[0]
|
| 53 |
-
x_t, y_t, w_t, h_t = tgt_faces[0]
|
| 54 |
-
|
| 55 |
-
# Extract and resize
|
| 56 |
-
face_src = src[y_s:y_s+h_s, x_s:x_s+w_s]
|
| 57 |
-
face_resized = cv2.resize(face_src, (w_t, h_t))
|
| 58 |
-
|
| 59 |
-
# Swap
|
| 60 |
-
tgt[y_t:y_t+h_t, x_t:x_t+w_t] = face_resized
|
| 61 |
-
|
| 62 |
-
# Convert back to PIL
|
| 63 |
-
return Image.fromarray(cv2.cvtColor(tgt, cv2.COLOR_BGR2RGB))
|
| 64 |
-
|
| 65 |
# Build Gradio interface
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
-
gr.Markdown("# Deepfake Detection
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
with gr.Tab("Face Swap"):
|
| 76 |
-
gr.Markdown("### Upload source and target images for face swap.")
|
| 77 |
-
src_img = gr.Image(type="pil", label="Source Face")
|
| 78 |
-
tgt_img = gr.Image(type="pil", label="Target Image")
|
| 79 |
-
out_img = gr.Image(type="pil", label="Swapped Image")
|
| 80 |
-
gr.Button("Swap Faces").click(fn=swap_faces, inputs=[src_img, tgt_img], outputs=out_img)
|
| 81 |
-
|
| 82 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load the Hugging Face model and processor for deepfake detection.
|
| 7 |
processor = AutoImageProcessor.from_pretrained("Smogy/SMOGY-Ai-images-detector")
|
| 8 |
model = AutoModelForImageClassification.from_pretrained("Smogy/SMOGY-Ai-images-detector")
|
|
|
|
| 17 |
conf = probs[0, idx].item()
|
| 18 |
return f"The image is {label} with confidence {conf:.2f}"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Build Gradio interface
|
| 21 |
with gr.Blocks() as demo:
|
| 22 |
+
gr.Markdown("# Deepfake Detection App")
|
| 23 |
+
gr.Markdown("### Upload an image to detect deepfake content.")
|
| 24 |
+
img_in = gr.Image(type="pil", label="Upload Image")
|
| 25 |
+
txt_out = gr.Textbox(label="Result")
|
| 26 |
+
gr.Button("Detect").click(fn=detect_deepfake, inputs=img_in, outputs=txt_out)
|
| 27 |
+
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|