Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,92 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import cv2
|
| 4 |
-
from mtcnn.mtcnn import MTCNN
|
| 5 |
-
import tensorflow as tf
|
| 6 |
-
import tensorflow_addons
|
| 7 |
-
import numpy as np
|
| 8 |
-
|
| 9 |
-
import os
|
| 10 |
-
import zipfile
|
| 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 |
-
model = tf.keras.models.load_model("FINAL-EFFICIENTNETV2-B0")
|
| 18 |
-
|
| 19 |
-
detector = MTCNN()
|
| 20 |
-
|
| 21 |
-
def deepfakespredict(input_img ):
|
| 22 |
-
|
| 23 |
-
labels = ['real', 'fake']
|
| 24 |
-
pred = [0, 0]
|
| 25 |
-
text =""
|
| 26 |
-
text2 =""
|
| 27 |
-
|
| 28 |
-
face = detector.detect_faces(input_img)
|
| 29 |
-
|
| 30 |
-
if len(face) > 0:
|
| 31 |
-
x, y, width, height = face[0]['box']
|
| 32 |
-
x2, y2 = x + width, y + height
|
| 33 |
-
|
| 34 |
-
cv2.rectangle(input_img, (x, y), (x2, y2), (0, 255, 0), 2)
|
| 35 |
-
|
| 36 |
-
face_image = input_img[y:y2, x:x2]
|
| 37 |
-
face_image2 = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
|
| 38 |
-
face_image3 = cv2.resize(face_image2, (224, 224))
|
| 39 |
-
face_image4 = face_image3/255
|
| 40 |
-
|
| 41 |
-
pred = model.predict(np.expand_dims(face_image4, axis=0))[0]
|
| 42 |
-
|
| 43 |
-
if pred[1] >= 0.6:
|
| 44 |
-
text = "The image is FAKE."
|
| 45 |
-
elif pred[0] >= 0.6:
|
| 46 |
-
text = "The image is REAL."
|
| 47 |
-
else:
|
| 48 |
-
text = "The image may be REAL or FAKE."
|
| 49 |
-
|
| 50 |
-
else:
|
| 51 |
-
text = "Face is not detected in the image."
|
| 52 |
-
|
| 53 |
-
text2 = "REAL: " + str(np.round(pred[0]*100, 2)) + "%, FAKE: " + str(np.round(pred[1]*100, 2)) + "%"
|
| 54 |
-
|
| 55 |
-
return input_img, text, text2, {labels[i]: float(pred[i]) for i in range(2)}
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
title="EfficientNetV2 Deepfakes Image Detector"
|
| 59 |
-
description="This is a demo implementation of EfficientNetV2 Deepfakes Image Detector. \
|
| 60 |
-
To use it, simply upload your image, or click one of the examples to load them. \
|
| 61 |
-
This demo and model represent the Final Year Project titled \"Achieving Face Swapped Deepfakes Detection Using EfficientNetV2\" by a CS undergraduate Lee Sheng Yeh. \
|
| 62 |
-
The examples were extracted from Celeb-DF(V2)(Li et al, 2020) and FaceForensics++(Rossler et al., 2019). Full reference
|
| 63 |
-
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: tp054565@mail.apu.edu.my
|
| 64 |
-
"
|
| 65 |
-
|
| 66 |
-
examples = [
|
| 67 |
-
['Fake-1.png'],
|
| 68 |
-
['Fake-2.png'],
|
| 69 |
-
['Fake-3.png'],
|
| 70 |
-
['Fake-4.png'],
|
| 71 |
-
['Fake-5.png'],
|
| 72 |
-
|
| 73 |
-
['Real-1.png'],
|
| 74 |
-
['Real-2.png'],
|
| 75 |
-
['Real-3.png'],
|
| 76 |
-
['Real-4.png'],
|
| 77 |
-
['Real-5.png']
|
| 78 |
-
|
| 79 |
-
]
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
gr.Interface(deepfakespredict,
|
| 83 |
-
inputs = ["image"],
|
| 84 |
-
outputs=[gr.outputs.Image(type="pil", label="Detected face"),
|
| 85 |
-
"text",
|
| 86 |
-
"text",
|
| 87 |
-
gr.outputs.Label(num_top_classes=None, type="auto", label="Confidence")],
|
| 88 |
-
title=title,
|
| 89 |
-
description=description,
|
| 90 |
-
examples = examples,
|
| 91 |
-
examples_per_page = 5
|
| 92 |
).launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
import cv2
|
| 4 |
+
from mtcnn.mtcnn import MTCNN
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import tensorflow_addons
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import zipfile
|
| 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 |
+
model = tf.keras.models.load_model("FINAL-EFFICIENTNETV2-B0")
|
| 18 |
+
|
| 19 |
+
detector = MTCNN()
|
| 20 |
+
|
| 21 |
+
def deepfakespredict(input_img ):
|
| 22 |
+
|
| 23 |
+
labels = ['real', 'fake']
|
| 24 |
+
pred = [0, 0]
|
| 25 |
+
text =""
|
| 26 |
+
text2 =""
|
| 27 |
+
|
| 28 |
+
face = detector.detect_faces(input_img)
|
| 29 |
+
|
| 30 |
+
if len(face) > 0:
|
| 31 |
+
x, y, width, height = face[0]['box']
|
| 32 |
+
x2, y2 = x + width, y + height
|
| 33 |
+
|
| 34 |
+
cv2.rectangle(input_img, (x, y), (x2, y2), (0, 255, 0), 2)
|
| 35 |
+
|
| 36 |
+
face_image = input_img[y:y2, x:x2]
|
| 37 |
+
face_image2 = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
|
| 38 |
+
face_image3 = cv2.resize(face_image2, (224, 224))
|
| 39 |
+
face_image4 = face_image3/255
|
| 40 |
+
|
| 41 |
+
pred = model.predict(np.expand_dims(face_image4, axis=0))[0]
|
| 42 |
+
|
| 43 |
+
if pred[1] >= 0.6:
|
| 44 |
+
text = "The image is FAKE."
|
| 45 |
+
elif pred[0] >= 0.6:
|
| 46 |
+
text = "The image is REAL."
|
| 47 |
+
else:
|
| 48 |
+
text = "The image may be REAL or FAKE."
|
| 49 |
+
|
| 50 |
+
else:
|
| 51 |
+
text = "Face is not detected in the image."
|
| 52 |
+
|
| 53 |
+
text2 = "REAL: " + str(np.round(pred[0]*100, 2)) + "%, FAKE: " + str(np.round(pred[1]*100, 2)) + "%"
|
| 54 |
+
|
| 55 |
+
return input_img, text, text2, {labels[i]: float(pred[i]) for i in range(2)}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
title="EfficientNetV2 Deepfakes Image Detector"
|
| 59 |
+
description="This is a demo implementation of EfficientNetV2 Deepfakes Image Detector. \
|
| 60 |
+
To use it, simply upload your image, or click one of the examples to load them. \
|
| 61 |
+
This demo and model represent the Final Year Project titled \"Achieving Face Swapped Deepfakes Detection Using EfficientNetV2\" by a CS undergraduate Lee Sheng Yeh. \
|
| 62 |
+
The examples were extracted from Celeb-DF(V2)(Li et al, 2020) and FaceForensics++(Rossler et al., 2019). Full reference detail is available in \"references.txt.\" \
|
| 63 |
+
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: tp054565@mail.apu.edu.my.\
|
| 64 |
+
"
|
| 65 |
+
|
| 66 |
+
examples = [
|
| 67 |
+
['Fake-1.png'],
|
| 68 |
+
['Fake-2.png'],
|
| 69 |
+
['Fake-3.png'],
|
| 70 |
+
['Fake-4.png'],
|
| 71 |
+
['Fake-5.png'],
|
| 72 |
+
|
| 73 |
+
['Real-1.png'],
|
| 74 |
+
['Real-2.png'],
|
| 75 |
+
['Real-3.png'],
|
| 76 |
+
['Real-4.png'],
|
| 77 |
+
['Real-5.png']
|
| 78 |
+
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
gr.Interface(deepfakespredict,
|
| 83 |
+
inputs = ["image"],
|
| 84 |
+
outputs=[gr.outputs.Image(type="pil", label="Detected face"),
|
| 85 |
+
"text",
|
| 86 |
+
"text",
|
| 87 |
+
gr.outputs.Label(num_top_classes=None, type="auto", label="Confidence")],
|
| 88 |
+
title=title,
|
| 89 |
+
description=description,
|
| 90 |
+
examples = examples,
|
| 91 |
+
examples_per_page = 5
|
| 92 |
).launch()
|