Skip to content

Commit db0f7ee

Browse files
Add detector.py file
1 parent a8101ab commit db0f7ee

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can download the weights of all models and h5 file of GAN_fake_vs_real model
66
After downloading the repository, you can run the requirements.txt file to install the required packages.
77
<br>
88

9-
You can run the predict.ipynb file from each directory to test the model.
9+
You can run the predict.ipynb file from each directory to test the model.
1010

1111
### Note:
1212
If you get this error `ImportError: cannot import name 'Sequence' from 'keras.utils' (/usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py)`
@@ -15,9 +15,17 @@ then open the file in location `/usr/local/lib/python3.7/dist-packages/keras_vid
1515
and then replace line `from keras.utils import Sequence` with `from tensorflow.keras.utils import Sequence` and save it.
1616
<br>
1717

18+
### Detector.py file:
19+
It consists of function that can be used to detect deepfake or ganfake etc.
20+
<br>
21+
you can call the function `predict` which accepts two parameters:
22+
1. path of the video or image
23+
2. type which you want to check (deepfake video, deepfake image, ganfake)
24+
1825
## Directory Structure
1926
```
2027
| .gitignore
28+
| detector.py
2129
| README.md
2230
| requirements.txt
2331
|

detector.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import os
2+
import cv2
3+
import numpy as np
4+
import tensorflow as tf
5+
from tensorflow import keras
6+
from tensorflow.keras.preprocessing import image
7+
8+
9+
def predict_deepfake_video(video_path, model_path, model_weights_path, faceCascade_path):
10+
"""
11+
Predict if a video is a deepfake or not.
12+
"""
13+
14+
# Load model and weights
15+
model = keras.models.load_model(model_path, compile=False)
16+
model.load_weights(model_weights_path)
17+
18+
cap = cv2.VideoCapture(video_path)
19+
img_array = []
20+
success = 1
21+
i = 1
22+
faceCascade = cv2.CascadeClassifier(faceCascade_path)
23+
24+
while success and i <= 90:
25+
success, img = cap.read()
26+
try:
27+
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
28+
faces = faceCascade.detectMultiScale(imgGray, 1.1, 4)
29+
if np.shape(faces) == (1, 4):
30+
x, y, w, h = faces[0]
31+
imgCropped = img[y: y + h, x:x + w]
32+
imgCropped = cv2.resize(imgCropped, (112, 112))
33+
img_array.append(imgCropped)
34+
i += 1
35+
except:
36+
return "Provide a video with more than 90 frames"
37+
38+
img_array = np.array(img_array)
39+
img_array = img_array.reshape(1, 90, 112, 112, 3)
40+
41+
res = model.predict(img_array).round()
42+
if res[0][0] == 0:
43+
return "Real"
44+
else:
45+
return "Deepfake"
46+
47+
48+
def predict_deepfake_image(image_path, model_path, model_weights_path):
49+
"""
50+
Predict if an image is a deepfake or not.
51+
"""
52+
53+
model = keras.models.load_model(model_path, compile=False)
54+
model.load_weights(model_weights_path)
55+
img = image.load_img(image_path, target_size=(256, 256))
56+
x = image.img_to_array(img)
57+
x = np.expand_dims(x, axis=0)
58+
x = tf.image.resize(x, (256, 256))
59+
x /= 255.0
60+
res = model.predict(x)
61+
if(res[0][0] < 0):
62+
return "Deepfake"
63+
else:
64+
return "Real"
65+
66+
67+
def predict_gan_fake(image_path, model_path, model_weights_path):
68+
"""
69+
Predict if an image is a GAN Fake or Real.
70+
"""
71+
72+
model = keras.models.load_model(model_path, compile=False)
73+
model.load_weights(model_weights_path)
74+
img = image.load_img(image_path, target_size=(256, 256))
75+
x = image.img_to_array(img)
76+
x = np.expand_dims(x, axis=0)
77+
x = tf.image.resize(x, (256, 256))
78+
x /= 255
79+
80+
res = model.predict(x)
81+
82+
if(res[0][0] < 0):
83+
return "GAN Fake"
84+
else:
85+
return "Real"
86+
87+
88+
def predict(input_path, type):
89+
"""
90+
It takes input and type
91+
Based on the type it does the prediction on input
92+
"1" for deepfake video
93+
"2" for deepfake image
94+
"3" for GAN Fake
95+
"""
96+
97+
physical_devices = tf.config.list_physical_devices('GPU')
98+
tf.config.experimental.set_memory_growth(physical_devices[0], True)
99+
100+
# check if input path exists and is a file
101+
if not os.path.exists(input_path) or not os.path.isfile(input_path):
102+
return "Provide a valid path"
103+
104+
if type == "1":
105+
model_path = "deepfake_video/video_model.h5"
106+
model_weights_path = "deepfake_video/Weights/model_weights"
107+
faceCascade_path = "deepfake_video/Resources/haarcascade_frontalface_default.xml"
108+
try:
109+
return predict_deepfake_video(input_path, model_path, model_weights_path, faceCascade_path)
110+
except:
111+
return "No video found"
112+
113+
elif type == "2":
114+
model_path = "deepfake_image\CNN_SVM_Model\cnn_svm_model.h5"
115+
model_weights_path = "deepfake_image\Weights_CNN_SVM\model_weights"
116+
try:
117+
return predict_deepfake_image(input_path, model_path, model_weights_path)
118+
except:
119+
return "No image found"
120+
121+
elif type == "3":
122+
model_path = "GAN_Fake_vs_Real/Inception_Resnet_SVM_Model\Inception_Resnet_svm_model.h5"
123+
model_weights_path = "GAN_Fake_vs_Real/Weights_Inception_Resnet_SVM/model_weights"
124+
try:
125+
return predict_gan_fake(input_path, model_path, model_weights_path)
126+
except:
127+
return "No image found"
128+
129+
else:
130+
return "Invalid type"

0 commit comments

Comments
 (0)