File size: 5,834 Bytes
0abaca2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import os
import subprocess
import locale
import winreg
import argparse
import inspect
import ast
abs_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
git = os.path.join(abs_path, "s", "git", "cmd", "git.exe")
master_path = os.path.join(abs_path, "p")
python = os.path.join(abs_path, "s", "python", "python.exe")
parser = argparse.ArgumentParser()
parser.add_argument('-wf', '--webcam-false', action='store_true')
parser.add_argument('-wt', '--webcam-true', action='store_true')
parser.add_argument('-mb', '--master-branch', action='store_true')
parser.add_argument('-nb', '--next-branch', action='store_true')
parser.add_argument('-nu', '--no-update', action='store_true')
args = parser.parse_args()
def run_git_command(args):
subprocess.run([git] + args, check=True)
def uncensore(branch):
functions_to_modify = {
"analyse_frame": False,
"analyse_stream": False,
"forward": 0.0,
"prepare_frame": "frame",
"analyse_video": False,
"analyse_image": False
}
file_path = os.path.join(master_path, branch, 'facefusion', 'content_analyser.py')
if not os.path.exists(file_path):
print(f"Файл {file_path} не найден.")
return
with open(file_path, 'r', encoding='utf-8') as file:
source_code = file.read()
tree = ast.parse(source_code)
class ModifyFunctions(ast.NodeTransformer):
def visit_FunctionDef(self, node):
if node.name in functions_to_modify:
return_value = functions_to_modify[node.name]
if isinstance(return_value, bool):
node.body = [ast.Return(value=ast.Constant(value=return_value))]
elif isinstance(return_value, float):
node.body = [ast.Return(value=ast.Constant(value=return_value))]
elif isinstance(return_value, str) and return_value == "frame":
node.body = [ast.Return(value=ast.Name(id='vision_frame', ctx=ast.Load()))]
return node
modified_tree = ModifyFunctions().visit(tree)
modified_code = ast.unparse(modified_tree)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(modified_code)
def prepare_environment(branch):
branch_path = os.path.join(master_path, branch)
os.chdir(branch_path)
if not args.no_update:
run_git_command(['reset', '--hard'])
run_git_command(['checkout', branch])
run_git_command(['pull', 'origin', branch, '--rebase'])
uncensore(branch)
def ask_webcam_mode(language):
webcam_choice = input(get_localized_text(language, "enable_webcam")).strip().lower()
if webcam_choice in ["y", "д"]:
return True
elif webcam_choice in ["n", "н"]:
return False
return None
def start_ff(branch, webcam_mode=False):
path_to_branch = os.path.join(master_path, branch)
second_file = os.path.join(path_to_branch, "facefusion.py")
args = ["run", "--open-browser", "--ui-layouts", "webcam"] if webcam_mode else ["run", "--open-browser"]
cmd = f'"{python}" "{second_file}" {" ".join(args)}'
subprocess.run(cmd, shell=True, check=True)
def get_localized_text(language, key):
texts = {
"en": {
"choose_action": "Choose an action:",
"update_master": "1. Update to the master branch and start it",
"update_next": "2. Update to the next branch and start it",
"enter_choice": "Enter the number of the action: ",
"invalid_choice": "Invalid choice, please try again.",
"enable_webcam": "Enable webcam mode? (Y/N): ",
},
"ru": {
"choose_action": "Выберите действие:",
"update_master": "1. Обновить до обычной ветки и запустить ее (master)",
"update_next": "2. Обновить до бета ветки и запустить ее (next)",
"enter_choice": "Введите номер действия: ",
"invalid_choice": "Неверный выбор, попробуйте снова.",
"enable_webcam": "Включить режим вебкамеры? (Y/N): ",
}
}
return texts[language].get(key, "")
def get_system_language():
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Control Panel\International")
language = winreg.QueryValueEx(key, "LocaleName")[0]
winreg.CloseKey(key)
return "ru" if language.split('-')[0].lower() == "ru" else "en"
except WindowsError:
return "ru" if locale.getlocale()[0].split('_')[0].lower() == "ru" else "en"
def facefusion():
language = get_system_language()
if args.master_branch or args.next_branch:
branch = "master" if args.master_branch else "next"
prepare_environment(branch)
webcam_mode = True if args.webcam_true else False if args.webcam_false else ask_webcam_mode(language)
start_ff(branch, webcam_mode)
return
while True:
print(get_localized_text(language, "choose_action"))
print(get_localized_text(language, "update_master"))
print(get_localized_text(language, "update_next"))
choice = input(get_localized_text(language, "enter_choice")).strip()
if choice in ['1', '2']:
branch = "master" if choice == '1' else "next"
prepare_environment(branch)
webcam_mode = ask_webcam_mode(language)
start_ff(branch, webcam_mode)
break
else:
print(get_localized_text(language, "invalid_choice"))
if __name__ == "__main__":
facefusion() |