Commit
·
76b6c6d
1
Parent(s):
936e149
Upload 19 files
Browse files- .gitattributes +6 -0
- FINAL-EFFICIENTNETV2-B0.zip +3 -0
- FINAL-EFFICIENTNETV2-B0/keras_metadata.pb +3 -0
- FINAL-EFFICIENTNETV2-B0/saved_model.pb +3 -0
- FINAL-EFFICIENTNETV2-B0/variables/variables.data-00000-of-00001 +3 -0
- FINAL-EFFICIENTNETV2-B0/variables/variables.index +0 -0
- Video1-fake-1-ff.mp4 +3 -0
- Video3-fake-3-ff.mp4 +3 -0
- Video6-real-1-ff.mp4 +3 -0
- Video8-real-3-ff.mp4 +3 -0
- app.py +179 -0
- fake-1.mp4 +0 -0
- obama-fake-trump.mp4 +0 -0
- obama-fake.mp4 +3 -0
- packages.txt +3 -0
- real-1.mp4 +0 -0
- references.txt +27 -0
- requirements.txt +8 -0
- results.gif +0 -0
- video.mp4 +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,9 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
FINAL-EFFICIENTNETV2-B0/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
obama-fake.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
Video1-fake-1-ff.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
Video3-fake-3-ff.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
Video6-real-1-ff.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
Video8-real-3-ff.mp4 filter=lfs diff=lfs merge=lfs -text
|
FINAL-EFFICIENTNETV2-B0.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2006d2344a1804c269dd6c686181ee0e28213db5c52b73abfbfacf99e0d604e2
|
| 3 |
+
size 22914217
|
FINAL-EFFICIENTNETV2-B0/keras_metadata.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8411f85bd22de246fee31adc6bbf0a60d403ac22d8f572154fd77eb866b8daf3
|
| 3 |
+
size 202114
|
FINAL-EFFICIENTNETV2-B0/saved_model.pb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ca17aff86eeedbeab2ace0fc42296a1fe11352c6adb418f04f96c5a3607bd28a
|
| 3 |
+
size 10505251
|
FINAL-EFFICIENTNETV2-B0/variables/variables.data-00000-of-00001
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6418ccca9c8b62339ccfae9e5e3aae785fbdeed31fa08af7207ad4f0fc94fbbf
|
| 3 |
+
size 23824720
|
FINAL-EFFICIENTNETV2-B0/variables/variables.index
ADDED
|
Binary file (21.2 kB). View file
|
|
|
Video1-fake-1-ff.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:58262ed5e804069587e393ed06b48e655ca35d7ad58b68c161f5356a14482c48
|
| 3 |
+
size 1746578
|
Video3-fake-3-ff.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4e9ef4e65483c4152f477803fb083be53ec9311e4006abaacbf9647bf3ae0fa5
|
| 3 |
+
size 9101725
|
Video6-real-1-ff.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ad4e54db5f1b0c2f556e039d61ec38e7195edbba6257e266244be64af0bda5e3
|
| 3 |
+
size 1771036
|
Video8-real-3-ff.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:719f49698458abfa2ff25eb617ff03c5e56ddea51d912d65fbfa44c3db94768a
|
| 3 |
+
size 8949516
|
app.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import os
|
| 11 |
+
import zipfile
|
| 12 |
+
|
| 13 |
+
local_zip = "FINAL-EFFICIENTNETV2-B0.zip"
|
| 14 |
+
zip_ref = zipfile.ZipFile(local_zip, 'r')
|
| 15 |
+
zip_ref.extractall('FINAL-EFFICIENTNETV2-B0')
|
| 16 |
+
zip_ref.close()
|
| 17 |
+
|
| 18 |
+
# Load face detector
|
| 19 |
+
mtcnn = MTCNN(margin=14, keep_all=True, factor=0.7, device='cpu')
|
| 20 |
+
|
| 21 |
+
#Face Detection function, Reference: (Timesler, 2020); Source link: https://www.kaggle.com/timesler/facial-recognition-model-in-pytorch
|
| 22 |
+
class DetectionPipeline:
|
| 23 |
+
"""Pipeline class for detecting faces in the frames of a video file."""
|
| 24 |
+
|
| 25 |
+
def __init__(self, detector, n_frames=None, batch_size=60, resize=None):
|
| 26 |
+
"""Constructor for DetectionPipeline class.
|
| 27 |
+
|
| 28 |
+
Keyword Arguments:
|
| 29 |
+
n_frames {int} -- Total number of frames to load. These will be evenly spaced
|
| 30 |
+
throughout the video. If not specified (i.e., None), all frames will be loaded.
|
| 31 |
+
(default: {None})
|
| 32 |
+
batch_size {int} -- Batch size to use with MTCNN face detector. (default: {32})
|
| 33 |
+
resize {float} -- Fraction by which to resize frames from original prior to face
|
| 34 |
+
detection. A value less than 1 results in downsampling and a value greater than
|
| 35 |
+
1 result in upsampling. (default: {None})
|
| 36 |
+
"""
|
| 37 |
+
self.detector = detector
|
| 38 |
+
self.n_frames = n_frames
|
| 39 |
+
self.batch_size = batch_size
|
| 40 |
+
self.resize = resize
|
| 41 |
+
|
| 42 |
+
def __call__(self, filename):
|
| 43 |
+
"""Load frames from an MP4 video and detect faces.
|
| 44 |
+
|
| 45 |
+
Arguments:
|
| 46 |
+
filename {str} -- Path to video.
|
| 47 |
+
"""
|
| 48 |
+
# Create video reader and find length
|
| 49 |
+
v_cap = cv2.VideoCapture(filename)
|
| 50 |
+
v_len = int(v_cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 51 |
+
|
| 52 |
+
# Pick 'n_frames' evenly spaced frames to sample
|
| 53 |
+
if self.n_frames is None:
|
| 54 |
+
sample = np.arange(0, v_len)
|
| 55 |
+
else:
|
| 56 |
+
sample = np.linspace(0, v_len - 1, self.n_frames).astype(int)
|
| 57 |
+
|
| 58 |
+
# Loop through frames
|
| 59 |
+
faces = []
|
| 60 |
+
frames = []
|
| 61 |
+
for j in range(v_len):
|
| 62 |
+
success = v_cap.grab()
|
| 63 |
+
if j in sample:
|
| 64 |
+
# Load frame
|
| 65 |
+
success, frame = v_cap.retrieve()
|
| 66 |
+
if not success:
|
| 67 |
+
continue
|
| 68 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 69 |
+
# frame = Image.fromarray(frame)
|
| 70 |
+
|
| 71 |
+
# Resize frame to desired size
|
| 72 |
+
if self.resize is not None:
|
| 73 |
+
frame = frame.resize([int(d * self.resize) for d in frame.size])
|
| 74 |
+
frames.append(frame)
|
| 75 |
+
|
| 76 |
+
# When batch is full, detect faces and reset frame list
|
| 77 |
+
if len(frames) % self.batch_size == 0 or j == sample[-1]:
|
| 78 |
+
|
| 79 |
+
boxes, probs = self.detector.detect(frames)
|
| 80 |
+
|
| 81 |
+
for i in range(len(frames)):
|
| 82 |
+
|
| 83 |
+
if boxes[i] is None:
|
| 84 |
+
faces.append(face2) #append previous face frame if no face is detected
|
| 85 |
+
continue
|
| 86 |
+
|
| 87 |
+
box = boxes[i][0].astype(int)
|
| 88 |
+
frame = frames[i]
|
| 89 |
+
face = frame[box[1]:box[3], box[0]:box[2]]
|
| 90 |
+
|
| 91 |
+
if not face.any():
|
| 92 |
+
faces.append(face2) #append previous face frame if no face is detected
|
| 93 |
+
continue
|
| 94 |
+
|
| 95 |
+
face2 = cv2.resize(face, (224, 224))
|
| 96 |
+
|
| 97 |
+
faces.append(face2)
|
| 98 |
+
|
| 99 |
+
frames = []
|
| 100 |
+
|
| 101 |
+
v_cap.release()
|
| 102 |
+
|
| 103 |
+
return faces
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
detection_pipeline = DetectionPipeline(detector=mtcnn,n_frames=20, batch_size=60)
|
| 107 |
+
|
| 108 |
+
model = tf.keras.models.load_model("FINAL-EFFICIENTNETV2-B0")
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def deepfakespredict(input_video):
|
| 112 |
+
|
| 113 |
+
faces = detection_pipeline(input_video)
|
| 114 |
+
|
| 115 |
+
total = 0
|
| 116 |
+
real = 0
|
| 117 |
+
fake = 0
|
| 118 |
+
|
| 119 |
+
for face in faces:
|
| 120 |
+
|
| 121 |
+
face2 = face/255
|
| 122 |
+
pred = model.predict(np.expand_dims(face2, axis=0))[0]
|
| 123 |
+
total+=1
|
| 124 |
+
|
| 125 |
+
pred2 = pred[1]
|
| 126 |
+
|
| 127 |
+
if pred2 > 0.5:
|
| 128 |
+
fake+=1
|
| 129 |
+
else:
|
| 130 |
+
real+=1
|
| 131 |
+
|
| 132 |
+
fake_ratio = fake/total
|
| 133 |
+
|
| 134 |
+
text =""
|
| 135 |
+
text2 = "Deepfakes Confidence: " + str(fake_ratio*100) + "%"
|
| 136 |
+
|
| 137 |
+
if fake_ratio >= 0.5:
|
| 138 |
+
text = "The video is FAKE."
|
| 139 |
+
else:
|
| 140 |
+
text = "The video is REAL."
|
| 141 |
+
|
| 142 |
+
face_frames = []
|
| 143 |
+
|
| 144 |
+
for face in faces:
|
| 145 |
+
face_frame = Image.fromarray(face.astype('uint8'), 'RGB')
|
| 146 |
+
face_frames.append(face_frame)
|
| 147 |
+
|
| 148 |
+
face_frames[0].save('results.gif', save_all=True, append_images=face_frames[1:], duration = 250, loop = 100 )
|
| 149 |
+
clip = mp.VideoFileClip("results.gif")
|
| 150 |
+
clip.write_videofile("video.mp4")
|
| 151 |
+
|
| 152 |
+
return text, text2, "video.mp4"
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
title="EfficientNetV2 Deepfakes Video Detector"
|
| 157 |
+
description="This is a demo implementation of EfficientNetV2 Deepfakes Image Detector by using frame-by-frame detection. \
|
| 158 |
+
To use it, simply upload your video, or click one of the examples to load them.\
|
| 159 |
+
This demo and model represent the Final Year Project titled \"Achieving Face Swapped Deepfakes Detection Using EfficientNetV2\" by a CS undergraduate Lee Sheng Yeh. \
|
| 160 |
+
The examples were extracted from Celeb-DF(V2)(Li et al, 2020) and FaceForensics++(Rossler et al., 2019). Full reference details is available in \"references.txt.\" \
|
| 161 |
+
The examples are used under fair use to demo the working of the model only. If any copyright is infringed, please contact the researcher via this email: [email protected].\
|
| 162 |
+
"
|
| 163 |
+
|
| 164 |
+
examples = [
|
| 165 |
+
['Video1-fake-1-ff.mp4'],
|
| 166 |
+
['Video6-real-1-ff.mp4'],
|
| 167 |
+
['Video3-fake-3-ff.mp4'],
|
| 168 |
+
['Video8-real-3-ff.mp4'],
|
| 169 |
+
['real-1.mp4'],
|
| 170 |
+
['fake-1.mp4'],
|
| 171 |
+
]
|
| 172 |
+
|
| 173 |
+
gr.Interface(deepfakespredict,
|
| 174 |
+
inputs = ["video"],
|
| 175 |
+
outputs=["text","text", gr.outputs.Video(label="Detected face sequence")],
|
| 176 |
+
title=title,
|
| 177 |
+
description=description,
|
| 178 |
+
examples=examples
|
| 179 |
+
).launch()
|
fake-1.mp4
ADDED
|
Binary file (622 kB). View file
|
|
|
obama-fake-trump.mp4
ADDED
|
Binary file (218 kB). View file
|
|
|
obama-fake.mp4
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8c3812d4b9baabc09d68a7a02601ae69c367ac49826b9a2e5cbdcbca59b843f1
|
| 3 |
+
size 4610918
|
packages.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
| 2 |
+
libsm6
|
| 3 |
+
libxext6
|
real-1.mp4
ADDED
|
Binary file (630 kB). View file
|
|
|
references.txt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Dataset References
|
| 2 |
+
|
| 3 |
+
Celeb-DF (V2)
|
| 4 |
+
|
| 5 |
+
@inproceedings{Celeb_DF_cvpr20,
|
| 6 |
+
author = {Yuezun Li and Xin Yang and Pu Sun and Honggang Qi and Siwei Lyu},
|
| 7 |
+
title = {Celeb-DF: A Large-scale Challenging Dataset for DeepFake Forensics},
|
| 8 |
+
booktitle= {IEEE Conference on Computer Vision and Patten Recognition (CVPR)},
|
| 9 |
+
year = {2020}}
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
FaceForensics++ Dataset
|
| 13 |
+
|
| 14 |
+
@inproceedings{roessler2019faceforensicspp,
|
| 15 |
+
author = {Andreas R\"ossler and Davide Cozzolino and Luisa Verdoliva and Christian Riess and Justus Thies and Matthias Nie{\ss}ner},
|
| 16 |
+
title = {Face{F}orensics++: Learning to Detect Manipulated Facial Images},
|
| 17 |
+
booktitle= {International Conference on Computer Vision (ICCV)},
|
| 18 |
+
year = {2019} }
|
| 19 |
+
|
| 20 |
+
@MISC{DDD_GoogleJigSaw2019,
|
| 21 |
+
AUTHOR = {Dufour, Nicholas and Gully, Andrew and Karlsson, Per and Vorbyov, Alexey Victor and Leung, Thomas and Childs, Jeremiah and Bregler, Christoph},
|
| 22 |
+
DATE = {2019-09},
|
| 23 |
+
TITLE = {DeepFakes Detection Dataset by Google & JigSaw}}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
Face Detection Fucntion (Timesler, 2020)
|
| 27 |
+
Source Link: https://www.kaggle.com/timesler/facial-recognition-model-in-pytorch
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
tensorflow-addons
|
| 3 |
+
facenet_pytorch
|
| 4 |
+
numpy
|
| 5 |
+
opencv-python
|
| 6 |
+
opencv-python-headless
|
| 7 |
+
mtcnn
|
| 8 |
+
moviepy
|
results.gif
ADDED
|
video.mp4
ADDED
|
Binary file (47.1 kB). View file
|
|
|