Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import cv2
|
| 4 |
+
import tempfile
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 8 |
+
|
| 9 |
+
# Load the deepfake detection model
|
| 10 |
+
processor = AutoImageProcessor.from_pretrained("Smogy/SMOGY-Ai-images-detector")
|
| 11 |
+
model = AutoModelForImageClassification.from_pretrained("Smogy/SMOGY-Ai-images-detector")
|
| 12 |
+
|
| 13 |
+
# Load face detection cascade
|
| 14 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 15 |
+
|
| 16 |
+
def detect_deepfake_image(image: Image.Image) -> str:
|
| 17 |
+
"""Detect deepfake in a single image"""
|
| 18 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model(**inputs)
|
| 21 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
| 22 |
+
idx = probs.argmax().item()
|
| 23 |
+
label = model.config.id2label[idx]
|
| 24 |
+
conf = probs[0, idx].item()
|
| 25 |
+
return f"The image is {label} with confidence {conf:.2f}"
|
| 26 |
+
|
| 27 |
+
def process_video(video_path: str) -> str:
|
| 28 |
+
"""Process video frame by frame and add detection annotations"""
|
| 29 |
+
# Open input video
|
| 30 |
+
cap = cv2.VideoCapture(video_path)
|
| 31 |
+
if not cap.isOpened():
|
| 32 |
+
raise ValueError("Could not open video file")
|
| 33 |
+
|
| 34 |
+
# Get video properties
|
| 35 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 36 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 37 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 38 |
+
|
| 39 |
+
# Create temporary output file
|
| 40 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file:
|
| 41 |
+
output_path = temp_file.name
|
| 42 |
+
|
| 43 |
+
# Initialize video writer
|
| 44 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 45 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 46 |
+
|
| 47 |
+
# Process each frame
|
| 48 |
+
while cap.isOpened():
|
| 49 |
+
ret, frame = cap.read()
|
| 50 |
+
if not ret:
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
# Convert to grayscale for face detection
|
| 54 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 55 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
| 56 |
+
|
| 57 |
+
# Process each detected face
|
| 58 |
+
for (x, y, w, h) in faces:
|
| 59 |
+
# Extract face ROI
|
| 60 |
+
face_img = frame[y:y+h, x:x+w]
|
| 61 |
+
|
| 62 |
+
# Convert to PIL Image and process
|
| 63 |
+
face_pil = Image.fromarray(cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB))
|
| 64 |
+
inputs = processor(images=face_pil, return_tensors="pt")
|
| 65 |
+
|
| 66 |
+
with torch.no_grad():
|
| 67 |
+
outputs = model(**inputs)
|
| 68 |
+
|
| 69 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
| 70 |
+
idx = probs.argmax().item()
|
| 71 |
+
label = model.config.id2label[idx]
|
| 72 |
+
conf = probs[0, idx].item()
|
| 73 |
+
|
| 74 |
+
# Draw bounding box and label
|
| 75 |
+
color = (0, 255, 0) if label == 'real' else (0, 0, 255) # BGR format
|
| 76 |
+
cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
|
| 77 |
+
cv2.putText(frame, f"{label} {conf:.2f}",
|
| 78 |
+
(x, y-10), cv2.FONT_HERSHEY_SIMPLEX,
|
| 79 |
+
0.7, color, 2, cv2.LINE_AA)
|
| 80 |
+
|
| 81 |
+
# Write processed frame
|
| 82 |
+
out.write(frame)
|
| 83 |
+
|
| 84 |
+
# Release resources
|
| 85 |
+
cap.release()
|
| 86 |
+
out.release()
|
| 87 |
+
|
| 88 |
+
return output_path
|
| 89 |
+
|
| 90 |
+
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown("# Deepfake Detection Suite")
|
| 92 |
+
|
| 93 |
+
with gr.Tab("Image Detection"):
|
| 94 |
+
gr.Markdown("## Image Deepfake Detection")
|
| 95 |
+
img_input = gr.Image(type="pil", label="Input Image")
|
| 96 |
+
img_output = gr.Textbox(label="Detection Result")
|
| 97 |
+
img_button = gr.Button("Analyze Image")
|
| 98 |
+
|
| 99 |
+
with gr.Tab("Video Detection"):
|
| 100 |
+
gr.Markdown("## Video Deepfake Detection")
|
| 101 |
+
vid_input = gr.Video(label="Input Video")
|
| 102 |
+
vid_output = gr.Video(label="Processed Video")
|
| 103 |
+
vid_button = gr.Button("Analyze Video")
|
| 104 |
+
|
| 105 |
+
img_button.click(fn=detect_deepfake_image, inputs=img_input, outputs=img_output)
|
| 106 |
+
vid_button.click(fn=process_video, inputs=vid_input, outputs=vid_output)
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
demo.launch()
|