diff --git a/app.py b/app.py index 692532d..17170f3 100644 --- a/app.py +++ b/app.py @@ -37,8 +37,8 @@ with gr.Blocks() as demo: with gr.Row(): gr.Markdown("# Refacer") with gr.Row(): - video=gr.Video(label="Original video") - video2=gr.Video(label="Refaced video",interactive=False) + video=gr.Video(label="Original video",format="mp4") + video2=gr.Video(label="Refaced video",interactive=False,format="mp4") for i in range(0,num_faces): with gr.Tab(f"Face #{i+1}"): diff --git a/notebooks/Refacer_colab.ipynb b/notebooks/Refacer_colab.ipynb index 1c422bd..e443b4d 100644 --- a/notebooks/Refacer_colab.ipynb +++ b/notebooks/Refacer_colab.ipynb @@ -35,27 +35,14 @@ "outputs": [], "source": [ "!pip uninstall numpy -y -q\n", - "!pip install numpy==1.24.3 onnxruntime-gpu gradio insightface ffmpeg_python opencv_python -q --force\n", + "!pip install --disable-pip-version-check --root-user-action=ignore numpy==1.24.3 onnxruntime-gpu gradio insightface==0.7.3 ffmpeg_python opencv_python -q --force\n", "\n", "!git clone https://github.com/xaviviro/refacer.git\n", "%cd refacer\n", "\n", - "!wget --content-disposition \"https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "0SsyI3quBb0L", - "outputId": "56add1a6-6b47-429d-e07a-cf3fadc7a6ce" - }, - "outputs": [], - "source": [ - "!python app.py --share_gradio" + "!wget --content-disposition \"https://huggingface.co/deepinsight/inswapper/resolve/main/inswapper_128.onnx\"\n", + "\n", + "!python app.py --share_gradio\n" ] } ], diff --git a/refacer.py b/refacer.py index d5862df..e302886 100644 --- a/refacer.py +++ b/refacer.py @@ -18,6 +18,8 @@ import psutil from enum import Enum from insightface.app.common import Face from insightface.utils.storage import ensure_available +import re +import subprocess class RefacerMode(Enum): CPU, CUDA, COREML, TENSORRT = range(1, 5) @@ -25,6 +27,7 @@ class RefacerMode(Enum): class Refacer: def __init__(self,force_cpu=False): self.force_cpu = force_cpu + self.__check_encoders() self.__check_providers() self.total_mem = psutil.virtual_memory().total self.__init_apps() @@ -96,12 +99,14 @@ class Refacer: return replacements def __convert_video(self,video_path,output_video_path): + print("Merging audio with the refaced video...") new_path = output_video_path + str(random.randint(0,999)) + "_c.mp4" #stream = ffmpeg.input(output_video_path) in1 = ffmpeg.input(output_video_path) in2 = ffmpeg.input(video_path) - out = ffmpeg.output(in1.video, in2.audio, new_path,vcodec="libx264") - out.run() + out = ffmpeg.output(in1.video, in2.audio, new_path,vcodec=self.ffmpeg_video_encoder) + out.run(overwrite_output=True,quiet=True) + print(f"The process has finished.\nThe refaced video can be found at {os.path.abspath(new_path)}") return new_path def __get_faces(self,frame,max_num=0): @@ -120,7 +125,7 @@ class Refacer: face.embedding = self.rec_app.get(frame, kps) ret.append(face) return ret - + def __process_faces(self,frame): faces = self.__get_faces(frame) for face in faces: @@ -168,4 +173,29 @@ class Refacer: output.write(result) output.release() - return self.__convert_video(video_path,output_video_path) \ No newline at end of file + return self.__convert_video(video_path,output_video_path) + + def __check_encoders(self): + self.ffmpeg_video_encoder="libx264" + pattern = r"encoders: ([a-zA-Z0-9_]+(?: [a-zA-Z0-9_]+)*)" + command = ['ffmpeg', '-codecs', '--list-encoders'] + commandout = subprocess.run(command, check=True, capture_output=True).stdout + result = commandout.decode('utf-8').split('\n') + for r in result: + if "264" in r: + encoders = re.search(pattern, r).group(1).split(' ') + #print(encoders) + for v_c in Refacer.VIDEO_CODECS: + if v_c in encoders: + self.ffmpeg_video_encoder=v_c + break + print(f"Video codec for FFMPEG: {self.ffmpeg_video_encoder}") + + VIDEO_CODECS = [ + #'h264_videotoolbox', #osx HW acceleration + 'h264_nvenc', #NVIDIA HW acceleration + #'h264_qsv', #Intel HW acceleration + #'h264_vaapi', #Intel HW acceleration + #'h264_omx', #HW acceleration + 'libx264', #No HW acceleration + ] \ No newline at end of file