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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -64
app.py CHANGED
@@ -1,5 +1,6 @@
1
- # -* coding:UTF-8 -*
2
  # !/usr/bin/env python
 
3
  import numpy as np
4
  import gradio as gr
5
  import roop.globals
@@ -13,63 +14,50 @@ 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
- 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,25 +69,36 @@ def swap_face(source_file, target_file, doFaceEnhancer):
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()
 
1
+ # -* coding:UTF-8 *-
2
  # !/usr/bin/env python
3
+
4
  import numpy as np
5
  import gradio as gr
6
  import roop.globals
 
14
  from roop.utilities import normalize_output_path
15
  import os
16
  from PIL import Image
17
+ import mimetypes
18
+
19
+
20
+ def swap_face(source_file, target_file, swap_type, doFaceEnhancer):
21
+ supported_images = ['.jpg', '.jpeg', '.png']
22
+ supported_videos = ['.mp4', '.mov', '.avi']
23
+
24
+ # Save source image
25
+ source_path = "input.jpg"
26
+ source_image = Image.fromarray(source_file)
27
+ source_image.save(source_path)
28
+
29
+ # Save target as image or video
30
+ mime_type, _ = mimetypes.guess_type(target_file.name)
31
+ ext = os.path.splitext(target_file.name)[1].lower()
32
+
33
+ if mime_type and "video" in mime_type:
34
+ target_path = "target" + ext
35
+ with open(target_path, "wb") as f:
36
+ f.write(target_file.read())
37
+ else:
38
+ target_path = "target.jpg"
39
+ target_image = Image.fromarray(target_file)
40
+ target_image.save(target_path)
41
+
42
+ print("source_path:", source_path)
43
+ print("target_path:", target_path)
44
+
45
+ roop.globals.source_path = source_path
46
+ roop.globals.target_path = target_path
47
+
48
+ # Output path based on type
49
+ if "video" in swap_type.lower() or ext in supported_videos:
50
+ output_path = "output.mp4"
51
+ else:
52
+ output_path = "output.jpg"
53
+
 
 
 
 
 
 
 
 
 
 
 
54
  roop.globals.output_path = normalize_output_path(
55
  roop.globals.source_path, roop.globals.target_path, output_path
56
  )
57
 
58
+ roop.globals.frame_processors = (
59
+ ["face_swapper", "face_enhancer"] if doFaceEnhancer else ["face_swapper"]
60
+ )
 
 
61
  roop.globals.headless = True
62
  roop.globals.keep_fps = True
63
  roop.globals.keep_audio = True
 
69
  roop.globals.execution_providers = decode_execution_providers(["cuda"])
70
  roop.globals.execution_threads = suggest_execution_threads()
71
 
72
+ print(
73
+ "start process",
74
+ roop.globals.source_path,
75
+ roop.globals.target_path,
76
+ roop.globals.output_path,
77
+ )
78
+
79
  for frame_processor in get_frame_processors_modules(
80
  roop.globals.frame_processors
81
  ):
82
  if not frame_processor.pre_check():
83
+ return "Frame processor pre-check failed"
84
 
 
 
 
85
  start()
86
+ return roop.globals.output_path
 
 
87
 
88
 
89
  app = gr.Interface(
90
  fn=swap_face,
91
+ inputs=[
92
+ gr.Image(label="Source Image"),
93
+ gr.File(label="Target (Image or Video)"),
94
+ gr.Radio(choices=["Image to Image", "Image to Video"], label="Swap Type"),
95
+ gr.Checkbox(label="Face Enhancer?", info="Apply face enhancement")
96
+ ],
97
+ outputs=[
98
+ gr.Image(label="Output Image") if not os.path.exists("output.mp4") else gr.Video(label="Output Video")
99
+ ],
100
+ title="Face Swap Tool (Roop)",
101
+ description="Swap face from source image to a target image or video using Roop.",
102
  )
103
+
104
  app.launch()