forked from Srujan-rai/Deepfake_voice_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
51 lines (39 loc) · 1.46 KB
/
app.py
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
from flask import Flask, request, render_template
from tensorflow.keras.preprocessing import image
import numpy as np
from tensorflow import keras
import os
from werkzeug.utils import secure_filename
app = Flask(__name__)
model = keras.models.load_model('deepfake_model.h5')
image_height = 640
image_width = 480
def process_image(file):
filename = secure_filename(file.filename)
file_path = os.path.join('uploads', filename)
file.save(file_path)
img = image.load_img(file_path, target_size=(image_height, image_width))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = img / 255.0
return img
@app.route('/')
def home():
return render_template('upload.html', prediction=None)
@app.route('/predict', methods=['POST'])
def predict():
if 'file' not in request.files:
return render_template('upload.html', prediction="No file uploaded")
file = request.files['file']
if file.filename == '':
return render_template('upload.html', prediction="No file selected")
if file:
img = process_image(file)
predictions = model.predict(img)
class_names = ['fake', 'real']
predicted_class_index = np.argmax(predictions)
predicted_class = class_names[predicted_class_index]
return render_template('upload.html', prediction=predicted_class, image_path=f'uploads/{file.filename}')
if __name__ == '__main__':
os.makedirs('uploads', exist_ok=True)
app.run(debug=True)