Update app.py
Browse files
app.py
CHANGED
|
@@ -28,7 +28,9 @@ def analyze_video(video):
|
|
| 28 |
cap = cv2.VideoCapture(video)
|
| 29 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 30 |
if total_frames == 0:
|
| 31 |
-
return
|
|
|
|
|
|
|
| 32 |
|
| 33 |
num_frames_to_process = min(200, total_frames)
|
| 34 |
frame_indices = [int(i * total_frames / num_frames_to_process) for i in range(num_frames_to_process)]
|
|
@@ -44,16 +46,17 @@ def analyze_video(video):
|
|
| 44 |
if frame_num == frame_indices[current_index]:
|
| 45 |
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 46 |
pil_image = Image.fromarray(rgb)
|
| 47 |
-
frames_to_process.append(
|
| 48 |
current_index += 1
|
| 49 |
frame_num += 1
|
| 50 |
|
| 51 |
cap.release()
|
| 52 |
if not frames_to_process:
|
| 53 |
-
return
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
inputs = processor(images=pil_images, return_tensors="pt")
|
| 57 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 58 |
|
| 59 |
with torch.no_grad():
|
|
@@ -64,20 +67,11 @@ def analyze_video(video):
|
|
| 64 |
human_confidences = probs[:, human_class_index].tolist()
|
| 65 |
ai_confidences = [1 - h for h in human_confidences]
|
| 66 |
|
| 67 |
-
|
| 68 |
ai_frame_count = sum(1 for score in ai_confidences if score >= 0.5)
|
| 69 |
-
ai_frame_ratio = ai_frame_count /
|
| 70 |
-
avg_score = sum(ai_confidences) /
|
| 71 |
-
|
| 72 |
-
# Frame-by-frame report
|
| 73 |
-
results = []
|
| 74 |
-
for frame_idx, pred, ai_score in zip(frame_numbers, predictions, ai_confidences):
|
| 75 |
-
label = model.config.id2label[pred].lower()
|
| 76 |
-
label_str = "artificial" if pred == ai_class_index else "human"
|
| 77 |
-
confidence = ai_score * 100
|
| 78 |
-
results.append(f"Frame {frame_idx}: {label_str} (AI Confidence: {confidence:.1f}%)")
|
| 79 |
-
|
| 80 |
-
# Final verdict
|
| 81 |
if ai_frame_ratio < 0.25:
|
| 82 |
verdict = "β
Very likely real β no strong signs of AI generation."
|
| 83 |
elif ai_frame_ratio < 0.5:
|
|
@@ -89,17 +83,32 @@ def analyze_video(video):
|
|
| 89 |
else:
|
| 90 |
verdict = "π€ Unclear β the video shows mixed signals."
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Gradio interface
|
| 99 |
gr.Interface(
|
| 100 |
-
fn=
|
| 101 |
inputs=gr.Video(label="Upload a video"),
|
| 102 |
outputs=gr.Textbox(label="Detection Results"),
|
| 103 |
title="AI Frame Detector",
|
| 104 |
description="Analyzes sampled frames from a video to detect whether it's likely AI-generated or real."
|
| 105 |
-
).launch()
|
|
|
|
| 28 |
cap = cv2.VideoCapture(video)
|
| 29 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 30 |
if total_frames == 0:
|
| 31 |
+
return {
|
| 32 |
+
"error": "β Could not read video frames."
|
| 33 |
+
}
|
| 34 |
|
| 35 |
num_frames_to_process = min(200, total_frames)
|
| 36 |
frame_indices = [int(i * total_frames / num_frames_to_process) for i in range(num_frames_to_process)]
|
|
|
|
| 46 |
if frame_num == frame_indices[current_index]:
|
| 47 |
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 48 |
pil_image = Image.fromarray(rgb)
|
| 49 |
+
frames_to_process.append(pil_image)
|
| 50 |
current_index += 1
|
| 51 |
frame_num += 1
|
| 52 |
|
| 53 |
cap.release()
|
| 54 |
if not frames_to_process:
|
| 55 |
+
return {
|
| 56 |
+
"error": "β No frames extracted."
|
| 57 |
+
}
|
| 58 |
|
| 59 |
+
inputs = processor(images=frames_to_process, return_tensors="pt")
|
|
|
|
| 60 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 61 |
|
| 62 |
with torch.no_grad():
|
|
|
|
| 67 |
human_confidences = probs[:, human_class_index].tolist()
|
| 68 |
ai_confidences = [1 - h for h in human_confidences]
|
| 69 |
|
| 70 |
+
total = len(ai_confidences)
|
| 71 |
ai_frame_count = sum(1 for score in ai_confidences if score >= 0.5)
|
| 72 |
+
ai_frame_ratio = ai_frame_count / total
|
| 73 |
+
avg_score = sum(ai_confidences) / total
|
| 74 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if ai_frame_ratio < 0.25:
|
| 76 |
verdict = "β
Very likely real β no strong signs of AI generation."
|
| 77 |
elif ai_frame_ratio < 0.5:
|
|
|
|
| 83 |
else:
|
| 84 |
verdict = "π€ Unclear β the video shows mixed signals."
|
| 85 |
|
| 86 |
+
return {
|
| 87 |
+
"ai_frame_count": ai_frame_count,
|
| 88 |
+
"total_frames": total,
|
| 89 |
+
"ai_frame_ratio": round(ai_frame_ratio, 3),
|
| 90 |
+
"average_ai_confidence": round(avg_score, 3),
|
| 91 |
+
"verdict": verdict
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
# Optional: Pretty UI for humans
|
| 95 |
+
def analyze_video_ui(video):
|
| 96 |
+
summary = analyze_video(video)
|
| 97 |
+
if "error" in summary:
|
| 98 |
+
return summary["error"]
|
| 99 |
+
return (
|
| 100 |
+
f"Frames flagged as AI-generated: {summary['ai_frame_count']} "
|
| 101 |
+
f"out of {summary['total_frames']} "
|
| 102 |
+
f"({summary['ai_frame_ratio'] * 100:.1f}%)\n"
|
| 103 |
+
f"Average AI Confidence: {summary['average_ai_confidence'] * 100:.1f}%\n"
|
| 104 |
+
f"Final Verdict: {summary['verdict']}"
|
| 105 |
+
)
|
| 106 |
|
| 107 |
# Gradio interface
|
| 108 |
gr.Interface(
|
| 109 |
+
fn=analyze_video_ui,
|
| 110 |
inputs=gr.Video(label="Upload a video"),
|
| 111 |
outputs=gr.Textbox(label="Detection Results"),
|
| 112 |
title="AI Frame Detector",
|
| 113 |
description="Analyzes sampled frames from a video to detect whether it's likely AI-generated or real."
|
| 114 |
+
).launch()
|