Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,13 +24,12 @@ def video_to_frames(video_path, output_folder):
|
|
| 24 |
cv2.imwrite(frame_name, image)
|
| 25 |
success, image = vidcap.read()
|
| 26 |
count += 1
|
| 27 |
-
if count>550:
|
| 28 |
-
|
| 29 |
print(f"{count} frames extracted from {video_path}.")
|
| 30 |
-
return [count,fps]
|
| 31 |
|
| 32 |
-
def frames_to_video(frame_folder, video_path, image_path, frame_count,fps):
|
| 33 |
-
print("ImagePath",image_path)
|
| 34 |
global progress
|
| 35 |
frames = [f for f in os.listdir(frame_folder) if f.endswith('.jpg')]
|
| 36 |
frames.sort(key=lambda x: int(x.split('_')[1].split('.')[0])) # Sort frames in ascending order
|
|
@@ -40,17 +39,15 @@ def frames_to_video(frame_folder, video_path, image_path, frame_count,fps):
|
|
| 40 |
|
| 41 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 42 |
out = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
|
|
|
| 43 |
providers = ["CUDAExecutionProvider"]
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
for i in tqdm(range(frame_count), desc="Converting frames to video"):
|
| 49 |
-
print("Progress:",progress)
|
| 50 |
img1 = cv2.imread(os.path.join(frame_folder, frames[i]))
|
| 51 |
-
#img2_pil = Image.open(image_path)
|
| 52 |
-
#img2_cv2 = cv2.cvtColor(np.array(img2_pil), cv2.COLOR_RGB2BGR)
|
| 53 |
-
|
| 54 |
faces1 = app.get(img1)
|
| 55 |
for _ in range(20):
|
| 56 |
faces2 = app.get(image_path)
|
|
@@ -73,14 +70,19 @@ def frames_to_video(frame_folder, video_path, image_path, frame_count,fps):
|
|
| 73 |
print(f"Video saved at {video_path}.")
|
| 74 |
|
| 75 |
def face_swap(video_path, image_path):
|
|
|
|
|
|
|
| 76 |
output_folder = "Out_Frames"
|
| 77 |
frame_count = video_to_frames(video_path, output_folder)
|
| 78 |
if frame_count[0] > 400:
|
| 79 |
frame_count[0] = 400
|
| 80 |
-
output_video_path = "output_video.mp4"
|
| 81 |
-
frames_to_video(output_folder, output_video_path, image_path, frame_count[0],frame_count[1])
|
| 82 |
-
return output_video_path
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
iface = gr.Interface(
|
| 86 |
fn=face_swap,
|
|
@@ -89,4 +91,14 @@ iface = gr.Interface(
|
|
| 89 |
title="Profaker's Face Swap",
|
| 90 |
description="Upload a video and an image. The faces in the video will be swapped with the face in the image.",
|
| 91 |
)
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
cv2.imwrite(frame_name, image)
|
| 25 |
success, image = vidcap.read()
|
| 26 |
count += 1
|
| 27 |
+
if count > 550:
|
| 28 |
+
break
|
| 29 |
print(f"{count} frames extracted from {video_path}.")
|
| 30 |
+
return [count, fps]
|
| 31 |
|
| 32 |
+
def frames_to_video(frame_folder, video_path, image_path, frame_count, fps):
|
|
|
|
| 33 |
global progress
|
| 34 |
frames = [f for f in os.listdir(frame_folder) if f.endswith('.jpg')]
|
| 35 |
frames.sort(key=lambda x: int(x.split('_')[1].split('.')[0])) # Sort frames in ascending order
|
|
|
|
| 39 |
|
| 40 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 41 |
out = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
|
| 42 |
+
|
| 43 |
providers = ["CUDAExecutionProvider"]
|
| 44 |
+
app = insightface.app.FaceAnalysis(name='buffalo_l', providers=providers)
|
| 45 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
| 46 |
+
swapper = insightface.model_zoo.get_model("inswapper_128.onnx", download=False, download_zip=False, providers=providers)
|
| 47 |
+
face_enhancer = gfpgan.GFPGANer(model_path="GFPGANv1.4.pth", upscale=1, device='cuda')
|
| 48 |
|
| 49 |
for i in tqdm(range(frame_count), desc="Converting frames to video"):
|
|
|
|
| 50 |
img1 = cv2.imread(os.path.join(frame_folder, frames[i]))
|
|
|
|
|
|
|
|
|
|
| 51 |
faces1 = app.get(img1)
|
| 52 |
for _ in range(20):
|
| 53 |
faces2 = app.get(image_path)
|
|
|
|
| 70 |
print(f"Video saved at {video_path}.")
|
| 71 |
|
| 72 |
def face_swap(video_path, image_path):
|
| 73 |
+
global progress
|
| 74 |
+
progress = 0
|
| 75 |
output_folder = "Out_Frames"
|
| 76 |
frame_count = video_to_frames(video_path, output_folder)
|
| 77 |
if frame_count[0] > 400:
|
| 78 |
frame_count[0] = 400
|
| 79 |
+
output_video_path = "output_video.mp4"
|
| 80 |
+
frames_to_video(output_folder, output_video_path, image_path, frame_count[0], frame_count[1])
|
| 81 |
+
return output_video_path
|
| 82 |
+
|
| 83 |
+
def get_progress():
|
| 84 |
+
global progress
|
| 85 |
+
return progress
|
| 86 |
|
| 87 |
iface = gr.Interface(
|
| 88 |
fn=face_swap,
|
|
|
|
| 91 |
title="Profaker's Face Swap",
|
| 92 |
description="Upload a video and an image. The faces in the video will be swapped with the face in the image.",
|
| 93 |
)
|
| 94 |
+
|
| 95 |
+
progress_iface = gr.Interface(
|
| 96 |
+
fn=get_progress,
|
| 97 |
+
inputs=[],
|
| 98 |
+
outputs="number",
|
| 99 |
+
live=True,
|
| 100 |
+
title="Progress Tracker"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
iface.launch(share=True)
|
| 104 |
+
progress_iface.launch(share=True)
|