Ii commited on
Commit
98c1e0d
·
verified ·
1 Parent(s): 2c8ca5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -39
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from refacer import Refacer
 
3
  import os
4
  import requests
5
 
@@ -24,60 +25,69 @@ def download_model():
24
  # Download the model when the script runs
25
  download_model()
26
 
 
 
 
 
 
 
 
 
 
 
27
  # Initialize the Refacer class
28
- refacer = Refacer(force_cpu=False, colab_performance=False)
29
 
30
- # Number of faces to handle
31
- num_faces = 5
32
 
33
  # Run function for refacing video
34
- def run(video_path, *inputs):
35
- origins = inputs[:num_faces]
36
- destinations = inputs[num_faces:2*num_faces]
37
- thresholds = inputs[2*num_faces:]
 
38
 
39
  faces = []
40
- for i in range(num_faces):
41
- if origins[i] is not None and destinations[i] is not None:
42
  faces.append({
43
- "origin": origins[i],
44
- "destination": destinations[i],
45
- "threshold": thresholds[i]
46
  })
47
 
48
- # Perform refacing
49
- refaced_video_path = refacer.reface(video_path, faces)
50
- return refaced_video_path # Return the path of the refaced video
51
 
52
- # Define Gradio UI
53
- with gr.Blocks() as demo:
54
- gr.Markdown("# Refacer - Replace Faces in Videos")
 
 
 
 
55
 
56
- # Input and output components
 
 
57
  with gr.Row():
58
- input_video = gr.Video(label="Original Video", format="mp4")
59
- output_video = gr.Video(label="Refaced Video", interactive=False, format="mp4")
60
 
61
- # Face replacement tabs
62
- origins = []
63
- destinations = []
64
- thresholds = []
65
- for i in range(num_faces):
66
  with gr.Tab(f"Face #{i+1}"):
67
- origins.append(gr.Image(label="Origin Face"))
68
- destinations.append(gr.Image(label="Destination Face"))
69
- thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
70
-
71
- # Reface button
 
72
  with gr.Row():
73
- reface_button = gr.Button("Reface")
74
 
75
- # Connect the function to the Gradio UI
76
- reface_button.click(
77
- fn=run,
78
- inputs=[input_video] + origins + destinations + thresholds,
79
- outputs=output_video
80
- )
81
 
82
  # Launch the Gradio app
83
- demo.queue().launch(share=True)
 
1
  import gradio as gr
2
  from refacer import Refacer
3
+ import argparse
4
  import os
5
  import requests
6
 
 
25
  # Download the model when the script runs
26
  download_model()
27
 
28
+ # Argument parser
29
+ parser = argparse.ArgumentParser(description='Refacer')
30
+ parser.add_argument("--max_num_faces", type=int, help="Max number of faces on UI", default=5)
31
+ parser.add_argument("--force_cpu", help="Force CPU mode", default=False, action="store_true")
32
+ parser.add_argument("--share_gradio", help="Share Gradio", default=False, action="store_true")
33
+ parser.add_argument("--server_name", type=str, help="Server IP address", default="127.0.0.1")
34
+ parser.add_argument("--server_port", type=int, help="Server port", default=7860)
35
+ parser.add_argument("--colab_performance", help="Use in colab for better performance", default=False, action="store_true")
36
+ args = parser.parse_args()
37
+
38
  # Initialize the Refacer class
39
+ refacer = Refacer(force_cpu=args.force_cpu, colab_performance=args.colab_performance)
40
 
41
+ num_faces = args.max_num_faces
 
42
 
43
  # Run function for refacing video
44
+ def run(*vars):
45
+ video_path = vars[0]
46
+ origins = vars[1:(num_faces+1)]
47
+ destinations = vars[(num_faces+1):(num_faces*2)+1]
48
+ thresholds = vars[(num_faces*2)+1:]
49
 
50
  faces = []
51
+ for k in range(0, num_faces):
52
+ if origins[k] is not None and destinations[k] is not None:
53
  faces.append({
54
+ 'origin': origins[k],
55
+ 'destination': destinations[k],
56
+ 'threshold': thresholds[k]
57
  })
58
 
59
+ # Call refacer to process video and get file path
60
+ refaced_video_path = refacer.reface(video_path, faces) # refaced video path
61
+ print(f"Refaced video can be found at {refaced_video_path}")
62
 
63
+ # Returning the video file path directly for Gradio to show it
64
+ return refaced_video_path # Gradio will handle it and show directly in the UI
65
+
66
+ # Prepare Gradio components
67
+ origin = []
68
+ destination = []
69
+ thresholds = []
70
 
71
+ with gr.Blocks() as demo:
72
+ with gr.Row():
73
+ gr.Markdown("# Refacer")
74
  with gr.Row():
75
+ video = gr.Video(label="Original video", format="mp4")
76
+ video2 = gr.Video(label="Refaced video", interactive=False, format="mp4")
77
 
78
+ for i in range(0, num_faces):
 
 
 
 
79
  with gr.Tab(f"Face #{i+1}"):
80
+ with gr.Row():
81
+ origin.append(gr.Image(label="Face to replace"))
82
+ destination.append(gr.Image(label="Destination face"))
83
+ with gr.Row():
84
+ thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
85
+
86
  with gr.Row():
87
+ button = gr.Button("Reface", variant="primary")
88
 
89
+ # Click event: Refacing the video and showing the refaced video in Gradio
90
+ button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
 
 
 
 
91
 
92
  # Launch the Gradio app
93
+ demo.queue().launch(show_error=True, share=args.share_gradio, server_name="0.0.0.0", server_port=args.server_port)