You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.9 KiB

import os
from Util import ConfigUtil
from Util.SDUtil import *
from natsort import natsorted
def C16(prompt_data, img, source_code, target_code, model_id, user_id):
# 图生图
prompt_data["22"]["inputs"]["image"] = img[0]
# 替换关键词
for i in range(len(source_code)):
s = source_code[i]
t = target_code[i]
prompt_data["331"]["inputs"]["text"] = prompt_data["331"]["inputs"]["text"].replace(s, t)
# 过滤器,哪些返回节点中获取到的图片是有用的
myfilter = []
# 遍历prompt_data中的所有节点
for key, value in prompt_data.items():
if 'inputs' in value:
if 'filename_prefix' in value['inputs']:
if value['inputs']['filename_prefix'] == 'ComfyUI':
myfilter.append(key)
return prompt_data, myfilter
def runComfyUI(json_file, model_id, task_type_code, input_image, source_code, target_code, user_id):
# 创建目标目录
output_path = "../Out/Images/" + task_type_code + "/" + str(model_id) + "/"
if not os.path.exists(output_path):
os.makedirs(output_path)
# 生成一个唯一的客户端ID
client_id = str(uuid.uuid4())
# 上传图片
img = []
for x in input_image:
with open(x, "rb") as f:
y = upload_file(server_address, f, "", True)
img.append(y)
with open(json_file, 'r', encoding="utf-8") as fi:
prompt_data = json.load(fi)
prompt_data, myfilter = C16(prompt_data, img, source_code, target_code, model_id, user_id)
# 生成
files = generate_clip(server_address, prompt_data, client_id, output_path, myfilter)
print(files)
if __name__ == '__main__':
# 配置文件
config = ConfigUtil.getConfig()
# 处理机编号
machine_id = config['system']['machine_id']
# 生图服务地址
txt2img_url = config['webui']['txt2img_url']
# WEB服务器地址
web_url = config['webui']['web_url']
# 配置文件
server_address = config.get('comfyui', 'server_address')
# 需要替换的提示词
source_code = ['']
# 替换后的提示词
target_code = ['']
# 模板号
model_id = 16
# 遍历少惠林所有学生
# input_image = ['../ShlImages/shl_xs_2.jpg']
# 指定目录路径
directory = '../ShlImages/'
# 获取目录下的所有文件和目录的完整路径
entries = [directory+entry for entry in os.listdir(directory)]
# 使用natsort进行自然排序
input_image = natsorted(entries)
# 使用哪个文件
json_file = '../JSON/' + str(model_id) + '.json'
user_id = str(uuid.uuid4())
# 请求
runComfyUI(json_file=json_file,
model_id=model_id,
task_type_code='User',
input_image=input_image,
source_code=source_code,
target_code=target_code, user_id=user_id)