Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
import tensorflow_addons
|
6 |
+
|
7 |
+
from facenet_pytorch import MTCNN
|
8 |
+
from PIL import Image
|
9 |
+
import moviepy.editor as mp
|
10 |
+
|
11 |
+
|
12 |
+
local_zip = "FINAL-EFFICIENTNETV2-B0.zip"
|
13 |
+
zip_ref = zipfile.ZipFile(local_zip, 'r')
|
14 |
+
zip_ref.extractall('FINAL-EFFICIENTNETV2-B0')
|
15 |
+
zip_ref.close()
|
16 |
+
|
17 |
+
# Load face detector
|
18 |
+
mtcnn = MTCNN(margin=14, keep_all=True, factor=0.7, device='cpu')
|
19 |
+
|
20 |
+
class DetectionPipeline:
|
21 |
+
"""Pipeline class for detecting faces in the frames of a video file."""
|
22 |
+
|
23 |
+
def __init__(self, detector, n_frames=None, batch_size=60, resize=None):
|
24 |
+
"""Constructor for DetectionPipeline class.
|
25 |
+
|
26 |
+
Keyword Arguments:
|
27 |
+
n_frames {int} -- Total number of frames to load. These will be evenly spaced
|
28 |
+
throughout the video. If not specified (i.e., None), all frames will be loaded.
|
29 |
+
(default: {None})
|
30 |
+
batch_size {int} -- Batch size to use with MTCNN face detector. (default: {32})
|
31 |
+
resize {float} -- Fraction by which to resize frames from original prior to face
|
32 |
+
detection. A value less than 1 results in downsampling and a value greater than
|
33 |
+
1 result in upsampling. (default: {None})
|
34 |
+
"""
|
35 |
+
self.detector = detector
|
36 |
+
self.n_frames = n_frames
|
37 |
+
self.batch_size = batch_size
|
38 |
+
self.resize = resize
|
39 |
+
|
40 |
+
def __call__(self, filename):
|
41 |
+
"""Load frames from an MP4 video and detect faces.
|
42 |
+
|
43 |
+
Arguments:
|
44 |
+
filename {str} -- Path to video.
|
45 |
+
"""
|
46 |
+
# Create video reader and find length
|
47 |
+
v_cap = cv2.VideoCapture(filename)
|
48 |
+
v_len = int(v_cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
49 |
+
|
50 |
+
# Pick 'n_frames' evenly spaced frames to sample
|
51 |
+
if self.n_frames is None:
|
52 |
+
sample = np.arange(0, v_len)
|
53 |
+
else:
|
54 |
+
sample = np.linspace(0, v_len - 1, self.n_frames).astype(int)
|
55 |
+
|
56 |
+
# Loop through frames
|
57 |
+
faces = []
|
58 |
+
frames = []
|
59 |
+
for j in range(v_len):
|
60 |
+
success = v_cap.grab()
|
61 |
+
if j in sample:
|
62 |
+
# Load frame
|
63 |
+
success, frame = v_cap.retrieve()
|
64 |
+
if not success:
|
65 |
+
continue
|
66 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
67 |
+
# frame = Image.fromarray(frame)
|
68 |
+
|
69 |
+
# Resize frame to desired size
|
70 |
+
if self.resize is not None:
|
71 |
+
frame = frame.resize([int(d * self.resize) for d in frame.size])
|
72 |
+
frames.append(frame)
|
73 |
+
|
74 |
+
# When batch is full, detect faces and reset frame list
|
75 |
+
if len(frames) % self.batch_size == 0 or j == sample[-1]:
|
76 |
+
|
77 |
+
boxes, probs = self.detector.detect(frames)
|
78 |
+
|
79 |
+
for i in range(len(frames)):
|
80 |
+
|
81 |
+
if boxes[i] is None:
|
82 |
+
faces.append(face2) #append previous face frame if no face is detected
|
83 |
+
continue
|
84 |
+
|
85 |
+
box = boxes[i][0].astype(int)
|
86 |
+
frame = frames[i]
|
87 |
+
face = frame[box[1]:box[3], box[0]:box[2]]
|
88 |
+
|
89 |
+
if not face.any():
|
90 |
+
faces.append(face2) #append previous face frame if no face is detected
|
91 |
+
continue
|
92 |
+
|
93 |
+
face2 = cv2.resize(face, (224, 224))
|
94 |
+
|
95 |
+
faces.append(face2)
|
96 |
+
|
97 |
+
frames = []
|
98 |
+
|
99 |
+
v_cap.release()
|
100 |
+
|
101 |
+
return faces
|
102 |
+
|
103 |
+
|
104 |
+
detection_pipeline = DetectionPipeline(detector=mtcnn,n_frames=20, batch_size=60)
|
105 |
+
|
106 |
+
model = tf.keras.models.load_model("FINAL-EFFICIENTNETV2-B0")
|
107 |
+
|
108 |
+
|
109 |
+
def deepfakespredict(input_video):
|
110 |
+
|
111 |
+
faces = detection_pipeline(input_video)
|
112 |
+
|
113 |
+
total = 0
|
114 |
+
real = 0
|
115 |
+
fake = 0
|
116 |
+
|
117 |
+
for face in faces:
|
118 |
+
|
119 |
+
face2 = face/255
|
120 |
+
pred = model.predict(np.expand_dims(face2, axis=0))[0]
|
121 |
+
total+=1
|
122 |
+
|
123 |
+
pred2 = pred[1]
|
124 |
+
|
125 |
+
if pred2 > 0.5:
|
126 |
+
fake+=1
|
127 |
+
else:
|
128 |
+
real+=1
|
129 |
+
|
130 |
+
fake_ratio = fake/total
|
131 |
+
|
132 |
+
text =""
|
133 |
+
text2 = "Deepfakes Confidence: " + str(fake_ratio)
|
134 |
+
|
135 |
+
if fake_ratio >= 0.5:
|
136 |
+
text = "The video is FAKE."
|
137 |
+
else:
|
138 |
+
text = "The video is REAL."
|
139 |
+
|
140 |
+
face_frames = []
|
141 |
+
|
142 |
+
for face in faces:
|
143 |
+
face_frame = Image.fromarray(face.astype('uint8'), 'RGB')
|
144 |
+
face_frames.append(face_frame)
|
145 |
+
|
146 |
+
face_frames[0].save('results.gif', save_all=True, append_images=face_frames[1:], duration = 250, loop = 100 )
|
147 |
+
clip = mp.VideoFileClip("results.gif")
|
148 |
+
clip.write_videofile("myvideo.mp4")
|
149 |
+
|
150 |
+
return text, text2, "myvideo.mp4"
|
151 |
+
|
152 |
+
|
153 |
+
title="EfficientNetV2 Deepfakes Video Detector"
|
154 |
+
description="This is a demo implementation of Deepfakes Video Detector by using EfficientNetV2 on frame-by-frame detection. To use it, simply upload your video, or click one of the examples to load them."
|
155 |
+
|
156 |
+
demo = gr.Interface(deepfakespredict,
|
157 |
+
inputs = ["video"],
|
158 |
+
outputs=["text","text", gr.outputs.Video(label="Detected face sequence")],
|
159 |
+
title=title,
|
160 |
+
description=description
|
161 |
+
)
|
162 |
+
demo.launch()
|