Ii commited on
Commit
7157f11
·
verified ·
1 Parent(s): 7e981ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -22
app.py CHANGED
@@ -37,36 +37,39 @@ 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)
61
-
62
- # Convert the refaced video to bytes
63
- with open(refaced_video_path, "rb") as f:
64
- video_bytes = f.read()
65
-
66
- # Delete the temporary video file
67
- os.remove(refaced_video_path)
68
 
69
- return video_bytes # Return the video bytes for Gradio output
 
 
 
 
 
70
 
71
  # Prepare Gradio components
72
  origin = []
@@ -77,22 +80,22 @@ with gr.Blocks() as demo:
77
  with gr.Row():
78
  gr.Markdown("# Refacer")
79
  with gr.Row():
80
- video = gr.Video(label="Original video", format="mp4")
81
- video2 = gr.Video(label="Refaced video", interactive=False, format="mp4") # Gradio UI only
82
 
83
- for i in range(0, num_faces):
84
  with gr.Tab(f"Face #{i+1}"):
85
  with gr.Row():
86
- origin.append(gr.Image(label="Face to replace"))
87
- destination.append(gr.Image(label="Destination face"))
88
  with gr.Row():
89
  thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
90
 
91
  with gr.Row():
92
  button = gr.Button("Reface", variant="primary")
93
 
94
- # Connect the button to the run function
95
  button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
96
 
97
- # Launch the Gradio app
98
  demo.queue().launch(show_error=True, share=args.share_gradio, server_name="0.0.0.0", server_port=args.server_port)
 
37
 
38
  # Initialize the Refacer class
39
  refacer = Refacer(force_cpu=args.force_cpu, colab_performance=args.colab_performance)
 
40
  num_faces = args.max_num_faces
41
 
42
+ # Function to process refacing
43
  def run(*vars):
44
+ video_path = vars[0] # Original video path
45
+ if not video_path:
46
+ raise ValueError("Video file not found. Please upload a valid video.")
47
+
48
  origins = vars[1:(num_faces+1)]
49
  destinations = vars[(num_faces+1):(num_faces*2)+1]
50
  thresholds = vars[(num_faces*2)+1:]
51
 
52
  faces = []
53
+ for k in range(num_faces):
54
+ if origins[k] and destinations[k]:
55
  faces.append({
56
  'origin': origins[k],
57
  'destination': destinations[k],
58
  'threshold': thresholds[k]
59
  })
60
 
61
+ try:
62
+ # Call refacer to process video and return bytes
63
+ refaced_video_path = refacer.reface(video_path, faces)
64
+ with open(refaced_video_path, "rb") as f:
65
+ video_bytes = f.read()
 
 
 
 
66
 
67
+ # Cleanup temporary file
68
+ os.remove(refaced_video_path)
69
+ return video_bytes
70
+ except Exception as e:
71
+ print(f"Error during refacing: {e}")
72
+ raise RuntimeError("Video refacing failed. Check logs.")
73
 
74
  # Prepare Gradio components
75
  origin = []
 
80
  with gr.Row():
81
  gr.Markdown("# Refacer")
82
  with gr.Row():
83
+ video = gr.Video(label="Original Video", format="mp4")
84
+ video2 = gr.Video(label="Refaced Video", interactive=False, format="mp4")
85
 
86
+ for i in range(num_faces):
87
  with gr.Tab(f"Face #{i+1}"):
88
  with gr.Row():
89
+ origin.append(gr.Image(label="Face to Replace"))
90
+ destination.append(gr.Image(label="Destination Face"))
91
  with gr.Row():
92
  thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
93
 
94
  with gr.Row():
95
  button = gr.Button("Reface", variant="primary")
96
 
97
+ # Configure button click
98
  button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
99
 
100
+ # Launch Gradio app
101
  demo.queue().launch(show_error=True, share=args.share_gradio, server_name="0.0.0.0", server_port=args.server_port)