Ii commited on
Commit
f7a3e0f
·
verified ·
1 Parent(s): 62a41be

Delete app.py.txt

Browse files
Files changed (1) hide show
  1. app.py.txt +0 -117
app.py.txt DELETED
@@ -1,117 +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
- # Simulate face swapping or any processing needed
35
- result_frame = refacer.reface(frame, [{
36
- 'origin': origin_face,
37
- 'destination': destination_face,
38
- 'threshold': threshold
39
- }])
40
- return result_frame
41
-
42
- # Function to process the video in parallel using multiprocessing
43
- def process_video(video_path, origins, destinations, thresholds, max_processes=2):
44
- cap = cv2.VideoCapture(video_path)
45
- frames = []
46
-
47
- # Read all frames from the video
48
- while cap.isOpened():
49
- ret, frame = cap.read()
50
- if not ret:
51
- break
52
- frames.append(frame)
53
-
54
- cap.release()
55
-
56
- # Parallel processing of frames with limited processes (for CPU optimization)
57
- with multiprocessing.Pool(processes=max_processes) as pool:
58
- processed_frames = pool.starmap(process_frame, [
59
- (frame, origins[min(i, len(origins) - 1)], destinations[min(i, len(destinations) - 1)], thresholds[min(i, len(thresholds) - 1)])
60
- for i, frame in enumerate(frames)
61
- ])
62
-
63
- # Saving the processed frames back into a video
64
- output_video_path = "processed_video.mp4"
65
- fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Compression using mp4 codec
66
- out = cv2.VideoWriter(output_video_path, fourcc, 30.0, (640, 360)) # Reduce resolution to speed up processing
67
-
68
- for frame in processed_frames:
69
- out.write(frame)
70
-
71
- out.release()
72
- return output_video_path
73
-
74
- # Gradio Interface function
75
- def run(video_path, *vars):
76
- # Split the inputs into origins, destinations, and thresholds based on num_faces
77
- num_faces = 5 # You can adjust this based on your UI
78
- origins = vars[:num_faces]
79
- destinations = vars[num_faces:2*num_faces]
80
- thresholds = vars[2*num_faces:]
81
-
82
- # Ensure there are no index errors by limiting the number of inputs
83
- if len(origins) != num_faces or len(destinations) != num_faces or len(thresholds) != num_faces:
84
- return "Please provide input for all faces."
85
-
86
- refaced_video_path = process_video(video_path, origins, destinations, thresholds)
87
- print(f"Refaced video can be found at {refaced_video_path}")
88
-
89
- return refaced_video_path
90
-
91
- # Prepare Gradio components
92
- origin = []
93
- destination = []
94
- thresholds = []
95
-
96
- with gr.Blocks() as demo:
97
- with gr.Row():
98
- gr.Markdown("# Refacer")
99
- with gr.Row():
100
- video_input = gr.Video(label="Original video", format="mp4")
101
- video_output = gr.Video(label="Refaced video", interactive=False, format="mp4")
102
-
103
- for i in range(5): # Set max faces to 5
104
- with gr.Tab(f"Face #{i+1}"):
105
- with gr.Row():
106
- origin.append(gr.Image(label="Face to replace"))
107
- destination.append(gr.Image(label="Destination face"))
108
- with gr.Row():
109
- thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
110
-
111
- with gr.Row():
112
- button = gr.Button("Reface", variant="primary")
113
-
114
- button.click(fn=run, inputs=[video_input] + origin + destination + thresholds, outputs=[video_output])
115
-
116
- # Launch the Gradio app
117
- demo.launch(show_error=True, server_name="0.0.0.0", server_port=7860)