Ii
commited on
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,119 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
-
import multiprocessing
|
| 4 |
-
import os
|
| 5 |
-
import requests
|
| 6 |
-
from refacer import Refacer
|
| 7 |
-
|
| 8 |
-
# Hugging Face URL to download the model
|
| 9 |
-
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
| 10 |
-
model_path = "./inswapper_128.onnx"
|
| 11 |
-
|
| 12 |
-
# Function to download the model
|
| 13 |
-
def download_model():
|
| 14 |
-
if not os.path.exists(model_path):
|
| 15 |
-
print("Downloading inswapper_128.onnx...")
|
| 16 |
-
response = requests.get(model_url)
|
| 17 |
-
if response.status_code == 200:
|
| 18 |
-
with open(model_path, 'wb') as f:
|
| 19 |
-
f.write(response.content)
|
| 20 |
-
print("Model downloaded successfully!")
|
| 21 |
-
else:
|
| 22 |
-
raise Exception(f"Failed to download the model. Status code: {response.status_code}")
|
| 23 |
-
else:
|
| 24 |
-
print("Model already exists.")
|
| 25 |
-
|
| 26 |
-
# Download the model when the script runs
|
| 27 |
-
download_model()
|
| 28 |
-
|
| 29 |
-
# Initialize Refacer class (force CPU mode)
|
| 30 |
-
refacer = Refacer(force_cpu=True)
|
| 31 |
-
|
| 32 |
-
# Dummy function to simulate frame-level processing
|
| 33 |
-
def process_frame(frame, origin_face, destination_face, threshold):
|
| 34 |
-
try:
|
| 35 |
-
result_frame = refacer.reface(frame, [{
|
| 36 |
-
'origin': origin_face,
|
| 37 |
-
'destination': destination_face,
|
| 38 |
-
'threshold': threshold
|
| 39 |
-
}])
|
| 40 |
-
return result_frame
|
| 41 |
-
except Exception as e:
|
| 42 |
-
print(f"Error in processing frame: {e}")
|
| 43 |
-
return frame
|
| 44 |
-
|
| 45 |
-
# Function to process the video in parallel using multiprocessing
|
| 46 |
-
def process_video(video_path, origins, destinations, thresholds, max_processes=2):
|
| 47 |
-
cap = cv2.VideoCapture(video_path)
|
| 48 |
-
frames = []
|
| 49 |
-
|
| 50 |
-
# Read all frames from the video
|
| 51 |
-
while cap.isOpened():
|
| 52 |
-
ret, frame = cap.read()
|
| 53 |
-
if not ret:
|
| 54 |
-
break
|
| 55 |
-
frames.append(frame)
|
| 56 |
-
|
| 57 |
-
cap.release()
|
| 58 |
-
|
| 59 |
-
# Parallel processing of frames with limited processes (for CPU optimization)
|
| 60 |
-
with multiprocessing.Pool(processes=max_processes) as pool:
|
| 61 |
-
processed_frames = pool.starmap(process_frame, [
|
| 62 |
-
(frame, origins[min(i, len(origins) - 1)], destinations[min(i, len(destinations) - 1)], thresholds[min(i, len(thresholds) - 1)])
|
| 63 |
-
for i, frame in enumerate(frames)
|
| 64 |
-
])
|
| 65 |
-
|
| 66 |
-
# Saving the processed frames back into a video
|
| 67 |
-
output_video_path = "processed_video.mp4"
|
| 68 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Compression using mp4 codec
|
| 69 |
-
height, width, _ = frames[0].shape
|
| 70 |
-
out = cv2.VideoWriter(output_video_path, fourcc, 30.0, (width, height))
|
| 71 |
-
|
| 72 |
-
for frame in processed_frames:
|
| 73 |
-
out.write(frame)
|
| 74 |
-
|
| 75 |
-
out.release()
|
| 76 |
-
return output_video_path
|
| 77 |
-
|
| 78 |
-
# Gradio Interface function
|
| 79 |
-
def run(video_path, *vars):
|
| 80 |
-
num_faces = 5
|
| 81 |
-
origins = vars[:num_faces]
|
| 82 |
-
destinations = vars[num_faces:2*num_faces]
|
| 83 |
-
thresholds = vars[2*num_faces:]
|
| 84 |
-
|
| 85 |
-
if len(origins) != num_faces or len(destinations) != num_faces or len(thresholds) != num_faces:
|
| 86 |
-
return "Please provide input for all faces."
|
| 87 |
-
|
| 88 |
-
refaced_video_path = process_video(video_path, origins, destinations, thresholds)
|
| 89 |
-
print(f"Refaced video can be found at {refaced_video_path}")
|
| 90 |
-
|
| 91 |
-
return refaced_video_path
|
| 92 |
-
|
| 93 |
-
# Prepare Gradio components
|
| 94 |
-
origin = []
|
| 95 |
-
destination = []
|
| 96 |
-
thresholds = []
|
| 97 |
-
|
| 98 |
-
with gr.Blocks() as demo:
|
| 99 |
-
with gr.Row():
|
| 100 |
-
gr.Markdown("# Refacer")
|
| 101 |
-
with gr.Row():
|
| 102 |
-
video_input = gr.Video(label="Original video", format="mp4")
|
| 103 |
-
video_output = gr.Video(label="Refaced video", interactive=False, format="mp4")
|
| 104 |
-
|
| 105 |
-
for i in range(5):
|
| 106 |
-
with gr.Tab(f"Face #{i+1}"):
|
| 107 |
-
with gr.Row():
|
| 108 |
-
origin.append(gr.Image(label="Face to replace"))
|
| 109 |
-
destination.append(gr.Image(label="Destination face"))
|
| 110 |
-
with gr.Row():
|
| 111 |
-
thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
|
| 112 |
-
|
| 113 |
-
with gr.Row():
|
| 114 |
-
button = gr.Button("Reface", variant="primary")
|
| 115 |
-
|
| 116 |
-
button.click(fn=run, inputs=[video_input] + origin + destination + thresholds, outputs=[video_output])
|
| 117 |
-
|
| 118 |
-
# Launch the Gradio app
|
| 119 |
-
demo.launch(show_error=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|