Final commit
Browse files
app.py
CHANGED
@@ -68,12 +68,13 @@ def process_frame(frame):
|
|
68 |
return prediction, output.item(), visualization
|
69 |
|
70 |
@spaces.GPU
|
71 |
-
def analyze_video(video_path, sample_rate=30, top_n=5):
|
72 |
cap = cv2.VideoCapture(video_path)
|
73 |
frame_count = 0
|
74 |
fake_count = 0
|
75 |
total_processed = 0
|
76 |
frames_info = []
|
|
|
77 |
|
78 |
while cap.isOpened():
|
79 |
ret, frame = cap.read()
|
@@ -86,6 +87,7 @@ def analyze_video(video_path, sample_rate=30, top_n=5):
|
|
86 |
|
87 |
if prediction is not None:
|
88 |
total_processed += 1
|
|
|
89 |
if prediction == "fake":
|
90 |
fake_count += 1
|
91 |
|
@@ -102,12 +104,23 @@ def analyze_video(video_path, sample_rate=30, top_n=5):
|
|
102 |
|
103 |
if total_processed > 0:
|
104 |
fake_percentage = (fake_count / total_processed) * 100
|
|
|
|
|
|
|
105 |
frames_info.sort(key=lambda x: x['confidence'], reverse=True)
|
106 |
top_frames = frames_info[:top_n]
|
107 |
|
108 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
else:
|
110 |
-
return
|
111 |
|
112 |
@app.route('/analyze', methods=['POST'])
|
113 |
def analyze_video_api():
|
@@ -125,20 +138,17 @@ def analyze_video_api():
|
|
125 |
file.save(filepath)
|
126 |
|
127 |
try:
|
128 |
-
|
129 |
os.remove(filepath) # Remove the file after analysis
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
frame
|
|
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
'
|
138 |
-
'top_frames': top_frames
|
139 |
-
}
|
140 |
-
|
141 |
-
return jsonify(result), 200
|
142 |
except Exception as e:
|
143 |
os.remove(filepath) # Remove the file if an error occurs
|
144 |
return jsonify({'error': str(e)}), 500
|
|
|
68 |
return prediction, output.item(), visualization
|
69 |
|
70 |
@spaces.GPU
|
71 |
+
def analyze_video(video_path, sample_rate=30, top_n=5, detection_threshold=0.5):
|
72 |
cap = cv2.VideoCapture(video_path)
|
73 |
frame_count = 0
|
74 |
fake_count = 0
|
75 |
total_processed = 0
|
76 |
frames_info = []
|
77 |
+
confidence_scores = []
|
78 |
|
79 |
while cap.isOpened():
|
80 |
ret, frame = cap.read()
|
|
|
87 |
|
88 |
if prediction is not None:
|
89 |
total_processed += 1
|
90 |
+
confidence_scores.append(confidence)
|
91 |
if prediction == "fake":
|
92 |
fake_count += 1
|
93 |
|
|
|
104 |
|
105 |
if total_processed > 0:
|
106 |
fake_percentage = (fake_count / total_processed) * 100
|
107 |
+
average_confidence = sum(confidence_scores) / len(confidence_scores)
|
108 |
+
model_confidence = 1 - (sum((score - average_confidence) ** 2 for score in confidence_scores) / len(confidence_scores))
|
109 |
+
|
110 |
frames_info.sort(key=lambda x: x['confidence'], reverse=True)
|
111 |
top_frames = frames_info[:top_n]
|
112 |
|
113 |
+
return {
|
114 |
+
'fake_percentage': fake_percentage,
|
115 |
+
'is_likely_deepfake': fake_percentage >= 60,
|
116 |
+
'top_frames': top_frames,
|
117 |
+
'model_confidence': model_confidence,
|
118 |
+
'total_frames_analyzed': total_processed,
|
119 |
+
'average_confidence_score': average_confidence,
|
120 |
+
'detection_threshold': detection_threshold
|
121 |
+
}
|
122 |
else:
|
123 |
+
return None
|
124 |
|
125 |
@app.route('/analyze', methods=['POST'])
|
126 |
def analyze_video_api():
|
|
|
138 |
file.save(filepath)
|
139 |
|
140 |
try:
|
141 |
+
result = analyze_video(filepath)
|
142 |
os.remove(filepath) # Remove the file after analysis
|
143 |
|
144 |
+
if result:
|
145 |
+
# Convert numpy arrays to base64 encoded strings
|
146 |
+
for frame in result['top_frames']:
|
147 |
+
frame['visualization'] = base64.b64encode(cv2.imencode('.png', frame['visualization'])[1]).decode('utf-8')
|
148 |
|
149 |
+
return jsonify(result), 200
|
150 |
+
else:
|
151 |
+
return jsonify({'error': 'No frames could be processed'}), 400
|
|
|
|
|
|
|
|
|
152 |
except Exception as e:
|
153 |
os.remove(filepath) # Remove the file if an error occurs
|
154 |
return jsonify({'error': str(e)}), 500
|