Ron0420 commited on
Commit
abd7b7e
1 Parent(s): f8c6e23

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ detector = MTCNN()
11
+
12
+ model = tf.keras.models.load_model("FINAL-EFFICIENTNETV2-B0")
13
+
14
+
15
+ def deepfakespredict(input_img):
16
+
17
+ face = detector.detect_faces(input_img)
18
+
19
+ text =""
20
+
21
+ if len(face) > 0:
22
+ x, y, width, height = face[0]['box']
23
+ x2, y2 = x + width, y + height
24
+
25
+ cv2.rectangle(input_img, (x, y), (x2, y2), (0, 255, 0), 2)
26
+
27
+ face_image = input_img[y:y2, x:x2]
28
+ face_image2 = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
29
+ face_image3 = cv2.resize(face_image2, (224, 224))
30
+ face_image4 = face_image3/255
31
+
32
+ pred = model.predict(np.expand_dims(face_image4, axis=0))[0]
33
+
34
+ if pred[1] >= 0.6:
35
+ text = "The image is fake."
36
+ elif pred[0] >= 0.6:
37
+ text = "The image is real."
38
+ else:
39
+ text = "The image might be real or fake."
40
+
41
+ # if pred[1] >= 0.5:
42
+ # text = "The image is fake."
43
+ # else:
44
+ # text = "The image is real."
45
+
46
+ else:
47
+ text = "Face is not detected in the image."
48
+
49
+ return pred, text, input_img
50
+
51
+
52
+ title="EfficientNetV2 Deepfakes Image Detector"
53
+ description="This is a demo implementation of EfficientNetV2 Deepfakes Image Detector. To use it, simply upload your image, or click one of the examples to load them."
54
+ examples = [
55
+ ['fake-86.jpg'],
56
+ ['fake-239.jpg'],
57
+ ['fake-254.jpg'],
58
+ ['fake-1266.jpg'],
59
+ ['fake-2225.jpg']
60
+ ]
61
+ demo = gr.Interface(deepfakespredict,
62
+ inputs = ["image"],
63
+ outputs=["text","text","image"],
64
+ title=title,
65
+ description=description,
66
+ examples=examples
67
+ )
68
+ demo.launch()