Spaces:
Running
Running
Deepro Bardhan
commited on
Commit
·
abb6264
1
Parent(s):
d68547b
deleted unused files
Browse files- MultiSrcMultiDst.py +0 -59
- SingleSrcMultiDst.py +0 -70
- VideoSwapping.py +0 -168
MultiSrcMultiDst.py
DELETED
|
@@ -1,59 +0,0 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import os
|
| 3 |
-
from SinglePhoto import FaceSwapper # Updated import
|
| 4 |
-
|
| 5 |
-
def main():
|
| 6 |
-
src_dir = os.path.join("MultiSrcMultiDst", "src")
|
| 7 |
-
dst_dir = os.path.join("MultiSrcMultiDst", "dst")
|
| 8 |
-
output_root = os.path.join("MultiSrcMultiDst", "output")
|
| 9 |
-
|
| 10 |
-
os.makedirs(output_root, exist_ok=True)
|
| 11 |
-
|
| 12 |
-
swapper = FaceSwapper()
|
| 13 |
-
|
| 14 |
-
# Get all source and destination image paths
|
| 15 |
-
source_images = [os.path.join(src_dir, f) for f in os.listdir(src_dir) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
|
| 16 |
-
dest_images = [os.path.join(dst_dir, f) for f in os.listdir(dst_dir) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
|
| 17 |
-
|
| 18 |
-
# Ask user for target_face_idx for each destination image
|
| 19 |
-
dest_face_indices = {}
|
| 20 |
-
for dest_path in dest_images:
|
| 21 |
-
dest_name = os.path.basename(dest_path)
|
| 22 |
-
while True:
|
| 23 |
-
try:
|
| 24 |
-
idx_str = input(f"Enter target_face_idx for destination image '{dest_name}' (default 1): ")
|
| 25 |
-
if idx_str.strip() == "":
|
| 26 |
-
idx = 1
|
| 27 |
-
else:
|
| 28 |
-
idx = int(idx_str)
|
| 29 |
-
if idx < 1:
|
| 30 |
-
print("Index must be >= 1.")
|
| 31 |
-
continue
|
| 32 |
-
dest_face_indices[dest_path] = idx
|
| 33 |
-
break
|
| 34 |
-
except ValueError:
|
| 35 |
-
print("Please enter a valid integer.")
|
| 36 |
-
|
| 37 |
-
for source_path in source_images:
|
| 38 |
-
source_name = os.path.splitext(os.path.basename(source_path))[0]
|
| 39 |
-
source_output_dir = os.path.join(output_root, source_name)
|
| 40 |
-
os.makedirs(source_output_dir, exist_ok=True)
|
| 41 |
-
|
| 42 |
-
for dest_path in dest_images:
|
| 43 |
-
dest_name = os.path.splitext(os.path.basename(dest_path))[0]
|
| 44 |
-
target_face_idx = dest_face_indices[dest_path]
|
| 45 |
-
try:
|
| 46 |
-
result = swapper.swap_faces(
|
| 47 |
-
source_path=source_path,
|
| 48 |
-
source_face_idx=1,
|
| 49 |
-
target_path=dest_path,
|
| 50 |
-
target_face_idx=target_face_idx
|
| 51 |
-
)
|
| 52 |
-
output_path = os.path.join(source_output_dir, f"{source_name}_to_{dest_name}.jpg")
|
| 53 |
-
cv2.imwrite(output_path, result)
|
| 54 |
-
print(f"Swapped {source_name} -> {dest_name}: {output_path}")
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"Error swapping {source_name} to {dest_name}: {str(e)}")
|
| 57 |
-
|
| 58 |
-
if __name__ == "__main__":
|
| 59 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SingleSrcMultiDst.py
DELETED
|
@@ -1,70 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
from SinglePhoto import FaceSwapper # Use the shared class
|
| 4 |
-
|
| 5 |
-
def main():
|
| 6 |
-
base_folder = "SingleSrcMultiDst"
|
| 7 |
-
dst_folder = os.path.join(base_folder, "dst")
|
| 8 |
-
output_folder = os.path.join(base_folder, "output")
|
| 9 |
-
source_img_name = "data_src.jpg" # Change as needed
|
| 10 |
-
|
| 11 |
-
if not os.path.exists(output_folder):
|
| 12 |
-
os.makedirs(output_folder)
|
| 13 |
-
|
| 14 |
-
source_img_path = os.path.join(base_folder, source_img_name)
|
| 15 |
-
if not os.path.exists(source_img_path):
|
| 16 |
-
print(f"Could not find source image: {source_img_path}")
|
| 17 |
-
return
|
| 18 |
-
|
| 19 |
-
swapper = FaceSwapper()
|
| 20 |
-
|
| 21 |
-
# Ask user for source_face_idx (default 1)
|
| 22 |
-
try:
|
| 23 |
-
user_input = input("Enter the source face index (starting from 1, default is 1): ")
|
| 24 |
-
source_face_idx = int(user_input) if user_input.strip() else 1
|
| 25 |
-
if source_face_idx < 1:
|
| 26 |
-
print("Invalid index. Using default value 1.")
|
| 27 |
-
source_face_idx = 1
|
| 28 |
-
except ValueError:
|
| 29 |
-
print("Invalid input. Using default value 1.")
|
| 30 |
-
source_face_idx = 1
|
| 31 |
-
|
| 32 |
-
# Ask user for each target_face_idx for each destination image
|
| 33 |
-
dest_images = [f for f in os.listdir(dst_folder) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
|
| 34 |
-
dest_face_indices = {}
|
| 35 |
-
for filename in dest_images:
|
| 36 |
-
while True:
|
| 37 |
-
try:
|
| 38 |
-
idx_str = input(f"Enter target_face_idx for destination image '{filename}' (default 1): ")
|
| 39 |
-
if idx_str.strip() == "":
|
| 40 |
-
idx = 1
|
| 41 |
-
else:
|
| 42 |
-
idx = int(idx_str)
|
| 43 |
-
if idx < 1:
|
| 44 |
-
print("Index must be >= 1.")
|
| 45 |
-
continue
|
| 46 |
-
dest_face_indices[filename] = idx
|
| 47 |
-
break
|
| 48 |
-
except ValueError:
|
| 49 |
-
print("Please enter a valid integer.")
|
| 50 |
-
|
| 51 |
-
for filename in dest_images:
|
| 52 |
-
target_img_path = os.path.join(dst_folder, filename)
|
| 53 |
-
if not os.path.exists(target_img_path):
|
| 54 |
-
print(f"Could not find target image: {target_img_path}")
|
| 55 |
-
continue
|
| 56 |
-
try:
|
| 57 |
-
result = swapper.swap_faces(
|
| 58 |
-
source_path=source_img_path,
|
| 59 |
-
source_face_idx=source_face_idx,
|
| 60 |
-
target_path=target_img_path,
|
| 61 |
-
target_face_idx=dest_face_indices[filename]
|
| 62 |
-
)
|
| 63 |
-
output_path = os.path.join(output_folder, f"swapped_{filename}")
|
| 64 |
-
cv2.imwrite(output_path, result)
|
| 65 |
-
print(f"Swapped face saved to: {output_path}")
|
| 66 |
-
except Exception as e:
|
| 67 |
-
print(f"Error processing {filename}: {str(e)}")
|
| 68 |
-
|
| 69 |
-
if __name__ == "__main__":
|
| 70 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VideoSwapping.py
DELETED
|
@@ -1,168 +0,0 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import os
|
| 3 |
-
import shutil
|
| 4 |
-
import time
|
| 5 |
-
from SinglePhoto import FaceSwapper
|
| 6 |
-
|
| 7 |
-
def extract_frames(video_path, frames_dir):
|
| 8 |
-
if not os.path.exists(frames_dir):
|
| 9 |
-
os.makedirs(frames_dir)
|
| 10 |
-
last_idx = -1
|
| 11 |
-
else:
|
| 12 |
-
# Find the last extracted frame index
|
| 13 |
-
existing = [f for f in os.listdir(frames_dir) if f.startswith("frame_") and f.endswith(".jpg")]
|
| 14 |
-
if existing:
|
| 15 |
-
last_idx = max([int(f.split("_")[1].split(".")[0]) for f in existing])
|
| 16 |
-
else:
|
| 17 |
-
last_idx = -1
|
| 18 |
-
|
| 19 |
-
cap = cv2.VideoCapture(video_path)
|
| 20 |
-
frame_paths = []
|
| 21 |
-
idx = 0
|
| 22 |
-
while True:
|
| 23 |
-
ret, frame = cap.read()
|
| 24 |
-
if not ret:
|
| 25 |
-
break
|
| 26 |
-
frame_path = os.path.join(frames_dir, f"frame_{idx:05d}.jpg")
|
| 27 |
-
if idx > last_idx:
|
| 28 |
-
cv2.imwrite(frame_path, frame)
|
| 29 |
-
frame_paths.append(frame_path)
|
| 30 |
-
idx += 1
|
| 31 |
-
cap.release()
|
| 32 |
-
return frame_paths
|
| 33 |
-
|
| 34 |
-
def frames_to_video(frames_dir, output_video_path, fps):
|
| 35 |
-
frames = sorted([os.path.join(frames_dir, f) for f in os.listdir(frames_dir) if f.endswith('.jpg')])
|
| 36 |
-
if not frames:
|
| 37 |
-
print("No frames found in directory.")
|
| 38 |
-
return
|
| 39 |
-
first_frame = cv2.imread(frames[0])
|
| 40 |
-
height, width, layers = first_frame.shape
|
| 41 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 42 |
-
out = cv2.VideoWriter(output_video_path, fourcc, fps, (width, height))
|
| 43 |
-
for frame_path in frames:
|
| 44 |
-
frame = cv2.imread(frame_path)
|
| 45 |
-
out.write(frame)
|
| 46 |
-
out.release()
|
| 47 |
-
|
| 48 |
-
def main():
|
| 49 |
-
# Use files from VideoSwapping folder
|
| 50 |
-
video_path = os.path.join("VideoSwapping", "data_dst.mp4")
|
| 51 |
-
source_image_path = os.path.join("VideoSwapping", "data_src.jpg")
|
| 52 |
-
frames_dir = os.path.join("VideoSwapping", "video_frames")
|
| 53 |
-
swapped_dir = os.path.join("VideoSwapping", "swapped_frames")
|
| 54 |
-
output_video_path = os.path.join("VideoSwapping", "output_swapped_video.mp4")
|
| 55 |
-
source_face_idx = 1
|
| 56 |
-
|
| 57 |
-
# Ask user for target_face_idx
|
| 58 |
-
while True:
|
| 59 |
-
try:
|
| 60 |
-
user_input = input("Enter target_face_idx (default is 1): ").strip()
|
| 61 |
-
if user_input == "":
|
| 62 |
-
dest_face_idx = 1
|
| 63 |
-
break
|
| 64 |
-
dest_face_idx = int(user_input)
|
| 65 |
-
break
|
| 66 |
-
except ValueError:
|
| 67 |
-
print("Invalid input. Please enter an integer value.")
|
| 68 |
-
|
| 69 |
-
print("Choose an option:")
|
| 70 |
-
print("1. Extract frames only")
|
| 71 |
-
print("2. Face swap only (requires extracted frames)")
|
| 72 |
-
print("3. Both extract frames and face swap")
|
| 73 |
-
choice = input("Enter 1, 2, or 3: ").strip()
|
| 74 |
-
|
| 75 |
-
frame_paths = []
|
| 76 |
-
if choice == "1" or choice == "3":
|
| 77 |
-
print("Extracting frames from video (resuming if needed)...")
|
| 78 |
-
frame_paths = extract_frames(video_path, frames_dir)
|
| 79 |
-
print(f"Extracted {len(frame_paths)} frames to {frames_dir}.")
|
| 80 |
-
if choice == "1":
|
| 81 |
-
return
|
| 82 |
-
|
| 83 |
-
if choice == "2":
|
| 84 |
-
# If only face swap, ensure frames are present
|
| 85 |
-
if not os.path.exists(frames_dir):
|
| 86 |
-
print("Frames directory does not exist. Please extract frames first.")
|
| 87 |
-
return
|
| 88 |
-
frame_paths = sorted([os.path.join(frames_dir, f) for f in os.listdir(frames_dir) if f.endswith('.jpg')])
|
| 89 |
-
|
| 90 |
-
if choice == "2" or choice == "3":
|
| 91 |
-
# Prepare output directory
|
| 92 |
-
if not os.path.exists(swapped_dir):
|
| 93 |
-
os.makedirs(swapped_dir)
|
| 94 |
-
|
| 95 |
-
# Initialize face swapper
|
| 96 |
-
swapper = FaceSwapper()
|
| 97 |
-
|
| 98 |
-
# Swap faces on each frame
|
| 99 |
-
print("Swapping faces on frames...")
|
| 100 |
-
start_time = time.time()
|
| 101 |
-
for idx, frame_path in enumerate(frame_paths):
|
| 102 |
-
frame_name = os.path.basename(frame_path)
|
| 103 |
-
# Replace 'frame_' with 'swapped_' in the filename
|
| 104 |
-
if frame_name.startswith("frame_"):
|
| 105 |
-
swapped_name = "swapped_" + frame_name[len("frame_"):]
|
| 106 |
-
else:
|
| 107 |
-
swapped_name = "swapped_" + frame_name
|
| 108 |
-
out_path = os.path.join(swapped_dir, swapped_name)
|
| 109 |
-
if os.path.exists(out_path):
|
| 110 |
-
# Skip already swapped frames
|
| 111 |
-
print(f"Frame {idx+1}/{len(frame_paths)} already swapped, skipping...", end='\r')
|
| 112 |
-
continue
|
| 113 |
-
try:
|
| 114 |
-
try:
|
| 115 |
-
swapped = swapper.swap_faces(
|
| 116 |
-
source_path=source_image_path,
|
| 117 |
-
source_face_idx=source_face_idx,
|
| 118 |
-
target_path=frame_path,
|
| 119 |
-
target_face_idx=dest_face_idx
|
| 120 |
-
)
|
| 121 |
-
except ValueError as ve:
|
| 122 |
-
if "Target image contains" in str(ve):
|
| 123 |
-
print(f"\nFrame {idx}: Target face idx {dest_face_idx} not found, trying with idx 1.",end='\r')
|
| 124 |
-
swapped = swapper.swap_faces(
|
| 125 |
-
source_path=source_image_path,
|
| 126 |
-
source_face_idx=source_face_idx,
|
| 127 |
-
target_path=frame_path,
|
| 128 |
-
target_face_idx=1
|
| 129 |
-
)
|
| 130 |
-
else:
|
| 131 |
-
raise ve
|
| 132 |
-
cv2.imwrite(out_path, swapped)
|
| 133 |
-
except Exception as e:
|
| 134 |
-
print(f"\nFrame {idx}: {e}")
|
| 135 |
-
# Optionally, copy the original frame if swap fails
|
| 136 |
-
cv2.imwrite(out_path, cv2.imread(frame_path))
|
| 137 |
-
# Estimate time left
|
| 138 |
-
elapsed = time.time() - start_time
|
| 139 |
-
avg_time = elapsed / (idx + 1)
|
| 140 |
-
remaining = avg_time * (len(frame_paths) - (idx + 1))
|
| 141 |
-
mins, secs = divmod(int(remaining), 60)
|
| 142 |
-
print(f"Swapping frame {idx+1}/{len(frame_paths)} | Est. time left: {mins:02d}:{secs:02d}", end='\r')
|
| 143 |
-
print() # Move to the next line after the loop
|
| 144 |
-
|
| 145 |
-
# Get FPS from original video
|
| 146 |
-
cap = cv2.VideoCapture(video_path)
|
| 147 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 148 |
-
cap.release()
|
| 149 |
-
|
| 150 |
-
# Combine swapped frames into video
|
| 151 |
-
print("Combining swapped frames into video...")
|
| 152 |
-
frames_to_video(swapped_dir, output_video_path, fps)
|
| 153 |
-
print(f"Done! Output video saved as {output_video_path}")
|
| 154 |
-
|
| 155 |
-
# Ask user if they want to keep the extracted frames and swapped images
|
| 156 |
-
answer = input("Do you want to keep the extracted frames and swapped images? (y/n): ").strip().lower()
|
| 157 |
-
if answer == 'n':
|
| 158 |
-
try:
|
| 159 |
-
shutil.rmtree(frames_dir)
|
| 160 |
-
shutil.rmtree(swapped_dir)
|
| 161 |
-
print("Temporary folders deleted.")
|
| 162 |
-
except Exception as e:
|
| 163 |
-
print(f"Error deleting folders: {e}")
|
| 164 |
-
else:
|
| 165 |
-
print("Temporary folders kept.")
|
| 166 |
-
|
| 167 |
-
if __name__ == "__main__":
|
| 168 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|