Update app.py
Browse files
app.py
CHANGED
@@ -6,11 +6,11 @@ import torch
|
|
6 |
import torch.nn.functional as F
|
7 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
8 |
import numpy as np
|
9 |
-
from
|
10 |
-
|
11 |
-
import os
|
12 |
|
13 |
app = Flask(__name__)
|
|
|
14 |
|
15 |
# Configuration
|
16 |
UPLOAD_FOLDER = 'uploads'
|
@@ -58,6 +58,7 @@ def analyze_video(video_path, sample_rate=30):
|
|
58 |
frame_count = 0
|
59 |
fake_count = 0
|
60 |
total_processed = 0
|
|
|
61 |
|
62 |
while cap.isOpened():
|
63 |
ret, frame = cap.read()
|
@@ -73,6 +74,10 @@ def analyze_video(video_path, sample_rate=30):
|
|
73 |
if prediction == "fake":
|
74 |
fake_count += 1
|
75 |
|
|
|
|
|
|
|
|
|
76 |
frame_count += 1
|
77 |
|
78 |
cap.release()
|
@@ -96,7 +101,20 @@ def analyze_video_api():
|
|
96 |
if file and allowed_file(file.filename):
|
97 |
filename = secure_filename(file.filename)
|
98 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
try:
|
102 |
fake_percentage = analyze_video(filepath)
|
@@ -115,4 +133,4 @@ def analyze_video_api():
|
|
115 |
return jsonify({'error': 'Invalid file type'}), 400
|
116 |
|
117 |
if __name__ == '__main__':
|
118 |
-
|
|
|
6 |
import torch.nn.functional as F
|
7 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
8 |
import numpy as np
|
9 |
+
from flask_socketio import SocketIO
|
10 |
+
import time
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
+
socketio = SocketIO(app, cors_allowed_origins="*")
|
14 |
|
15 |
# Configuration
|
16 |
UPLOAD_FOLDER = 'uploads'
|
|
|
58 |
frame_count = 0
|
59 |
fake_count = 0
|
60 |
total_processed = 0
|
61 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
62 |
|
63 |
while cap.isOpened():
|
64 |
ret, frame = cap.read()
|
|
|
74 |
if prediction == "fake":
|
75 |
fake_count += 1
|
76 |
|
77 |
+
# Emit progress update
|
78 |
+
progress = (frame_count / total_frames) * 100
|
79 |
+
socketio.emit('analysis_progress', {'progress': progress})
|
80 |
+
|
81 |
frame_count += 1
|
82 |
|
83 |
cap.release()
|
|
|
101 |
if file and allowed_file(file.filename):
|
102 |
filename = secure_filename(file.filename)
|
103 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
104 |
+
|
105 |
+
# Save file and emit upload progress
|
106 |
+
chunk_size = 4096
|
107 |
+
file_size = int(request.headers.get('Content-Length', 0))
|
108 |
+
bytes_read = 0
|
109 |
+
with open(filepath, 'wb') as f:
|
110 |
+
while True:
|
111 |
+
chunk = file.read(chunk_size)
|
112 |
+
if not chunk:
|
113 |
+
break
|
114 |
+
f.write(chunk)
|
115 |
+
bytes_read += len(chunk)
|
116 |
+
progress = (bytes_read / file_size) * 100
|
117 |
+
socketio.emit('upload_progress', {'progress': progress})
|
118 |
|
119 |
try:
|
120 |
fake_percentage = analyze_video(filepath)
|
|
|
133 |
return jsonify({'error': 'Invalid file type'}), 400
|
134 |
|
135 |
if __name__ == '__main__':
|
136 |
+
socketio.run(app, host='0.0.0.0', port=7860)
|