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.
79 lines
2.4 KiB
79 lines
2.4 KiB
1 year ago
|
import os
|
||
|
import random
|
||
|
|
||
|
from Util import ConfigUtil
|
||
|
from Util.ComfyUIUtil import *
|
||
|
from Util.CommonUtil import *
|
||
|
|
||
|
|
||
|
def C16(prompt_data, img, source_code, target_code):
|
||
|
# 图生图
|
||
|
prompt_data["22"]["inputs"]["image"] = img
|
||
|
|
||
|
# 替换关键词
|
||
|
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 generate_random_number(length):
|
||
|
return ''.join(random.choices('0123456789', k=length))
|
||
|
|
||
|
|
||
|
def runComfyUI(json_file, model_id, task_type_code, input_image, source_code, target_code):
|
||
|
# 配置文件
|
||
|
config = ConfigUtil.getConfig()
|
||
|
server_address = config.get('comfyui', 'server_address')
|
||
|
|
||
|
# 创建目标目录
|
||
|
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())
|
||
|
|
||
|
# 上传图片
|
||
|
with open(input_image, "rb") as f:
|
||
|
img = upload_file(server_address, f, "", True)
|
||
|
|
||
|
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)
|
||
|
|
||
|
# 生成
|
||
|
generate_clip(server_address, prompt_data, client_id, output_path, myfilter)
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
# 打开配置文件
|
||
|
config = ConfigUtil.getConfig()
|
||
|
# 不使用代理
|
||
|
set_http_proxy("")
|
||
|
|
||
|
# 第20个少惠林学员
|
||
|
for k in range(49, 50):
|
||
|
runComfyUI(json_file='../JSON/16.json',
|
||
|
model_id=16,
|
||
|
task_type_code='User',
|
||
|
input_image='../ShlImages/shl_xs_' + str(k) + '.jpg',
|
||
|
source_code=['1 girl'],
|
||
|
target_code=['1 boy']
|
||
|
#target_code=['1girl']
|
||
|
)
|
||
|
|