
随着AIGC的爆火,图片生成技术得到飞速发展,当前AI生成的图片已达到真假难辨的高保真度。例如stable diffusion与midjourney为代表的文生图大模型。不过,当合成图片中出现文字内容时,现存的AI技术依然无法驾驭文字内容。因此,modescope提出了一种新型的文字生成方法,此方法称之为AnyTex。AnyTex可以支持中文、英语、日语、韩语等多语言的文字生成,还支持对输入图片中的文字内容进行编辑。

AnyText主要基于扩散(Diffusion)模型,包含两个核心模块:
隐空间辅助模块(Auxiliary Latent Module):隐空间辅助模块对三类辅助信息(字形、文字位置和掩码图像)进行编码并构建隐空间特征图像,用来辅助视觉文字的生成;

文本嵌入模块(Text Embedding Module):文本嵌入模块则将描述词中的语义部分与待生成文本的字形部分解耦,使用图像编码模块单独提取字形信息后再与语义信息做融合,既有助于文字的书写精度,也有利于提升文字与背景的一致性。

训练阶段,除了使用扩散模型常用的噪声预测损失,还增加了文本感知损失,在图像空间对每个生成文本区域进行像素级的监督,以进一步提升文字书写精度。

此模型我们可以直接在modelscope社区进行在线demo的使用,其在浏览器中输入如下地址:
https://modelscope.cn/studios/damo/studio_anytext/summary
便可以看到体验demo了,其模型提供了几个实例,我们可以直接运行与修改prompt的文字,当然也可以自行写自己的prompt。

模型支持文本生成与文本编辑
文本生成:在【提示】输入框中输入要生成的图片的文字描述(中文或英文)。 需要生成的每个文本行都应该用双引号引起来。 然后,手动绘制每个文本行的指定位置以生成图像。 文字位置的绘制对于最终图像的质量至关重要,请勿绘制得太随意或太小。 位置的数量应与文本行的数量相匹配,并且每个位置的大小应尽可能与相应文本行的长度或宽度匹配。 如果【手动绘制】不方便,可以尝试拖动矩形【手动矩形】或随机位置【自动绘制】。

生成多行时,每个位置按照一定的规则与文本行进行匹配。 [排序位置]选项用于确定是否优先从上到下或从左到右排序。 也可以打开参数设置中的【Show Debug】选项来观察结果中的文本位置和字形图像。 还可以选择[修改位置],使用渲染文本的边界框作为修改位置。

文本编辑:
[Ref]中的图像作为参考图像,然后调整画笔大小,并标记要编辑的区域。 在【提示】中输入文字描述以及要修改的新文字,然后生成图像。

这里输入2张图片,一张原始图片,一张需要把文字进行mask的图片。然后在prompt中输入需要需要修改的文字即可。

除了在线进行demo的使用外,也可以使用代码进行使用。

# 安装git(如有请跳过)conda install -c anaconda git# 克隆anytext仓库git clone https://github.com/tyxsspa/AnyText.gitcd AnyText# 准备字库文件(推荐Arial Unicode MS,需自行下载)mv your/path/to/arialuni.ttf ./font/Arial_Unicode.ttf# 方式一:如果使用modelscope notebook最新镜像(ubuntu22.04-cuda11.8.0-py310-torch2.1.0-tf2.14.0-1.10.0),直接安装如下包即可pip install Pillow==9.5.0pip install gradio==3.50.0# 方式二:重新创建一个独立虚拟环境(耗时较长)conda env create -f environment.yamlconda activate anytext
使用前需要进行相关库的安装以及相关使用环境的搭建。然后输入如下代码就可以使用了。

from modelscope.pipelines import pipelinefrom util import save_imagespipe = pipeline('my-anytext-task', model='damo/cv_anytext_text_generation_editing', model_revision='v1.1.1', use_fp16=True, use_translator=False)img_save_folder = "SaveImages"params = { "show_debug": True, "image_count": 2, "ddim_steps": 20,}# 1. text generationmode = 'text-generation'input_data = { "prompt": 'photo of caramel macchiato coffee on the table, top-down perspective, with "Any" "Text" written on it using cream', "seed": 66273235, "draw_pos": 'example_images/gen9.png'}results, rtn_code, rtn_warning, debug_info = pipe(input_data, mode=mode, **params)if rtn_code >= 0: save_images(results, img_save_folder)# 2. text editingmode = 'text-editing'input_data = { "prompt": 'A cake with colorful characters that reads "EVERYDAY"', "seed": 8943410, "draw_pos": 'example_images/edit7.png', "ori_image": 'example_images/ref7.jpg'}results, rtn_code, rtn_warning, debug_info = pipe(input_data, mode=mode, **params)if rtn_code >= 0: save_images(results, img_save_folder)print(f'Done, result images are saved in: {img_save_folder}')

更多内容可以参考modescope社区相关模型,也可自行在线demo使用。
https://modelscope.cn/studios/damo/studio_anytext/summaryttps://github.com/tyxsspa/AnyTexthttps://arxiv.org/abs/2311.03054https://modelscope.cn/models/damo/cv_anytext_text_generation_editing/summary