|
|
import os |
|
|
import torch |
|
|
from comfy.model_management import CPUState |
|
|
|
|
|
|
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = "" |
|
|
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "" |
|
|
|
|
|
|
|
|
import comfy.model_management |
|
|
comfy.model_management.cpu_state = CPUState.CPU |
|
|
|
|
|
import random |
|
|
import sys |
|
|
from typing import Sequence, Mapping, Any, Union |
|
|
import torch |
|
|
from PIL import Image |
|
|
from huggingface_hub import hf_hub_download |
|
|
import spaces |
|
|
|
|
|
import subprocess, sys |
|
|
|
|
|
import gradio |
|
|
import gradio_client |
|
|
import gradio as gr |
|
|
|
|
|
print("gradio version:", gradio.__version__) |
|
|
print("gradio_client version:", gradio_client.__version__) |
|
|
|
|
|
hf_hub_download(repo_id="facefusion/models-3.3.0", filename="hyperswap_1a_256.onnx", local_dir="models/hyperswap") |
|
|
hf_hub_download(repo_id="facefusion/models-3.3.0", filename="hyperswap_1b_256.onnx", local_dir="models/hyperswap") |
|
|
hf_hub_download(repo_id="facefusion/models-3.3.0", filename="hyperswap_1c_256.onnx", local_dir="models/hyperswap") |
|
|
|
|
|
hf_hub_download(repo_id="martintomov/comfy", filename="facedetection/yolov5l-face.pth", local_dir="models") |
|
|
|
|
|
hf_hub_download(repo_id="gmk123/GFPGAN", filename="parsing_parsenet.pth", local_dir="models/facedetection") |
|
|
|
|
|
hf_hub_download(repo_id="MonsterMMORPG/tools", filename="1k3d68.onnx", local_dir="models/insightface/models/buffalo_l") |
|
|
hf_hub_download(repo_id="MonsterMMORPG/tools", filename="2d106det.onnx", local_dir="models/insightface/models/buffalo_l") |
|
|
hf_hub_download(repo_id="maze/faceX", filename="det_10g.onnx", local_dir="models/insightface/models/buffalo_l") |
|
|
hf_hub_download(repo_id="typhoon01/aux_models", filename="genderage.onnx", local_dir="models/insightface/models/buffalo_l") |
|
|
hf_hub_download(repo_id="maze/faceX", filename="w600k_r50.onnx", local_dir="models/insightface/models/buffalo_l") |
|
|
|
|
|
hf_hub_download(repo_id="vladmandic/insightface-faceanalysis", filename="buffalo_l.zip", local_dir="models/insightface/models/buffalo_l") |
|
|
|
|
|
|
|
|
def get_value_at_index(obj: Union[Sequence, Mapping], index: int) -> Any: |
|
|
"""Returns the value at the given index of a sequence or mapping. |
|
|
|
|
|
If the object is a sequence (like list or string), returns the value at the given index. |
|
|
If the object is a mapping (like a dictionary), returns the value at the index-th key. |
|
|
|
|
|
Some return a dictionary, in these cases, we look for the "results" key |
|
|
|
|
|
Args: |
|
|
obj (Union[Sequence, Mapping]): The object to retrieve the value from. |
|
|
index (int): The index of the value to retrieve. |
|
|
|
|
|
Returns: |
|
|
Any: The value at the given index. |
|
|
|
|
|
Raises: |
|
|
IndexError: If the index is out of bounds for the object and the object is not a mapping. |
|
|
""" |
|
|
try: |
|
|
return obj[index] |
|
|
except KeyError: |
|
|
return obj["result"][index] |
|
|
|
|
|
|
|
|
def find_path(name: str, path: str = None) -> str: |
|
|
""" |
|
|
Recursively looks at parent folders starting from the given path until it finds the given name. |
|
|
Returns the path as a Path object if found, or None otherwise. |
|
|
""" |
|
|
|
|
|
if path is None: |
|
|
path = os.getcwd() |
|
|
|
|
|
|
|
|
if name in os.listdir(path): |
|
|
path_name = os.path.join(path, name) |
|
|
print(f"{name} found: {path_name}") |
|
|
return path_name |
|
|
|
|
|
|
|
|
parent_directory = os.path.dirname(path) |
|
|
|
|
|
|
|
|
if parent_directory == path: |
|
|
return None |
|
|
|
|
|
|
|
|
return find_path(name, parent_directory) |
|
|
|
|
|
|
|
|
def add_comfyui_directory_to_sys_path() -> None: |
|
|
""" |
|
|
Add 'ComfyUI' to the sys.path |
|
|
""" |
|
|
comfyui_path = find_path("ComfyUI") |
|
|
if comfyui_path is not None and os.path.isdir(comfyui_path): |
|
|
sys.path.append(comfyui_path) |
|
|
print(f"'{comfyui_path}' added to sys.path") |
|
|
|
|
|
|
|
|
def add_extra_model_paths() -> None: |
|
|
""" |
|
|
Parse the optional extra_model_paths.yaml file and add the parsed paths to the sys.path. |
|
|
""" |
|
|
try: |
|
|
from main import load_extra_path_config |
|
|
except ImportError: |
|
|
print( |
|
|
"Could not import load_extra_path_config from main.py. Looking in utils.extra_config instead." |
|
|
) |
|
|
from utils.extra_config import load_extra_path_config |
|
|
|
|
|
extra_model_paths = find_path("extra_model_paths.yaml") |
|
|
|
|
|
if extra_model_paths is not None: |
|
|
load_extra_path_config(extra_model_paths) |
|
|
else: |
|
|
print("Could not find the extra_model_paths config file.") |
|
|
|
|
|
|
|
|
add_comfyui_directory_to_sys_path() |
|
|
add_extra_model_paths() |
|
|
|
|
|
|
|
|
def import_custom_nodes() -> None: |
|
|
"""Find all custom nodes in the custom_nodes folder and add those node objects to NODE_CLASS_MAPPINGS |
|
|
|
|
|
This function sets up a new asyncio event loop, initializes the PromptServer, |
|
|
creates a PromptQueue, and initializes the custom nodes. |
|
|
""" |
|
|
import asyncio |
|
|
import execution |
|
|
from nodes import init_extra_nodes |
|
|
import server |
|
|
|
|
|
|
|
|
loop = asyncio.new_event_loop() |
|
|
asyncio.set_event_loop(loop) |
|
|
|
|
|
|
|
|
server_instance = server.PromptServer(loop) |
|
|
execution.PromptQueue(server_instance) |
|
|
|
|
|
|
|
|
|
|
|
loop.run_until_complete(init_extra_nodes()) |
|
|
|
|
|
import_custom_nodes() |
|
|
from nodes import NODE_CLASS_MAPPINGS |
|
|
|
|
|
|
|
|
loadimage = NODE_CLASS_MAPPINGS["LoadImage"]() |
|
|
vhs_loadvideo = NODE_CLASS_MAPPINGS["VHS_LoadVideo"]() |
|
|
reactoroptions = NODE_CLASS_MAPPINGS["ReActorOptions"]() |
|
|
vhs_videoinfoloaded = NODE_CLASS_MAPPINGS["VHS_VideoInfoLoaded"]() |
|
|
reactorfaceswapopt = NODE_CLASS_MAPPINGS["ReActorFaceSwapOpt"]() |
|
|
vhs_videocombine = NODE_CLASS_MAPPINGS["VHS_VideoCombine"]() |
|
|
|
|
|
|
|
|
def generate_image(source_image, input_video, input_index, input_faces_order, swap_model, pingpong, loop_count, select_every_nth, use_audio): |
|
|
with torch.inference_mode(): |
|
|
loadimage_29 = loadimage.load_image(image=source_image) |
|
|
|
|
|
vhs_loadvideo_51 = vhs_loadvideo.load_video( |
|
|
video=input_video, |
|
|
force_rate=0, |
|
|
custom_width=0, |
|
|
custom_height=0, |
|
|
frame_load_cap=0, |
|
|
skip_first_frames=0, |
|
|
select_every_nth=select_every_nth, |
|
|
format="AnimateDiff", |
|
|
unique_id=17765013700631265033, |
|
|
) |
|
|
|
|
|
reactoroptions_107 = reactoroptions.execute( |
|
|
input_faces_order=input_faces_order, |
|
|
input_faces_index=str(input_index), |
|
|
detect_gender_input="no", |
|
|
source_faces_order="large-small", |
|
|
source_faces_index="0", |
|
|
detect_gender_source="no", |
|
|
console_log_level=1, |
|
|
) |
|
|
|
|
|
for q in range(1): |
|
|
vhs_videoinfoloaded_105 = vhs_videoinfoloaded.get_video_info( |
|
|
video_info=get_value_at_index(vhs_loadvideo_51, 3) |
|
|
) |
|
|
|
|
|
reactorfaceswapopt_106 = reactorfaceswapopt.execute( |
|
|
enabled=True, |
|
|
swap_model=swap_model, |
|
|
facedetection="YOLOv5l", |
|
|
face_restore_model="none", |
|
|
face_restore_visibility=1, |
|
|
codeformer_weight=0.5, |
|
|
input_image=get_value_at_index(vhs_loadvideo_51, 0), |
|
|
source_image=get_value_at_index(loadimage_29, 0), |
|
|
options=get_value_at_index(reactoroptions_107, 0), |
|
|
) |
|
|
|
|
|
|
|
|
combine_kwargs = dict( |
|
|
frame_rate=get_value_at_index(vhs_videoinfoloaded_105, 0), |
|
|
loop_count=loop_count, |
|
|
filename_prefix="vidswap", |
|
|
format="video/h264-mp4", |
|
|
pix_fmt="yuv420p", |
|
|
crf=20, |
|
|
save_metadata=False, |
|
|
trim_to_audio=False, |
|
|
pingpong=pingpong, |
|
|
save_output=True, |
|
|
images=get_value_at_index(reactorfaceswapopt_106, 0), |
|
|
unique_id=17889577966051683261, |
|
|
) |
|
|
|
|
|
if use_audio: |
|
|
combine_kwargs["audio"] = get_value_at_index(vhs_loadvideo_51, 2) |
|
|
|
|
|
vhs_videocombine_28 = vhs_videocombine.combine_video(**combine_kwargs) |
|
|
|
|
|
saved_path = f"output/{vhs_videocombine_28['ui']['gifs'][0]['filename']}" |
|
|
return saved_path |
|
|
|
|
|
if __name__ == "__main__": |
|
|
with gr.Blocks() as app: |
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
|
|
|
with gr.Row(): |
|
|
|
|
|
with gr.Group(): |
|
|
source_image = gr.Image(label="Source Image (Face)", type="filepath") |
|
|
swap_model = gr.Dropdown( |
|
|
choices=["hyperswap_1a_256.onnx", "hyperswap_1b_256.onnx", "hyperswap_1c_256.onnx"], |
|
|
value="hyperswap_1b_256.onnx", |
|
|
label="Swap Model" |
|
|
) |
|
|
input_index = gr.Dropdown(choices=[0, 1, 2, 3, 4], value=0, label="Target Face Index") |
|
|
input_faces_order_dropdown = gr.Dropdown( |
|
|
choices=[ |
|
|
"left-right", |
|
|
"right-left", |
|
|
"top-bottom", |
|
|
"bottom-top", |
|
|
"large-small" |
|
|
], |
|
|
value="large-small", |
|
|
label="Target Faces Order") |
|
|
|
|
|
|
|
|
with gr.Group(): |
|
|
input_video = gr.Video(label="Target Video (Body)") |
|
|
select_every_nth = gr.Dropdown(choices=[1, 2], value=1, label='"1" = choose every frame, "2" - every second frame') |
|
|
loop_count = gr.Dropdown(choices=[0, 1, 2, 3, 4], value=0, label='"Loop_Count" = repeat loop append to your video') |
|
|
pingpong_checkbox = gr.Checkbox(label='"Pingpong" = reverse append to your video', value=False) |
|
|
audio_checkbox = gr.Checkbox(label='"Audio" = enable audio', value=False) |
|
|
|
|
|
|
|
|
generate_btn = gr.Button("Check: Audio? Loop Count? Pingpong? Generate!") |
|
|
|
|
|
with gr.Column(): |
|
|
|
|
|
output_video = gr.Video(label="Generated Video") |
|
|
|
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
text = """ |
|
|
***Hyperswap_1b_256.onnx is the best (in most cases) - but model has inner bug - sometimes they produce "FAIL" swap (working, but do not do any swapping - on SOME faces. So, for stability - do test image to image face swap on one screenshot from your video first). Most stable version is Hyperswap_1a_256.onnx. |
|
|
|
|
|
***Target_Face_Index: Index_0 = First Face. To switch for another target face - switch to Index_1, Index_2, e.t.c. |
|
|
|
|
|
***Note: "1" or "2" - 'every frame' or 'every second frame' - if you have video 60fps or 48 fps - you can choose "2" to select every 2nd frame - for two time reduce total number of frames in video (got 30 fps and 24 fps video, accordingly). |
|
|
|
|
|
***Video 05 sec * 24 fps = 120 frames/720p = takes 275 sec (4.5 min) for generating. Overall: SECONDS --> MINUTES. |
|
|
|
|
|
***To cancel job - just close your browser's page. |
|
|
|
|
|
***If needed, use AdvancedLivePortrait to correct faces on video before swapping. Here is [workflow](https://openart.ai/workflows/ocelot_vibrant_0/advanced-liveportrait-for-video-as-source/hV07PExjpK3JEd6kNnkr) for ComfyUI. |
|
|
|
|
|
***Use Avidemux - simple but powerful freeware video editor. [Download](https://www.avidemux.org/nightly/) - choose win64 v2.8.2 for Windows 10. |
|
|
|
|
|
***Use MediaInfo (freeware) to get information about video file - [Download](https://mediaarea.net/en/MediaInfo) |
|
|
|
|
|
***Free and easy hosting for short mp4 files - [https://sendvid.com/](https://sendvid.com/) |
|
|
|
|
|
***"ComfyUI Reactor Video Face Swap Hyperswap running directly on Gradio. - [How to convert your any ComfyUI workflow to Gradio](https://huggingface.co/blog/run-comfyui-workflows-on-spaces) |
|
|
|
|
|
""" |
|
|
gr.Markdown(text) |
|
|
|
|
|
|
|
|
generate_btn.click( |
|
|
fn=generate_image, |
|
|
inputs=[source_image, input_video, input_index, input_faces_order_dropdown, swap_model, pingpong_checkbox, loop_count, select_every_nth, audio_checkbox], |
|
|
outputs=[output_video] |
|
|
) |
|
|
|
|
|
app.launch(share=True) |
|
|
|