小道 修改facefusion 换脸过程中产生的临时图片文件的保存目录 附代码


之前有不少的小伙伴给我的视频留言,说facefusion换脸过程中会产生很多临时的图片文件,而且这些图片默认就保存在c盘

这个目录下 C:\Users\yourname\AppData\Local\Temp\facefusion

如果你的视频很大的话,那么产生的临时文件就会占用很大的c盘空间,也许很多小伙伴的C盘空间并不富裕,所以换脸完成后这些缓存图片都会自动删除,但是还是会占用一段时间,所以我今天抽空花了5分钟修改了下

实现了 把临时图片文件放在facefusion 所在目录下的tmp这个文件夹里面。等换脸完成以后会自动删除这个文件夹。

视频演示

打开facefusion 找到这个文件filesystem.py 然后复制以下代码到里面即可

from typing import List, Optional
import glob
import os
import shutil
import tempfile
import filetype
from pathlib import Path

import facefusion.globals
from facefusion.common_helper import is_windows

if is_windows():
    import ctypes


def get_temp_frame_paths(target_path: str) -> List[str]:
    temp_frames_pattern = get_temp_frames_pattern(target_path, '*')
    return sorted(glob.glob(temp_frames_pattern))


def get_temp_frames_pattern(target_path: str, temp_frame_prefix: str) -> str:
    temp_directory_path = get_temp_directory_path(target_path)
    return os.path.join(temp_directory_path, temp_frame_prefix + '.' + facefusion.globals.temp_frame_format)


def get_temp_file_path(target_path: str) -> str:
    _, target_extension = os.path.splitext(os.path.basename(target_path))
    temp_directory_path = get_temp_directory_path(target_path)
    return os.path.join(temp_directory_path, 'temp' + target_extension)


def get_temp_directory_path(target_path: str) -> str:
    target_name, _ = os.path.splitext(os.path.basename(target_path))
    current_script_path = os.path.abspath(__file__)
    current_script_dir = os.path.dirname(current_script_path)
    parent_dir = os.path.dirname(current_script_dir)
    temp_directory_path = os.path.join(parent_dir, 'tmp')
    return os.path.join(temp_directory_path, target_name)


def create_temp(target_path: str) -> None:
    temp_directory_path = get_temp_directory_path(target_path)
    Path(temp_directory_path).mkdir(parents=True, exist_ok=True)


def move_temp(target_path: str, output_path: str) -> None:
    temp_file_path = get_temp_file_path(target_path)

    if is_file(temp_file_path):
        if is_file(output_path):
            os.remove(output_path)
        shutil.move(temp_file_path, output_path)


def clear_temp(target_path: str) -> None:
    temp_directory_path = get_temp_directory_path(target_path)
    parent_directory_path = os.path.dirname(temp_directory_path)

    if not facefusion.globals.keep_temp and is_directory(temp_directory_path):
        shutil.rmtree(temp_directory_path, ignore_errors=True)
    if os.path.exists(parent_directory_path) and not os.listdir(parent_directory_path):
        os.rmdir(parent_directory_path)


def get_file_size(file_path: str) -> int:
    if is_file(file_path):
        return os.path.getsize(file_path)
    return 0


def is_file(file_path: str) -> bool:
    return bool(file_path and os.path.isfile(file_path))


def is_directory(directory_path: str) -> bool:
    return bool(directory_path and os.path.isdir(directory_path))


def is_audio(audio_path: str) -> bool:
    return is_file(audio_path) and filetype.helpers.is_audio(audio_path)


def has_audio(audio_paths: List[str]) -> bool:
    if audio_paths:
        return any(is_audio(audio_path) for audio_path in audio_paths)
    return False


def is_image(image_path: str) -> bool:
    return is_file(image_path) and filetype.helpers.is_image(image_path)


def has_image(image_paths: List[str]) -> bool:
    if image_paths:
        return any(is_image(image_path) for image_path in image_paths)
    return False


def is_video(video_path: str) -> bool:
    return is_file(video_path) and filetype.helpers.is_video(video_path)


def filter_audio_paths(paths: List[str]) -> List[str]:
    if paths:
        return [path for path in paths if is_audio(path)]
    return []


def filter_image_paths(paths: List[str]) -> List[str]:
    if paths:
        return [path for path in paths if is_image(path)]
    return []


def resolve_relative_path(path: str) -> str:
    return os.path.abspath(os.path.join(os.path.dirname(__file__), path))


def list_directory(directory_path: str) -> Optional[List[str]]:
    if is_directory(directory_path):
        files = os.listdir(directory_path)
        files = [Path(file).stem for file in files if not Path(file).stem.startswith(('.', '__'))]
        return sorted(files)
    return None


def sanitize_path_for_windows(full_path: str) -> Optional[str]:
    buffer_size = 0

    while True:
        unicode_buffer = ctypes.create_unicode_buffer(buffer_size)
        buffer_threshold = ctypes.windll.kernel32.GetShortPathNameW(full_path, unicode_buffer, buffer_size)  # type:ignore[attr-defined]

        if buffer_size > buffer_threshold:
            return unicode_buffer.value
        if buffer_threshold == 0:
            return None
        buffer_size = buffer_threshold


然后关闭run.bat 重新启动即可,如果担心复制错了 

可以下载这个文件 然后覆盖文件也可以


https://url24.ctfile.com/f/24666824-1315076998-5a8ea3?p=4440 (访问密码: 4440)

我使用的是facefusion 2.6.1 其他版本未做测试

可以去下面的地址 下载facefusion 2.6.1

https://www.jian27.com/html/835.html


扫描下方二维码 关注我的微信公众号 #关注微信公众号 - 更多福利 .png