1 Star 0 Fork 0

fulehua/facechain

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
run_inference.py 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
tastelikefeet 提交于 2023-08-18 20:47 . Feat/refine code (#59)
# Copyright (c) Alibaba, Inc. and its affiliates.
import os
from facechain.inference import GenPortrait
import cv2
from modelscope import snapshot_download
from facechain.constants import neg_prompt, pos_prompt_with_cloth, pos_prompt_with_style, styles, cloth_prompt
def generate_pos_prompt(style_model, prompt_cloth):
if style_model == styles[0]['name'] or style_model is None:
pos_prompt = pos_prompt_with_cloth.format(prompt_cloth)
else:
matched = list(filter(lambda style: style_model == style['name'], styles))
if len(matched) == 0:
raise ValueError(f'styles not found: {style_model}')
matched = matched[0]
pos_prompt = pos_prompt_with_style.format(matched['add_prompt_style'])
return pos_prompt
use_main_model = True
use_face_swap = True
use_post_process = True
use_stylization = False
processed_dir = './processed'
num_generate = 5
base_model = 'ly261666/cv_portrait_model'
revision = 'v2.0'
multiplier_style = 0.25
base_model_sub_dir = 'film/film'
train_output_dir = './output'
output_dir = './generated'
use_style = False
if not use_style:
style_model_path = None
pos_prompt = generate_pos_prompt(styles[0]['name'], cloth_prompt[0]['prompt'])
else:
model_dir = snapshot_download(styles[1]['model_id'], revision=styles[1]['revision'])
style_model_path = os.path.join(model_dir, styles[1]['bin_file'])
pos_prompt = generate_pos_prompt(styles[1]['name'], styles[1]['add_prompt_style']) # style has its own prompt
gen_portrait = GenPortrait(pos_prompt, neg_prompt, style_model_path, multiplier_style, use_main_model,
use_face_swap, use_post_process,
use_stylization)
outputs = gen_portrait(processed_dir, num_generate, base_model,
train_output_dir, base_model_sub_dir, revision)
os.makedirs(output_dir, exist_ok=True)
for i, out_tmp in enumerate(outputs):
cv2.imwrite(os.path.join(output_dir, f'{i}.png'), out_tmp)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fulehua/facechain.git
git@gitee.com:fulehua/facechain.git
fulehua
facechain
facechain
dev/add_hf_readme

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385