Ii commited on
Commit
9dfaf16
·
verified ·
1 Parent(s): 27c452b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -31,13 +31,16 @@ 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):
@@ -63,7 +66,8 @@ def process_video(video_path, origins, destinations, thresholds, max_processes=2
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)
@@ -73,13 +77,11 @@ def process_video(video_path, origins, destinations, thresholds, max_processes=2
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
 
@@ -100,14 +102,14 @@ with gr.Blocks() as demo:
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
 
 
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):
 
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)
 
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
 
 
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