Deepro Bardhan commited on
Commit
d68547b
·
1 Parent(s): 3881be4

DoneEpicChanges

Browse files
Files changed (2) hide show
  1. .gitignore +6 -14
  2. app.py +225 -42
.gitignore CHANGED
@@ -1,19 +1,11 @@
1
  venv310/
 
 
 
 
 
 
2
 
3
- MultiSrcMultiDst/*/*
4
- !MultiSrcMultiDst/
5
- !MultiSrcMultiDst/*/
6
 
7
- SingleSrcMultiDst/*/*
8
- !SingleSrcMultiDst/
9
- !SingleSrcMultiDst/*/
10
 
11
- SinglePhoto/*/*
12
- !SinglePhoto/
13
- !SinglePhoto/*/
14
 
15
- VideoSwapping/*/*
16
- !VideoSwapping/
17
- !VideoSwapping/*/
18
-
19
- __pycache__/*
 
1
  venv310/
2
+ SinglePhoto/
3
+ VideoSwapping/
4
+ MultiSrcSingleDst/
5
+ MultiSrcMultiDst/
6
+ SingleSrcMultiDst/
7
+ __pycache__/*
8
 
 
 
 
9
 
 
 
 
10
 
 
 
 
11
 
 
 
 
 
 
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import os
3
  import cv2
 
 
4
  from SinglePhoto import FaceSwapper
5
 
6
  wellcomingMessage = """
@@ -11,75 +13,231 @@ wellcomingMessage = """
11
  swapper = FaceSwapper()
12
 
13
  def swap_single_photo(src_img, src_idx, dst_img, dst_idx):
 
14
  try:
15
- src_path = "tmp_src.jpg"
16
- dst_path = "tmp_dst.jpg"
17
- cv2.imwrite(src_path, src_img)
18
- cv2.imwrite(dst_path, dst_img)
 
 
 
 
 
 
 
19
  result = swapper.swap_faces(src_path, int(src_idx), dst_path, int(dst_idx))
20
- return result
 
 
 
 
 
 
 
 
 
 
21
  except Exception as e:
22
- return f"Error: {e}"
 
23
 
24
  def swap_video(src_img, src_idx, video, dst_idx):
25
- # Save source image
26
- src_path = "tmp_src.jpg"
27
- cv2.imwrite(src_path, src_img)
28
- # Save video
29
- video_path = "tmp_video.mp4"
30
- with open(video_path, "wb") as f:
31
- f.write(video.read())
32
- # Extract frames
33
- frames_dir = "tmp_video_frames"
34
- swapped_dir = "tmp_swapped_frames"
35
- output_video_path = "tmp_output_video.mp4"
36
- from VideoSwapping import extract_frames, frames_to_video
37
- frame_paths = extract_frames(video_path, frames_dir)
38
  os.makedirs(swapped_dir, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  for idx, frame_path in enumerate(frame_paths):
40
  out_path = os.path.join(swapped_dir, f"swapped_{idx:05d}.jpg")
41
  try:
42
  swapped = swapper.swap_faces(src_path, int(src_idx), frame_path, int(dst_idx))
43
  cv2.imwrite(out_path, swapped)
44
- except Exception:
 
45
  cv2.imwrite(out_path, cv2.imread(frame_path))
46
- # Combine frames to video
47
- cap = cv2.VideoCapture(video_path)
48
  fps = cap.get(cv2.CAP_PROP_FPS)
49
  cap.release()
50
  frames_to_video(swapped_dir, output_video_path, fps)
51
- return output_video_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  def swap_multi_src_single_dst(src_imgs, dst_img, dst_idx):
54
- # src_imgs: list of images
55
  results = []
56
- dst_path = "tmp_dst.jpg"
57
- cv2.imwrite(dst_path, dst_img)
 
 
 
 
 
 
 
 
 
 
 
 
58
  for i, src_img in enumerate(src_imgs):
59
- src_path = f"tmp_src_{i}.jpg"
60
- cv2.imwrite(src_path, src_img)
 
 
 
 
 
61
  try:
62
  result = swapper.swap_faces(src_path, 1, dst_path, int(dst_idx))
63
- results.append(result)
 
 
64
  except Exception as e:
65
  results.append(f"Error: {e}")
66
- return results
 
67
 
68
  def swap_multi_src_multi_dst(src_imgs, dst_imgs, dst_indices):
69
- # src_imgs, dst_imgs: lists of images; dst_indices: list of indices
70
  results = []
 
 
 
 
 
 
 
 
 
 
 
 
71
  for i, src_img in enumerate(src_imgs):
72
- src_path = f"tmp_src_{i}.jpg"
73
- cv2.imwrite(src_path, src_img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  for j, dst_img in enumerate(dst_imgs):
75
- dst_path = f"tmp_dst_{j}.jpg"
76
- cv2.imwrite(dst_path, dst_img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  try:
78
- result = swapper.swap_faces(src_path, 1, dst_path, int(dst_indices[j]))
79
- results.append(result)
 
 
 
80
  except Exception as e:
81
  results.append(f"Error: {e}")
82
- return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  with gr.Blocks() as demo:
85
  gr.Markdown(wellcomingMessage)
@@ -92,7 +250,10 @@ with gr.Blocks() as demo:
92
  gr.Image(label="Destination Image"),
93
  gr.Number(value=1, label="Destination Face Index"),
94
  ],
95
- outputs=gr.Image(label="Swapped Image"),
 
 
 
96
  )
97
  with gr.Tab("Video Swapping"):
98
  gr.Interface(
@@ -103,7 +264,23 @@ with gr.Blocks() as demo:
103
  gr.Video(label="Target Video"),
104
  gr.Number(value=1, label="Destination Face Index"),
105
  ],
106
- outputs=gr.Video(label="Swapped Video"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  )
108
  with gr.Tab("MultiSrc SingleDst"):
109
  gr.Interface(
@@ -113,7 +290,10 @@ with gr.Blocks() as demo:
113
  gr.Image(label="Destination Image"),
114
  gr.Number(value=1, label="Destination Face Index"),
115
  ],
116
- outputs=gr.Gallery(label="Swapped Images"),
 
 
 
117
  )
118
  with gr.Tab("MultiSrc MultiDst"):
119
  gr.Interface(
@@ -123,7 +303,10 @@ with gr.Blocks() as demo:
123
  gr.Gallery(label="Destination Images", type="numpy", columns=3),
124
  gr.Textbox(label="Destination Face Indices (comma-separated, e.g. 1,1,2)"),
125
  ],
126
- outputs=gr.Gallery(label="Swapped Images"),
 
 
 
127
  )
128
 
129
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import os
3
  import cv2
4
+ import numpy as np
5
+ import shutil
6
  from SinglePhoto import FaceSwapper
7
 
8
  wellcomingMessage = """
 
13
  swapper = FaceSwapper()
14
 
15
  def swap_single_photo(src_img, src_idx, dst_img, dst_idx):
16
+ log = ""
17
  try:
18
+ src_path = "SinglePhoto/data_src.jpg"
19
+ dst_path = "SinglePhoto/data_dst.jpg"
20
+ output_path = "SinglePhoto/output_swapped.jpg"
21
+ os.makedirs(os.path.dirname(src_path), exist_ok=True)
22
+ os.makedirs(os.path.dirname(dst_path), exist_ok=True)
23
+ os.makedirs(os.path.dirname(output_path), exist_ok=True)
24
+ src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
25
+ dst_img_bgr = cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR)
26
+ cv2.imwrite(src_path, src_img_bgr)
27
+ cv2.imwrite(dst_path, dst_img_bgr)
28
+ log += f"Saved source to {src_path}, destination to {dst_path}\n"
29
  result = swapper.swap_faces(src_path, int(src_idx), dst_path, int(dst_idx))
30
+ cv2.imwrite(output_path, result)
31
+ log += f"Swapped and saved result to {output_path}\n"
32
+ try:
33
+ if os.path.exists(src_path):
34
+ os.remove(src_path)
35
+ if os.path.exists(dst_path):
36
+ os.remove(dst_path)
37
+ log += "Cleaned up temp files.\n"
38
+ except Exception as cleanup_error:
39
+ log += f"Cleanup error: {cleanup_error}\n"
40
+ return output_path, log
41
  except Exception as e:
42
+ log += f"Error: {e}\n"
43
+ return None, log
44
 
45
  def swap_video(src_img, src_idx, video, dst_idx):
46
+ log = ""
47
+ src_path = "VideoSwapping/data_src.jpg"
48
+ dst_video_path = "VideoSwapping/data_dst.mp4"
49
+ frames_dir = "VideoSwapping/video_frames"
50
+ swapped_dir = "VideoSwapping/swapped_frames"
51
+ output_video_path = "VideoSwapping/output_tmp_output_video.mp4"
52
+
53
+ os.makedirs(os.path.dirname(src_path), exist_ok=True)
54
+ os.makedirs(os.path.dirname(dst_video_path), exist_ok=True)
55
+ os.makedirs(frames_dir, exist_ok=True)
 
 
 
56
  os.makedirs(swapped_dir, exist_ok=True)
57
+
58
+ src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
59
+ cv2.imwrite(src_path, src_img_bgr)
60
+ log += f"Saved source image to {src_path}\n"
61
+
62
+ if isinstance(video, str) and os.path.exists(video):
63
+ shutil.copy(video, dst_video_path)
64
+ log += f"Copied video to {dst_video_path}\n"
65
+ else:
66
+ dst_video_path = video
67
+
68
+ from VideoSwapping import extract_frames, frames_to_video
69
+ frame_paths = extract_frames(dst_video_path, frames_dir)
70
+ log += f"Extracted {len(frame_paths)} frames to {frames_dir}\n"
71
  for idx, frame_path in enumerate(frame_paths):
72
  out_path = os.path.join(swapped_dir, f"swapped_{idx:05d}.jpg")
73
  try:
74
  swapped = swapper.swap_faces(src_path, int(src_idx), frame_path, int(dst_idx))
75
  cv2.imwrite(out_path, swapped)
76
+ log += f"Swapped frame {idx} and saved to {out_path}\n"
77
+ except Exception as e:
78
  cv2.imwrite(out_path, cv2.imread(frame_path))
79
+ log += f"Failed to swap frame {idx}: {e}\n"
80
+ cap = cv2.VideoCapture(dst_video_path)
81
  fps = cap.get(cv2.CAP_PROP_FPS)
82
  cap.release()
83
  frames_to_video(swapped_dir, output_video_path, fps)
84
+ log += f"Combined swapped frames into video {output_video_path}\n"
85
+
86
+ try:
87
+ if os.path.exists(src_path):
88
+ os.remove(src_path)
89
+ if os.path.exists(dst_video_path):
90
+ os.remove(dst_video_path)
91
+ if os.path.exists(frames_dir):
92
+ shutil.rmtree(frames_dir)
93
+ if os.path.exists(swapped_dir):
94
+ shutil.rmtree(swapped_dir)
95
+ log += "Cleaned up temp files and folders.\n"
96
+ except Exception as cleanup_error:
97
+ log += f"Cleanup error: {cleanup_error}\n"
98
+
99
+ return output_video_path, log
100
 
101
  def swap_multi_src_single_dst(src_imgs, dst_img, dst_idx):
102
+ log = ""
103
  results = []
104
+ src_dir = "MultiSrcSingleDst/src"
105
+ dst_dir = "MultiSrcSingleDst/dst"
106
+ output_dir = "MultiSrcSingleDst/output"
107
+ os.makedirs(src_dir, exist_ok=True)
108
+ os.makedirs(dst_dir, exist_ok=True)
109
+ os.makedirs(output_dir, exist_ok=True)
110
+
111
+ if isinstance(dst_img, tuple):
112
+ dst_img = dst_img[0]
113
+ dst_img_bgr = cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR)
114
+ dst_path = os.path.join(dst_dir, "data_dst.jpg")
115
+ cv2.imwrite(dst_path, dst_img_bgr)
116
+ log += f"Saved destination image to {dst_path}\n"
117
+
118
  for i, src_img in enumerate(src_imgs):
119
+ if isinstance(src_img, tuple):
120
+ src_img = src_img[0]
121
+ src_path = os.path.join(src_dir, f"data_src_{i}.jpg")
122
+ output_path = os.path.join(output_dir, f"output_swapped_{i}.jpg")
123
+ src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
124
+ cv2.imwrite(src_path, src_img_bgr)
125
+ log += f"Saved source image {i} to {src_path}\n"
126
  try:
127
  result = swapper.swap_faces(src_path, 1, dst_path, int(dst_idx))
128
+ cv2.imwrite(output_path, result)
129
+ results.append(output_path)
130
+ log += f"Swapped and saved result to {output_path}\n"
131
  except Exception as e:
132
  results.append(f"Error: {e}")
133
+ log += f"Error swapping source {i}: {e}\n"
134
+ return results, log
135
 
136
  def swap_multi_src_multi_dst(src_imgs, dst_imgs, dst_indices):
137
+ log = ""
138
  results = []
139
+ src_dir = "MultiSrcMultiDst/src"
140
+ dst_dir = "MultiSrcMultiDst/dst"
141
+ output_dir = "MultiSrcMultiDst/output"
142
+ os.makedirs(src_dir, exist_ok=True)
143
+ os.makedirs(dst_dir, exist_ok=True)
144
+ os.makedirs(output_dir, exist_ok=True)
145
+
146
+ if isinstance(dst_indices, str):
147
+ dst_indices_list = [int(idx.strip()) for idx in dst_indices.split(",") if idx.strip().isdigit()]
148
+ else:
149
+ dst_indices_list = [int(idx) for idx in dst_indices]
150
+
151
  for i, src_img in enumerate(src_imgs):
152
+ if isinstance(src_img, tuple):
153
+ src_img = src_img[0]
154
+ if src_img is None:
155
+ results.append(f"Error: Source image at index {i} is None")
156
+ log += f"Source image at index {i} is None\n"
157
+ continue
158
+ src_path = os.path.join(src_dir, f"data_src_{i}.jpg")
159
+ if isinstance(src_img, np.ndarray):
160
+ src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
161
+ cv2.imwrite(src_path, src_img_bgr)
162
+ log += f"Saved source image {i} to {src_path}\n"
163
+ elif isinstance(src_img, str) and os.path.exists(src_img):
164
+ shutil.copy(src_img, src_path)
165
+ log += f"Copied source image {i} from {src_img} to {src_path}\n"
166
+ else:
167
+ results.append(f"Error: Invalid source image at index {i}")
168
+ log += f"Invalid source image at index {i}\n"
169
+ continue
170
  for j, dst_img in enumerate(dst_imgs):
171
+ if isinstance(dst_img, tuple):
172
+ dst_img = dst_img[0]
173
+ if dst_img is None:
174
+ results.append(f"Error: Destination image at index {j} is None")
175
+ log += f"Destination image at index {j} is None\n"
176
+ continue
177
+ dst_path = os.path.join(dst_dir, f"data_dst_{j}.jpg")
178
+ output_path = os.path.join(output_dir, f"output_swapped_{i}_{j}.jpg")
179
+ if isinstance(dst_img, np.ndarray):
180
+ dst_img_bgr = cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR)
181
+ cv2.imwrite(dst_path, dst_img_bgr)
182
+ log += f"Saved destination image {j} to {dst_path}\n"
183
+ elif isinstance(dst_img, str) and os.path.exists(dst_img):
184
+ shutil.copy(dst_img, dst_path)
185
+ log += f"Copied destination image {j} from {dst_img} to {dst_path}\n"
186
+ else:
187
+ results.append(f"Error: Invalid destination image at index {j}")
188
+ log += f"Invalid destination image at index {j}\n"
189
+ continue
190
  try:
191
+ dst_idx = dst_indices_list[j] if j < len(dst_indices_list) else 1
192
+ result = swapper.swap_faces(src_path, 1, dst_path, int(dst_idx))
193
+ cv2.imwrite(output_path, result)
194
+ results.append(output_path)
195
+ log += f"Swapped src {i} with dst {j} and saved to {output_path}\n"
196
  except Exception as e:
197
  results.append(f"Error: {e}")
198
+ log += f"Error swapping src {i} with dst {j}: {e}\n"
199
+ return results, log
200
+
201
+ def swap_single_src_multi_dst(src_img, dst_imgs, dst_indices):
202
+ log = ""
203
+ results = []
204
+ src_dir = "SingleSrcMultiDst/src"
205
+ dst_dir = "SingleSrcMultiDst/dst"
206
+ output_dir = "SingleSrcMultiDst/output"
207
+ os.makedirs(src_dir, exist_ok=True)
208
+ os.makedirs(dst_dir, exist_ok=True)
209
+ os.makedirs(output_dir, exist_ok=True)
210
+
211
+ if isinstance(src_img, tuple):
212
+ src_img = src_img[0]
213
+ src_path = os.path.join(src_dir, "data_src.jpg")
214
+ src_img_bgr = cv2.cvtColor(src_img, cv2.COLOR_RGB2BGR)
215
+ cv2.imwrite(src_path, src_img_bgr)
216
+ log += f"Saved source image to {src_path}\n"
217
+
218
+ if isinstance(dst_indices, str):
219
+ dst_indices_list = [int(idx.strip()) for idx in dst_indices.split(",") if idx.strip().isdigit()]
220
+ else:
221
+ dst_indices_list = [int(idx) for idx in dst_indices]
222
+
223
+ for j, dst_img in enumerate(dst_imgs):
224
+ if isinstance(dst_img, tuple):
225
+ dst_img = dst_img[0]
226
+ dst_path = os.path.join(dst_dir, f"data_dst_{j}.jpg")
227
+ output_path = os.path.join(output_dir, f"output_swapped_{j}.jpg")
228
+ dst_img_bgr = cv2.cvtColor(dst_img, cv2.COLOR_RGB2BGR)
229
+ cv2.imwrite(dst_path, dst_img_bgr)
230
+ log += f"Saved destination image {j} to {dst_path}\n"
231
+ try:
232
+ dst_idx = dst_indices_list[j] if j < len(dst_indices_list) else 1
233
+ result = swapper.swap_faces(src_path, 1, dst_path, int(dst_idx))
234
+ cv2.imwrite(output_path, result)
235
+ results.append(output_path)
236
+ log += f"Swapped and saved result to {output_path}\n"
237
+ except Exception as e:
238
+ results.append(f"Error: {e}")
239
+ log += f"Error swapping with destination {j}: {e}\n"
240
+ return results, log
241
 
242
  with gr.Blocks() as demo:
243
  gr.Markdown(wellcomingMessage)
 
250
  gr.Image(label="Destination Image"),
251
  gr.Number(value=1, label="Destination Face Index"),
252
  ],
253
+ outputs=[
254
+ gr.Image(label="Swapped Image"),
255
+ gr.Textbox(label="Log Output", lines=8, interactive=False)
256
+ ],
257
  )
258
  with gr.Tab("Video Swapping"):
259
  gr.Interface(
 
264
  gr.Video(label="Target Video"),
265
  gr.Number(value=1, label="Destination Face Index"),
266
  ],
267
+ outputs=[
268
+ gr.Video(label="Swapped Video"),
269
+ gr.Textbox(label="Log Output", lines=8, interactive=False)
270
+ ],
271
+ )
272
+ with gr.Tab("SingleSrc MultiDst"):
273
+ gr.Interface(
274
+ fn=swap_single_src_multi_dst,
275
+ inputs=[
276
+ gr.Image(label="Source Image"),
277
+ gr.Gallery(label="Destination Images", type="numpy", columns=3),
278
+ gr.Textbox(label="Destination Face Indices (comma-separated, e.g. 1,1,2)"),
279
+ ],
280
+ outputs=[
281
+ gr.Gallery(label="Swapped Images"),
282
+ gr.Textbox(label="Log Output", lines=8, interactive=False)
283
+ ],
284
  )
285
  with gr.Tab("MultiSrc SingleDst"):
286
  gr.Interface(
 
290
  gr.Image(label="Destination Image"),
291
  gr.Number(value=1, label="Destination Face Index"),
292
  ],
293
+ outputs=[
294
+ gr.Gallery(label="Swapped Images"),
295
+ gr.Textbox(label="Log Output", lines=8, interactive=False)
296
+ ],
297
  )
298
  with gr.Tab("MultiSrc MultiDst"):
299
  gr.Interface(
 
303
  gr.Gallery(label="Destination Images", type="numpy", columns=3),
304
  gr.Textbox(label="Destination Face Indices (comma-separated, e.g. 1,1,2)"),
305
  ],
306
+ outputs=[
307
+ gr.Gallery(label="Swapped Images"),
308
+ gr.Textbox(label="Log Output", lines=8, interactive=False)
309
+ ],
310
  )
311
 
312
  if __name__ == "__main__":