Ii commited on
Commit
2c8ca5e
·
verified ·
1 Parent(s): 8671d12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -44
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  from refacer import Refacer
3
- import argparse
4
  import os
5
  import requests
6
 
@@ -25,65 +24,60 @@ def download_model():
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(video_path, *vars):
45
- origins = vars[:num_faces]
46
- destinations = vars[num_faces:2*num_faces]
47
- thresholds = vars[2*num_faces:]
48
 
49
  faces = []
50
- for k in range(num_faces):
51
- if origins[k] is not None and destinations[k] is not None:
52
  faces.append({
53
- 'origin': origins[k],
54
- 'destination': destinations[k],
55
- 'threshold': thresholds[k]
56
  })
57
 
58
- # Call refacer to process video
59
- refaced_video = refacer.reface(video_path, faces) # Directly returns the processed video as bytes
60
- return refaced_video # Return video content to display in Gradio UI
61
-
62
- # Prepare Gradio components
63
- origin = []
64
- destination = []
65
- thresholds = []
66
 
 
67
  with gr.Blocks() as demo:
 
 
 
68
  with gr.Row():
69
- gr.Markdown("# Refacer - Replace Faces in Video")
70
- with gr.Row():
71
- video = gr.Video(label="Original Video", format="mp4")
72
- video2 = gr.Video(label="Refaced Video", format="mp4", interactive=False)
73
 
 
 
 
 
74
  for i in range(num_faces):
75
  with gr.Tab(f"Face #{i+1}"):
76
- with gr.Row():
77
- origin.append(gr.Image(label="Face to replace"))
78
- destination.append(gr.Image(label="Destination face"))
79
- with gr.Row():
80
- thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
81
-
82
  with gr.Row():
83
- button = gr.Button("Reface", variant="primary")
84
 
85
- # Updated: Directly display the refaced video in the output
86
- button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
 
 
 
 
87
 
88
  # Launch the Gradio app
89
- demo.queue().launch(show_error=True, share=args.share_gradio, server_name="0.0.0.0", server_port=args.server_port)
 
1
  import gradio as gr
2
  from refacer import Refacer
 
3
  import os
4
  import requests
5
 
 
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)