Apex-X commited on
Commit
3b87af8
·
verified ·
1 Parent(s): de461be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -23
app.py CHANGED
@@ -13,31 +13,63 @@ from roop.processors.frame.core import get_frame_processors_modules
13
  from roop.utilities import normalize_output_path
14
  import os
15
  from PIL import Image
 
 
16
 
17
 
18
- def swap_face(source_file, target_file,doFaceEnhancer):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- source_path = "input.jpg"
21
- target_path = "target.jpg"
22
 
23
- source_image = Image.fromarray(source_file)
24
- source_image.save(source_path)
25
- target_image = Image.fromarray(target_file)
26
- target_image.save(target_path)
27
 
28
- print("source_path: ", source_path)
29
- print("target_path: ", target_path)
 
 
 
 
30
 
31
- roop.globals.source_path = source_path
32
- roop.globals.target_path = target_path
33
- output_path = "output.jpg"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  roop.globals.output_path = normalize_output_path(
35
  roop.globals.source_path, roop.globals.target_path, output_path
36
  )
37
- if doFaceEnhancer == True:
38
- roop.globals.frame_processors = ["face_swapper","face_enhancer"]
 
39
  else:
40
  roop.globals.frame_processors = ["face_swapper"]
 
41
  roop.globals.headless = True
42
  roop.globals.keep_fps = True
43
  roop.globals.keep_audio = True
@@ -49,24 +81,25 @@ def swap_face(source_file, target_file,doFaceEnhancer):
49
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
50
  roop.globals.execution_threads = suggest_execution_threads()
51
 
52
- print(
53
- "start process",
54
- roop.globals.source_path,
55
- roop.globals.target_path,
56
- roop.globals.output_path,
57
- )
58
-
59
  for frame_processor in get_frame_processors_modules(
60
  roop.globals.frame_processors
61
  ):
62
  if not frame_processor.pre_check():
63
- return
64
 
 
 
 
65
  start()
 
 
66
  return output_path
67
 
68
 
69
  app = gr.Interface(
70
- fn=swap_face, inputs=[gr.Image(), gr.Image(),gr.Checkbox(label="face_enhancer?", info="do face enhancer?")], outputs="image"
 
 
71
  )
72
  app.launch()
 
13
  from roop.utilities import normalize_output_path
14
  import os
15
  from PIL import Image
16
+ import cv2 # OpenCV for video processing
17
+ import tempfile
18
 
19
 
20
+ def save_video_frames(video_path, folder):
21
+ """Extract frames from video and save to folder"""
22
+ cap = cv2.VideoCapture(video_path)
23
+ frame_paths = []
24
+ idx = 0
25
+ while True:
26
+ ret, frame = cap.read()
27
+ if not ret:
28
+ break
29
+ frame_path = os.path.join(folder, f"frame_{idx:05d}.png")
30
+ cv2.imwrite(frame_path, frame)
31
+ frame_paths.append(frame_path)
32
+ idx += 1
33
+ cap.release()
34
+ return frame_paths
35
 
 
 
36
 
37
+ def swap_face(source_file, target_file, doFaceEnhancer):
38
+ # source_file and target_file are paths to video files
 
 
39
 
40
+ # Create temp folders to extract frames
41
+ temp_dir = tempfile.mkdtemp()
42
+ source_frames_dir = os.path.join(temp_dir, "source_frames")
43
+ target_frames_dir = os.path.join(temp_dir, "target_frames")
44
+ os.makedirs(source_frames_dir, exist_ok=True)
45
+ os.makedirs(target_frames_dir, exist_ok=True)
46
 
47
+ # Extract frames from source and target videos
48
+ source_frame_paths = save_video_frames(source_file, source_frames_dir)
49
+ target_frame_paths = save_video_frames(target_file, target_frames_dir)
50
+
51
+ # For demo, assume same number of frames (more advanced syncing may be needed)
52
+ # Save first frames as example input images for roop (or process each frame one by one)
53
+ source_img_path = os.path.join(temp_dir, "input.jpg")
54
+ target_img_path = os.path.join(temp_dir, "target.jpg")
55
+ first_source_frame = cv2.imread(source_frame_paths[0])
56
+ first_target_frame = cv2.imread(target_frame_paths[0])
57
+ cv2.imwrite(source_img_path, first_source_frame)
58
+ cv2.imwrite(target_img_path, first_target_frame)
59
+
60
+ roop.globals.source_path = source_img_path
61
+ roop.globals.target_path = target_img_path
62
+
63
+ output_path = os.path.join(temp_dir, "output_video.mp4")
64
  roop.globals.output_path = normalize_output_path(
65
  roop.globals.source_path, roop.globals.target_path, output_path
66
  )
67
+
68
+ if doFaceEnhancer:
69
+ roop.globals.frame_processors = ["face_swapper", "face_enhancer"]
70
  else:
71
  roop.globals.frame_processors = ["face_swapper"]
72
+
73
  roop.globals.headless = True
74
  roop.globals.keep_fps = True
75
  roop.globals.keep_audio = True
 
81
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
82
  roop.globals.execution_threads = suggest_execution_threads()
83
 
84
+ # Check frame processors before start
 
 
 
 
 
 
85
  for frame_processor in get_frame_processors_modules(
86
  roop.globals.frame_processors
87
  ):
88
  if not frame_processor.pre_check():
89
+ return None
90
 
91
+ # Here you would ideally call start() and it processes the entire video,
92
+ # but since roop's start() expects images, you might need to adapt it
93
+ # to work frame by frame or modify roop internals accordingly.
94
  start()
95
+
96
+ # Return output video path to Gradio to play/download
97
  return output_path
98
 
99
 
100
  app = gr.Interface(
101
+ fn=swap_face,
102
+ inputs=[gr.Video(label="Source Video"), gr.Video(label="Target Video"), gr.Checkbox(label="Face Enhancer?")],
103
+ outputs=gr.Video(label="Output Video"),
104
  )
105
  app.launch()