raghvandr99 commited on
Commit
e69a276
·
verified ·
1 Parent(s): 3b2f3bb
Files changed (2) hide show
  1. app.py +102 -0
  2. dockerfile +30 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from flask import Flask, request, jsonify
3
+ from werkzeug.utils import secure_filename
4
+ import cv2
5
+ import dlib
6
+ import numpy as np
7
+ import librosa
8
+ import torch
9
+ from tensorflow.keras.models import load_model
10
+
11
+ # Initialize Flask app
12
+ app = Flask(__name__)
13
+
14
+ # Define directories for uploads
15
+ UPLOAD_FOLDER = './uploads'
16
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
17
+
18
+ # Ensure upload directory exists
19
+ if not os.path.exists(UPLOAD_FOLDER):
20
+ os.makedirs(UPLOAD_FOLDER)
21
+
22
+ # Load pre-trained models for detection
23
+ face_swap_model = load_model('models/face_swap_detector.h5')
24
+ audio_clone_model = torch.load('models/audio_clone_detector.pth')
25
+
26
+ # Face detection using dlib
27
+ face_detector = dlib.get_frontal_face_detector()
28
+
29
+ # Helper function to detect face swap
30
+ def detect_face_swap(video_path):
31
+ cap = cv2.VideoCapture(video_path)
32
+ deepfake_detected = False
33
+ while cap.isOpened():
34
+ ret, frame = cap.read()
35
+ if not ret:
36
+ break
37
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
38
+ faces = face_detector(gray)
39
+
40
+ for face in faces:
41
+ x, y, w, h = (face.left(), face.top(), face.width(), face.height())
42
+ face_img = frame[y:y+h, x:x+w]
43
+ face_img_resized = cv2.resize(face_img, (128, 128)) # Resize to fit model input
44
+ face_img_resized = np.expand_dims(face_img_resized, axis=0)
45
+
46
+ prediction = face_swap_model.predict(face_img_resized)
47
+ if prediction > 0.5:
48
+ deepfake_detected = True
49
+ break
50
+
51
+ cap.release()
52
+ return deepfake_detected
53
+
54
+ # Helper function to detect audio clone
55
+ def detect_audio_clone(audio_path):
56
+ y, sr = librosa.load(audio_path)
57
+ mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
58
+ mfccs_tensor = torch.tensor(mfccs).unsqueeze(0)
59
+
60
+ with torch.no_grad():
61
+ prediction = audio_clone_model(mfccs_tensor)
62
+ if prediction.item() > 0.5:
63
+ return True
64
+ return False
65
+
66
+ # API endpoint to upload video and audio, process, and return results
67
+ @app.route('/upload', methods=['POST'])
68
+ def upload_file():
69
+ if 'video' not in request.files:
70
+ return jsonify({'error': 'No video file uploaded'}), 400
71
+
72
+ # Save video file
73
+ video_file = request.files['video']
74
+ video_filename = secure_filename(video_file.filename)
75
+ video_path = os.path.join(app.config['UPLOAD_FOLDER'], video_filename)
76
+ video_file.save(video_path)
77
+
78
+ # Run face swap detection
79
+ face_swap_detected = detect_face_swap(video_path)
80
+
81
+ # Run audio clone detection if an audio track exists
82
+ audio_detected = False
83
+ audio_path = None
84
+ if 'audio' in request.files:
85
+ audio_file = request.files['audio']
86
+ audio_filename = secure_filename(audio_file.filename)
87
+ audio_path = os.path.join(app.config['UPLOAD_FOLDER'], audio_filename)
88
+ audio_file.save(audio_path)
89
+
90
+ # Run audio clone detection
91
+ audio_detected = detect_audio_clone(audio_path)
92
+
93
+ result = {
94
+ 'face_swap_detected': face_swap_detected,
95
+ 'audio_clone_detected': audio_detected,
96
+ }
97
+
98
+ return jsonify(result), 200
99
+
100
+ # Run the Flask app
101
+ if __name__ == '__main__':
102
+ app.run(host='0.0.0.0', port=5000)
dockerfile ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base image
2
+ FROM python:3.9-slim
3
+
4
+ # Set up working directory
5
+ WORKDIR /app
6
+
7
+ # Install dependencies
8
+ RUN apt-get update && \
9
+ apt-get install -y \
10
+ ffmpeg \
11
+ libsm6 \
12
+ libxext6 \
13
+ libgl1-mesa-glx \
14
+ && rm -rf /var/lib/apt/lists/*
15
+
16
+ # Copy requirements file
17
+ COPY requirements.txt .
18
+
19
+ # Install Python dependencies
20
+ RUN pip install --upgrade pip
21
+ RUN pip install -r requirements.txt
22
+
23
+ # Copy the application code
24
+ COPY . .
25
+
26
+ # Expose the Flask port
27
+ EXPOSE 5000
28
+
29
+ # Run the Flask app
30
+ CMD ["python", "app.py"]