Hardik commited on
Commit
a6b0e12
·
1 Parent(s): 89c8d8a

first commit

Browse files
Files changed (3) hide show
  1. .gitignore +4 -0
  2. app.py +93 -0
  3. requirements.txt +7 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ __pycache__/
2
+ *.mp4
3
+ *.pkl
4
+ *.log
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import torch
4
+ import dlib
5
+ import numpy as np
6
+ from imutils import face_utils
7
+ from torchvision import models, transforms
8
+ from tempfile import NamedTemporaryFile
9
+
10
+ # Load face detector and landmark predictor
11
+ face_detector = dlib.get_frontal_face_detector()
12
+ PREDICTOR_PATH = "./lib/shape_predictor_81_face_landmarks.dat"
13
+ face_predictor = dlib.shape_predictor(PREDICTOR_PATH)
14
+
15
+ # Load deepfake detection model
16
+ model = models.resnet34()
17
+ model.fc = torch.nn.Linear(model.fc.in_features, 2)
18
+ ckpt_path = "./resnet34.pkl"
19
+ model.load_state_dict(torch.load(ckpt_path, map_location="cpu"))
20
+ model.eval()
21
+
22
+ # Define transformation for face images
23
+ transform = transforms.Compose([
24
+ transforms.ToPILImage(),
25
+ transforms.Resize((224, 224)),
26
+ transforms.ToTensor(),
27
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
28
+ ])
29
+
30
+ def process_video(video_path: str):
31
+ cap = cv2.VideoCapture(video_path)
32
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
33
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
34
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
35
+
36
+ output_path = video_path.replace(".mp4", "_processed.mp4")
37
+ output_video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
38
+
39
+ while cap.isOpened():
40
+ ret, frame = cap.read()
41
+ if not ret:
42
+ break
43
+
44
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
45
+ faces = face_detector(rgb_frame, 1)
46
+
47
+ for face in faces:
48
+ landmarks = face_utils.shape_to_np(face_predictor(rgb_frame, face))
49
+ x_min, y_min = np.min(landmarks, axis=0)
50
+ x_max, y_max = np.max(landmarks, axis=0)
51
+
52
+ face_crop = rgb_frame[y_min:y_max, x_min:x_max]
53
+ if face_crop.size == 0:
54
+ continue
55
+
56
+ face_tensor = transform(face_crop).unsqueeze(0)
57
+ with torch.no_grad():
58
+ output = torch.softmax(model(face_tensor), dim=1)
59
+ fake_confidence = output[0, 1].item() * 100 # Fake confidence as a percentage
60
+ label = "Fake" if fake_confidence > 50 else "Real"
61
+ color = (0, 0, 255) if label == "Fake" else (0, 255, 0)
62
+
63
+ # Annotating confidence score with label
64
+ label_text = f"{label} ({fake_confidence:.2f}%)"
65
+
66
+ cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
67
+ cv2.putText(frame, label_text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
68
+
69
+ output_video.write(frame)
70
+
71
+ cap.release()
72
+ output_video.release()
73
+ return output_path
74
+
75
+ def gradio_interface(video_file):
76
+ with NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
77
+ temp_file.write(video_file.read())
78
+ temp_path = temp_file.name
79
+
80
+ output_path = process_video(temp_path)
81
+ return output_path
82
+
83
+ # Gradio UI
84
+ iface = gr.Interface(
85
+ fn=gradio_interface,
86
+ inputs=gr.Video(label="Upload Video"),
87
+ outputs=gr.Video(label="Processed Video"),
88
+ title="Deepfake Detection",
89
+ description="Upload a video to detect deepfakes. The model will process faces and classify them as real or fake."
90
+ )
91
+
92
+ if __name__ == "__main__":
93
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ torchvision
4
+ dlib
5
+ opencv-python
6
+ numpy
7
+ imutils