AI 文摘

修复SadTalker无法在新版本StableDiffusionWebUI中运行的问题





作者: 萤火遛AI 来源: 萤火遛AI

大家好,我是每天分享AI应用的萤火君!

使用 Stable Diffusion WebUI 的同学可能会发现一个问题,升级到新版本之后(大约是1.9.0之后的版本),原本可以正常使用的 SadTalker 生成时报错了,用不了了。本文就来分析和解决这个问题。

简单介绍下SadTalker:SadTalker 可以说是比较早的开源数字人生产工具,只需要一张人物的照片和语音,我们就可以生成一个人物说话的视频。虽然现在已经有效果更好、更丰富的各种数字人产品,不过SadTalker还是有一些用处的。

言归正传,SadTalker在新版 Stable Diffusion WebUI 中到底发生了什么问题呢?

分析问题

即然它能运行在老版本的 Stable Diffusion WebUI 中,我猜就是程序兼容性的问题了。

那么到底哪里不兼容呢?程序出了问题,我们一般要先看看错误信息是什么。

前端页面中只显示了一个 Error,具体的错误还得看控制台中输出的日志,我这里给大家粘贴出来:

File "/root/SDWebUI/program/extensions/SadTalker/src/face3d/util/my_awing_arch.py", line 18, in calculate_points  
    preds = preds.astype(np.float, copy=False)  
  File "/root/miniconda3/envs/SDWebUI/lib/python3.10/site-packages/numpy/__init__.py", line 324, in __getattr__  
    raise AttributeError(__former_attrs__[attr])  
AttributeError: module 'numpy' has no attribute 'float'.  
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.  
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:  
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations. Did you mean: 'cfloat'?

看不懂英文的同学不要害怕,可以找个工具自行翻译下。

这个错误从上往下看是SadTalk中的这个文件报错了,位置是第18行:

SadTalker/src/face3d/util/my_awing_arch.py

继续往下看是 Python3.10 中的 numpy 包抛出的错误:

python3.10/site-packages/numpy/init.py

再往下是详细的错误说明,大概是说程序使用了 numpy 包中不存在的属性 float,np.float 是一个过期的别名:

AttributeError: module ’numpy’ has no attribute ‘float’.

np.float was a deprecated alias for the builtin float.

而且错误说明还给了一个明确的解决方案,直接使用 float 或者使用 np.float64。

To avoid this error in existing code, use float by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.float64 here.

我们做个总结:SadTalk使用了 numpy 中的一个被过期删除的属性,然后程序报错了,解决方案是换个用法。

另外,这个问题仅在新版本的 Stable Diffusion WebUI 中出现,可以猜测是新版 SD WebUI 依赖的 numpy 版本升级了,而 SadTalker 没有同步升级,还在使用旧的 numpy。

解决问题

既然问题说的这么明白了,解决起来就很简单了。

我们不能将 numpy 降低到旧的版本,因为有的程序需要新版本,那就只能修改 SadTalker 使用新的方式了。

找到 src/face3d/util/my_awing_arch.py 第18行:

preds = preds.astype(np.float, copy=False)

修改为:

preds = preds.astype(float, copy=False)

除了这个地方,还有一处需要修改:

src/face3d/util/preprocess.py 第101行

trans_params = np.array([w0, h0, s, t[0], t[1]])

修改为:

trans_params = np.array([w0, h0, s, t[0][0], t[1][0]])

这个问题也是 numpy 版本升级导致的,t[0] 和 t[1] 是两个序列,不能直接作为数组中的元素。根据上下文我们使用索引取其中的第一个元素就可以了。

修改了这两处代码,SadTalker 又能正确的生成视频了。

安装插件

修改后的 SadTalker 我也发布在 Github上了,大家也可以直接使用下面这个链接来安装插件:

https://github.com/bosima/SadTalker

安装成功之后别忘了重启 Stable Diffusion WebUI。

以上就是本文的主要内容,如有问题欢迎留言。

更多AI工具,参考Github-AiBard123国内AiBard123

可关注我们的公众号:每天AI新工具