艺术字纹理 模型介绍
本模型为艺术字纹理模型,基于可控的稳定扩散模型(Stable Diffusion, SD),生成以文字为主体且带有纹理的图片素材。用户使用的时候,仅需要输入带文字的黑白图/文字内容,以及对应纹理的提示词(prompt),便可以生成带有对应文字的带纹理的图片素材。目前已经上线创空间:https://modelscope.cn/studios/WordArt/WordArt/summary
模型描述
艺术字纹理模型的整体框架如上图所示。模型首先会对输入的文字图片(如果是输入文案,会先渲染成文字图片)进行预处理,包括边缘提取等操作,文字边缘信息和用户输入的提示词,共同作为ControlNet的输入,并最终得到输出的结果。
期望模型使用方式以及适用范围
该模型适用于输入带文字的黑白图/文字内容,以及对应纹理的提示词(prompt),最终生成带有对应文字的带纹理的图片素材。需要注意的是,文字在图片中的比例不能太小(建议文字的边长站图片短边的比例不小于1/3),当文字在图片中的占比太小的时候,上纹理后的可能导致文字的笔画不清晰。
如何使用
模型推理
安装指定版本的依赖库:
# Pillow >= 10.0.0, 会导致渲染文字布局的时候出错
# Note: 最好先手动执行,依赖requirements.txt自动安装,初次安装可能会出现版本信息不一致,可以通过`import PIL; PIL.__version__`确认
pip install Pillow==9.4.0
参数说明
艺术字纹理pipeline的入参是一个字典,格式如下:
{
"image": {
"image_path_url": str
},
"text": {
"text_content": str,
"font_name": str,
"output_image_ratio": str
},
"prompt": str,
"neg_prompt": str,
"image_short_size": int,
"image_num": int,
}
每个字段具体说明如下:
- image (Object): 图片(掩膜)输入的相关字段,和text需要二选一。
- imagepathurl (str): 掩膜链接或者本地路径。
- text (Object): 文字输入的相关字段,和image需要二选一。
- text_content (str): 用户输入的文字内容,需要小于等于6个字。
- font_name (str): 使用内置字体的名称,目前支持的字体如下:
- 'dongfangdakai':阿里妈妈东方大楷;
- outputimageratio (str): 文字输入的图片的宽高比,默认为"1:1",可选的比例有:"1:1", "16:9", "9:16"。
- prompt (str): 用户输入的正向提示词。
- neg_prompt (str): 用户输入的负向提示词。
- imageshortsize (int): 生成的图片短边的长度,默认为512,取值范围为[512, 1024],若输入数值非64的倍数,则最终取值为不大于该数值的能被64整除的最大数。
- image_num (int): 生成的图片数量,默认为 1。
示例代码
基础示例代码如下:
import cv2
import os
import os.path as osp
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.outputs import OutputKeys
model_id = 'MorningsunLee/cv_sd_wordart-texttypo'
# 初始化实时检测pipeline
inference = pipeline('wordart-generation', model=model_id)
dst_img_dir = './tmp'
prompt_list = ['fruits', 'marble']
neg_prompt_list = [
'',
'',
]
img_names = ['fruits.jpg', 'marble.jpg']
if not osp.isdir(dst_img_dir):
os.makedirs(dst_img_dir)
for img_name, p, n_p in zip(img_names, prompt_list, neg_prompt_list):
# 提供文案,使用内置布局功能
input_params = {
"text": {
"text_content": "开放视觉",
"font_name": "dongfangdakai",
"output_image_ratio": "16:9"
},
"prompt": p,
"neg_prompt": n_p,
"image_short_size": 512,
"image_num": 1,
}
# 提供模板图
# input_params = {
# "image": {
# "image_path_url": "https://xxx/mask.png"
# },
# "prompt": p,
# "neg_prompt": n_p,
# "image_short_size": 512,
# "image_num": 1,
# }
output = inference(input_params)
im = output[OutputKeys.OUTPUT_IMGS][0]
dst_img_path = osp.join(dst_img_dir, img_name)
cv2.imwrite(dst_img_path, im)
自定义模型
用户可以通过修改configuration.json
文件来指定基模和lora,configuration.json
的具体格式如下:
{
"framework":"pytorch",
"task":"wordart-generation",
"pipeline":{"type":"wordart-texture"},
"model":{
"type":"wordart-texture",
"base_model_path": null,
"lora_model_paths": null,
"lora_model_ratios": null
},
"allow_remote":true
}
- Note:
configuration.json
文件一般的路径为~/.cache/modelscope/YourUserName/cv_sd_wordart-texttypo/configuration.json
;- 目前仅支持sdv1-5的模型。
自定义基模
通过修改basemodelpath字段指定基模,具体地:
{
"framework":"pytorch",
"task":"wordart-generation",
"pipeline":{"type":"wordart-texture"},
"model":{
"type":"wordart-texture",
"base_model_path": "/The/path/to/your/basemodel.safetensors",
"lora_model_paths": null,
"lora_model_ratios": null
},
"allow_remote":true
}
自定义lora
通过修改loramodelpaths和loramodelratios字段来指定lora,具体地:
{
"framework":"pytorch",
"task":"wordart-generation",
"pipeline":{"type":"wordart-texture"},
"model":{
"type":"wordart-texture",
"base_model_path": null,
"lora_model_paths": ["/The/path/to/your/lora.safetensors"],
"lora_model_ratios": [1.0]
},
"allow_remote":true
}
- Note:
- loramodelpaths和loramodelratios字段是列表类型,支持加载多个loras;
- 基模和lora相关字段可以同时指定,同时使用自定义的基模和lora。
相关数据集
结合艺术字变形,我们构建了一个变形艺术字的纹理数据集(WordART v1),可以为艺术字纹理提供一些思路,具体如下所示:
相关论文以及引用信息
如果这个工作对您有帮助,请引用以下文章:
@misc{he2023wordart,
title={wordart designer: user-driven artistic typography synthesis using large language models},
author={Jun-Yan He and Zhi-Qi Cheng and Chenyang Li and jingdong Sun and Wangmeng Xiang and Xianhui Lin, and Xiaoyang Kang and Zengke Jin and Yusen Hu and Bin Luo and Yifeng Geng and Xuansong Xie and Jingren Zhou},
journal={EMNLP},
year={2023}
}
评论