From a439a59296f1a020914ac29069dbe70d46111ca7 Mon Sep 17 00:00:00 2001 From: Andras Fekete Date: Fri, 16 Jun 2023 09:04:25 -0400 Subject: [PATCH] Process only 1k frames at a time to minimize memory footprint --- refacer.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/refacer.py b/refacer.py index 0cee44a..9ae0d36 100644 --- a/refacer.py +++ b/refacer.py @@ -173,6 +173,12 @@ class Refacer: if audio_stream is not None: self.video_has_audio = True + def reface_group(self, faces, frames, output): + with ThreadPoolExecutor(max_workers = self.use_num_cpus) as executor: + results = list(tqdm(executor.map(self.process_faces, frames), total=len(frames),desc="Processing frames")) + for result in results: + output.write(result) + def reface(self, video_path, faces): self.__check_video_has_audio(video_path) output_video_path = os.path.join('out',Path(video_path).name) @@ -199,15 +205,17 @@ class Refacer: pbar.update() else: break + if (len(frames) > 1000): + self.reface_group(faces,frames,output) + frames=[] + cap.release() pbar.close() - - with ThreadPoolExecutor(max_workers = self.use_num_cpus) as executor: - results = list(tqdm(executor.map(self.process_faces, frames), total=len(frames),desc="Processing frames")) - for result in results: - output.write(result) - output.release() + self.reface_group(faces,frames,output) + frames=[] + output.release() + return self.__convert_video(video_path,output_video_path) def __try_ffmpeg_encoder(self, vcodec): @@ -248,4 +256,4 @@ class Refacer: #'h264_vaapi', #Intel HW acceleration #'h264_omx', #HW acceleration 'libx264':'0' #No HW acceleration - } \ No newline at end of file + }