|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
import io |
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
model_path = hf_hub_download("dima806/deepfake_vs_real_image_detection", "model.pkl") |
|
|
|
|
|
import pickle |
|
with open(model_path, 'rb') as f: |
|
model = pickle.load(f) |
|
|
|
def process_frame(frame): |
|
|
|
|
|
result = model.predict(np.array(frame)) |
|
return result |
|
|
|
def process_video(video_file): |
|
video = video_file.read() |
|
|
|
|
|
with Image.open(io.BytesIO(video)) as img: |
|
|
|
if hasattr(img, 'n_frames') and img.n_frames > 1: |
|
results = [] |
|
for frame in range(img.n_frames): |
|
img.seek(frame) |
|
frame_result = process_frame(img.convert('RGB')) |
|
results.append(frame_result) |
|
else: |
|
|
|
results = [process_frame(img.convert('RGB'))] |
|
|
|
|
|
final_result = analyze_results(results) |
|
|
|
return final_result |
|
|
|
def analyze_results(results): |
|
|
|
|
|
return np.mean(results) |
|
|
|
iface = gr.Interface( |
|
fn=process_video, |
|
inputs=gr.File(label="Upload Video"), |
|
outputs=gr.Text(label="Deepfake Detection Result") |
|
) |
|
|
|
iface.launch() |