forked from gilhotraapoorva/DeepFake_MultiFace_Videos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
119 lines (93 loc) · 3.35 KB
/
app2.py
File metadata and controls
119 lines (93 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from cProfile import label
from flask import Flask, request, jsonify, redirect
import os
import uuid
import cv2
import numpy as np
from tensorflow import keras
from keras.applications import inception_v3
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
IMG_SIZE = 224
BATCH_SIZE = 64
EPOCHS = 10
MAX_SEQ_LENGTH = 20
NUM_FEATURES = 2048
MODEL_PATH = 'my_model.h5'
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'mp4', 'avi'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def crop_center_square(frame):
y, x = frame.shape[0:2]
min_dim = min(y, x)
start_x = (x // 2) - (min_dim // 2)
start_y = (y // 2) - (min_dim // 2)
return frame[start_y : start_y + min_dim, start_x : start_x+min_dim]
def load_video(path, max_frames=0, resize=(IMG_SIZE, IMG_SIZE)):
cap = cv2.VideoCapture(path)
frames = []
try:
while True:
ret, frame = cap.read()
if not ret:
break
frame = crop_center_square(frame)
frame = cv2.resize(frame, resize)
frame = frame[:, :, [2, 1, 0]]
frames.append(frame)
if len(frames) == max_frames:
break
finally:
cap.release()
return np.array(frames)
def build_feature_extractor():
feature_extractor = keras.applications.InceptionV3(
weights="imagenet",
include_top=False,
pooling="avg",
input_shape=(IMG_SIZE, IMG_SIZE, 3),
)
preprocess_input = keras.applications.inception_v3.preprocess_input
inputs = keras.Input((IMG_SIZE, IMG_SIZE, 3))
preprocessed = preprocess_input(inputs)
outputs = feature_extractor(preprocessed)
return keras.Model(inputs, outputs, name="feature_extractor")
feature_extractor = build_feature_extractor()
def prepare_single_video(frames):
frames = frames[None, ...]
frame_mask = np.zeros(shape=(1, MAX_SEQ_LENGTH,), dtype="bool")
frame_features = np.zeros(shape=(1, MAX_SEQ_LENGTH, NUM_FEATURES), dtype="float32")
for i, batch in enumerate(frames):
video_length = batch.shape[0]
length = min(MAX_SEQ_LENGTH, video_length)
for j in range(length):
frame_features[i, j, :] = feature_extractor.predict(batch[None, j, :])
frame_mask[i, :length] = 1
return frame_features, frame_mask
def sequence_prediction(path):
frames = load_video(path)
frame_features, frame_mask = prepare_single_video(frames)
model = keras.models.load_model(MODEL_PATH)
return model.predict([frame_features, frame_mask])[0]
@app.route('/summary', methods=['POST'])
def predict_deepfake():
if request.method == 'POST':
video_file = request.files['video']
if video_file and allowed_file(video_file.filename):
video_path = os.path.join(UPLOAD_FOLDER, video_file.filename)
video_file.save(video_path)
ans=sequence_prediction(video_path)
print(ans)
if(ans <= 0.76):
is_deepfake="FAKE"
else:
is_deepfake="REAL"
return jsonify({'is_deepfake': is_deepfake})
else:
return jsonify({'error': 'Invalid file format'}), 400
return jsonify({'error': 'No video uploaded'}), 400
# if __name__ == '__main__':
# app.run(host='127.0.0.1', port=5000)