From 339e531a2e7ddc320a68c731b93dde3b17d12021 Mon Sep 17 00:00:00 2001 From: Somdev Sangwan Date: Tue, 30 May 2023 20:30:35 +0530 Subject: [PATCH 1/5] --cores option --- run.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/run.py b/run.py index f14e318..be5511c 100644 --- a/run.py +++ b/run.py @@ -41,10 +41,13 @@ parser.add_argument('-o', '--output', help='save output to this file', dest='out parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps', action='store_true', default=False) parser.add_argument('--gpu', help='use gpu', dest='gpu', action='store_true', default=False) parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_frames', action='store_true', default=False) +parser.add_argument('--cores', help='number of cores to use', dest='cores_count', type=int) for name, value in vars(parser.parse_args()).items(): args[name] = value +if not args['cores_count']: + args['cores_count'] = psutil.cpu_count()-1 sep = "/" if os.name == "nt": @@ -60,7 +63,7 @@ def start_processing(): print(f"Processing time: {end_time - start_time:.2f} seconds", flush=True) return frame_paths = args["frame_paths"] - n = len(frame_paths)//(psutil.cpu_count()-1) + n = len(frame_paths)//(args['cores_count']) processes = [] for i in range(0, len(frame_paths), n): p = pool.apply_async(process_video, args=(args['source_img'], frame_paths[i:i+n],)) @@ -148,7 +151,7 @@ def start(): if not args['output_file']: args['output_file'] = rreplace(args['target_path'], "/", "/swapped-", 1) if "/" in target_path else "swapped-"+target_path global pool - pool = mp.Pool(psutil.cpu_count()-1) + pool = mp.Pool(args['cores_count']) target_path = args['target_path'] test_face = get_face(cv2.imread(args['source_img'])) if not test_face: From 561397ab058867cbc2feef6d6896903fd4cacb24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20G=C3=B6z=C3=BCkara?= Date: Tue, 30 May 2023 19:07:20 +0300 Subject: [PATCH 2/5] Improves final output video quality significantly Improves final output video quality significantly --- core/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/utils.py b/core/utils.py index 2d12fa6..66dab65 100644 --- a/core/utils.py +++ b/core/utils.py @@ -36,7 +36,7 @@ def set_fps(input_path, output_path, fps): def create_video(video_name, fps, output_dir): output_dir = path(output_dir) - os.system(f'ffmpeg -framerate {fps} -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 32 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') + os.system(f'ffmpeg -framerate {fps} -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"') def extract_frames(input_path, output_dir): From a3617b1cff4361bd3712ba9d2e99a2e7bf5d16ef Mon Sep 17 00:00:00 2001 From: Symbiomatrix Date: Tue, 30 May 2023 19:29:33 +0300 Subject: [PATCH 3/5] Update run.py --- run.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/run.py b/run.py index be5511c..7e16348 100644 --- a/run.py +++ b/run.py @@ -161,9 +161,10 @@ def start(): process_img(args['source_img'], target_path, args['output_file']) status("swap successful!") return - video_name = target_path.split("/")[-1].split(".")[0] - output_dir = target_path.replace(target_path.split("/")[-1], "").rstrip("/") + "/" + video_name - Path(output_dir).mkdir(exist_ok=True) + video_name = os.path.basename(target_path) + video_name = os.path.splitext(video_name)[0] + output_dir = os.path.join(os.path.dirname(target_path),video_name) + os.makedirs(output_dir, exist_ok = True) status("detecting video's FPS...") fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: From cb2ff526788909a610f13010717d845c10ae91b2 Mon Sep 17 00:00:00 2001 From: Somdev Sangwan Date: Tue, 30 May 2023 22:04:38 +0530 Subject: [PATCH 4/5] just use Path --- run.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.py b/run.py index 7e16348..49ca578 100644 --- a/run.py +++ b/run.py @@ -164,7 +164,7 @@ def start(): video_name = os.path.basename(target_path) video_name = os.path.splitext(video_name)[0] output_dir = os.path.join(os.path.dirname(target_path),video_name) - os.makedirs(output_dir, exist_ok = True) + Path(output_dir).mkdir(exist_ok=True) status("detecting video's FPS...") fps = detect_fps(target_path) if not args['keep_fps'] and fps > 30: From e4f1d9aaff43549667f7c81c6f48a86606fb9b95 Mon Sep 17 00:00:00 2001 From: RealCalumPlays <69392845+RealCalumPlays@users.noreply.github.com> Date: Tue, 30 May 2023 17:35:36 +0100 Subject: [PATCH 5/5] Update README.md to include --cores parameter (#128) * Update utils.py * Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b65f4a7..7eea61c 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Additional command line arguments are given below: --keep-fps keep original fps --gpu use gpu --keep-frames don't delete frames directory +--cores number of cores to use ``` Looking for a CLI mode? Using the -f/--face argument will make the program in cli mode.