Video-Face-Swap / app.py
yoshibomball123's picture
Uploaded app.py
0c52219 verified
raw
history blame
1.61 kB
import os
import gradio as gr
import torch
from transformers import pipeline
import cv2
# Load Hugging Face face swap model using the DeepFaceLab model by senhan007
face_swap_model = pipeline("image-to-image", model="senhan007/DeepFaceLab")
def swap_faces(image, video):
image_path = "uploaded_image.jpg"
video_path = "uploaded_video.mp4"
output_path = "swapped_video.mp4"
# Save uploaded files
image.save(image_path)
video.save(video_path)
# Open the video file
video_cap = cv2.VideoCapture(video_path)
frame_rate = video_cap.get(cv2.CAP_PROP_FPS)
width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_video = cv2.VideoWriter(output_path, fourcc, frame_rate, (width, height))
while True:
ret, frame = video_cap.read()
if not ret:
break
# Apply the face swap (replace with model logic)
# For now, this is just a placeholder; you'll need to integrate the model inference here.
swapped_frame = frame
output_video.write(swapped_frame)
video_cap.release()
output_video.release()
return output_path
# Create the Gradio interface
iface = gr.Interface(
fn=swap_faces,
inputs=[
gr.Image(type="pil", label="Upload Reference Image"),
gr.Video(type="file", label="Upload Video"),
],
outputs=gr.Video(label="Face Swapped Video"),
title="Video Face Swap",
description="Upload a reference image and a video to swap faces."
)
iface.launch()