File size: 1,802 Bytes
dc4f473
1dbeab5
 
93dd3e3
 
1dbeab5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93dd3e3
1dbeab5
93dd3e3
 
 
1dbeab5
 
 
93dd3e3
1dbeab5
 
 
93dd3e3
 
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
52
53
54
55
56
57
58
import streamlit as st
import yt_dlp
import torch
from transformers import pipeline

st.set_page_config(page_title="Video Deepfake Detector", layout="centered")

# Download video using yt-dlp
def download_video(video_url, output_path="video.mp4"):
    ydl_opts = {
        'format': 'best',
        'outtmpl': output_path,
        'quiet': True,
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([video_url])
    return output_path

# Load the deepfake detection model
def load_model():
    return pipeline("image-classification", model="microsoft/resnet-50")

# Analyze video frames for deepfake detection
def analyze_video(video_path, model):
    import cv2
    cap = cv2.VideoCapture(video_path)
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    step = max(frame_count // 5, 1)
    results = []
    
    for i in range(0, frame_count, step):
        cap.set(cv2.CAP_PROP_POS_FRAMES, i)
        ret, frame = cap.read()
        if not ret:
            continue
        rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        prediction = model(rgb_frame)
        results.append(prediction[0])
    cap.release()
    return results

# Streamlit interface
st.title("🎥 Video Deepfake Detector")
video_url = st.text_input("Enter YouTube Video URL:")

if st.button("Submit") and video_url:
    with st.spinner("Downloading and analyzing video..."):
        try:
            video_path = download_video(video_url)
            model = load_model()
            predictions = analyze_video(video_path, model)

            st.success("Analysis Complete!")
            for idx, pred in enumerate(predictions):
                st.write(f"Frame {idx + 1}: {pred['label']} with confidence {pred['score']:.2f}")
        except Exception as e:
            st.error(f"Error: {e}")