你的浏览器不支持canvas

做你害怕做的事情,然后你会发现,不过如此。

Docker部署Wav2Lip数字人

时间: 作者: 黄运鑫

本文章属原创文章,未经作者许可,禁止转载,复制,下载,以及用作商业用途。原作者保留所有解释权。


准备

  • 1.主机安装docker
  • 2.浏览器访问GitHub地址https://github.com/Rudrabha/Wav2Lip,下载master分支的代码
  • 3.将代码上传到主机,上传后我的代码根目录是/home/deploy/Wav2Lip/master
  • 4.下载模型,我下载的是Wav2Lip,下载后上传到主机/home/deploy/Wav2Lip/master/checkpoints下,文件名是wav2lip.pth。这个模型嘴部比较模糊,部署后可以换其他模型
  • 5.准备一个5-10秒的头像视频,文件名为video.mp4,上传到/home/deploy/Wav2Lip/master/temp
  • 6.准备一个5-10秒的音频文件,文件名为voice.mp3,上传到/home/deploy/Wav2Lip/master/temp

构建镜像

  • /home/deploy/Wav2Lip下创建文件Dockerfile,内容如下:
FROM python:3.6

#修改时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
	&& echo 'Asia/Shanghai' >/etc/timezone

RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list

#安装ffmpeg
RUN apt update
RUN apt install ffmpeg -y

#拷贝代码用于安装依赖
COPY master /app

# 设置工作目录
WORKDIR /app

# 安装依赖,使用--verbose显示安装过程
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r requirements.txt --verbose

#安装低版本opencv-python-headless,解决AttributeError: module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'报错
RUN pip install "opencv-python-headless<4.3" -i https://pypi.tuna.tsinghua.edu.cn/simple/
  • 执行docker build -t wav2lip:py3.6 .构建镜像
  • 构建镜像时,执行到pip install安装依赖会非常慢。由于国内的原因,很多链接访问不到导致超时,等待两三个小时都安装不好,第一个超时的Downloading如下:
 => CACHED [7/9] WORKDIR /app
 => [8/9] RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r requirements.txt --verbose
 => => #   -- Performing Test HAVE_CXX_WNO_IMPLICIT_CONST_INT_FLOAT_CONVERSION - Failed
 => => #   -- Performing Test HAVE_CXX_WNO_CLASS_MEMACCESS
 => => #   -- Performing Test HAVE_CXX_WNO_CLASS_MEMACCESS - Success
 => => #   -- libva: missing va.h header (VA_INCLUDE_DIR)
 => => #   -- IPPICV: Downloading ippicv_2021.10.0_lnx_intel64_20230919_general.tgz from https://raw.githubusercontent.com/opencv/opencv_3rdparty/0cc4aa06bf2bef4b05d237c69a5a96b9cd0cb85a/ipp
 => => # icv/ippicv_2021.10.0_lnx_intel64_20230919_general.tgz
  • 解决办法是实现科学上网,测试网络可用后,使用网络代理构建镜像,命令如下(用了大概50分钟才构建完成):、
docker build -t wav2lip:py3.6 . --network host --build-arg HTTP_PROXY=http://127.0.0.1:10809 --build-arg HTTPS_PROXY=http://127.0.0.1:10809

启动容器

  • 镜像构建完成后启动容器,命令如下:
docker run --network=host --restart always -it -d \
--gpus "all" \
-v /home/deploy/Wav2Lip/master:/app \
--name wav2lip \
wav2lip:py3.6
  • 执行docker ps命令查看容器是否启动成功,结果如下:
[root@localhost Wav2Lip]# docker ps
CONTAINER ID   IMAGE          COMMAND     CREATED        STATUS       PORTS    NAMES
968c8060de43   wav2lip:py3.6  "python3"   5 seconds ago  Up 4 seconds          wav2lip

生成视频

  • 执行docker exec -it wav2lip /bin/bash进入容器,然后执行cd /app进入目录
  • 执行生成视频命令如下:
python inference.py --checkpoint_path checkpoints/wav2lip.pth --face temp/video.mp4 --audio temp/voice.mp3
  • 等待执行完成,查看results目录下有result_voice.mp4文件说明执行成功,执行过程如下:
root@localhost:/app# python inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face temp/video2.mp4 --audio temp/voice.mp3
Using cuda for inference.
Reading video frames...
Number of frames available for inference: 288
Extracting raw audio...
ffmpeg version 4.3.6-0+deb11u1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 10 (Debian 10.2.1-6)
  configuration: --prefix=/usr --extra-version=0+deb11u1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-pocketsphinx --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0x55d99a6f7a80] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'temp/voice.mp3':
  Duration: 00:00:21.84, start: 0.000000, bitrate: 48 kb/s
    Stream #0:0: Audio: mp3, 24000 Hz, mono, fltp, 48 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'temp/temp.wav':
  Metadata:
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 24000 Hz, mono, s16, 384 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
[mp3float @ 0x55d99a716340] overread, skip -5 enddists: -4 -4
[mp3float @ 0x55d99a716340] overread, skip -8 enddists: -5 -5
[mp3float @ 0x55d99a716340] overread, skip -7 enddists: -3 -3
[mp3float @ 0x55d99a716340] overread, skip -5 enddists: -1 -1
[mp3float @ 0x55d99a716340] overread, skip -6 enddists: -2 -2
[mp3float @ 0x55d99a716340] overread, skip -6 enddists: -4 -4
size=    1024kB time=00:00:21.84 bitrate= 384.0kbits/s speed= 489x    
video:0kB audio:1024kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.007440%
(80, 1748)
Length of mel chunks: 523
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 18/18 [00:06<00:00,  2.69it/s]
Load checkpoint from: checkpoints/wav2lip_gan.pth██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 18/18 [00:06<00:00,  4.01it/s]
Model loaded
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:13<00:00,  2.74s/it]
ffmpeg version 4.3.6-0+deb11u1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 10 (Debian 10.2.1-6)
  configuration: --prefix=/usr --extra-version=0+deb11u1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-pocketsphinx --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'temp/temp.wav':
  Metadata:
    encoder         : Lavf58.45.100
  Duration: 00:00:21.84, bitrate: 384 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 24000 Hz, mono, s16, 384 kb/s
Input #1, avi, from 'temp/result.avi':
  Metadata:
    encoder         : Lavf58.35.101
  Duration: 00:00:21.73, start: 0.000000, bitrate: 395 kb/s
    Stream #1:0: Video: mpeg4 (Simple Profile) (DIVX / 0x58564944), yuv420p, 268x444 [SAR 1:1 DAR 67:111], 389 kb/s, 24.07 fps, 24.08 tbr, 24.07 tbn, 2407 tbc
Stream mapping:
  Stream #1:0 -> #0:0 (mpeg4 (native) -> h264 (libx264))
  Stream #0:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x55d5d8c53580] -qscale is ignored, -crf is recommended.
[libx264 @ 0x55d5d8c53580] using SAR=1/1
[libx264 @ 0x55d5d8c53580] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x55d5d8c53580] profile High, level 2.1, 4:2:0, 8-bit
[libx264 @ 0x55d5d8c53580] 264 - core 160 r3011 cde9a93 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=14 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=24 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'results/result_voice.mp4':
  Metadata:
    encoder         : Lavf58.45.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p(progressive), 268x444 [SAR 1:1 DAR 67:111], q=-1--1, 24.08 fps, 18496 tbn, 24.08 tbc
    Metadata:
      encoder         : Lavc58.91.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 24000 Hz, mono, fltp, 69 kb/s
    Metadata:
      encoder         : Lavc58.91.100 aac
frame=  523 fps=0.0 q=-1.0 Lsize=     759kB time=00:00:21.84 bitrate= 284.7kbits/s speed=23.5x    
video:530kB audio:217kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.767610%
[libx264 @ 0x55d5d8c53580] frame I:3     Avg QP:19.23  size:  9958
[libx264 @ 0x55d5d8c53580] frame P:254   Avg QP:21.49  size:  1793
[libx264 @ 0x55d5d8c53580] frame B:266   Avg QP:25.12  size:   212
[libx264 @ 0x55d5d8c53580] consecutive B-frames: 22.4% 22.6% 20.7% 34.4%
[libx264 @ 0x55d5d8c53580] mb I  I16..4: 11.2% 76.5% 12.3%
[libx264 @ 0x55d5d8c53580] mb P  I16..4:  0.7%  4.3%  0.3%  P16..4: 27.6% 10.6%  6.4%  0.0%  0.0%    skip:50.1%
[libx264 @ 0x55d5d8c53580] mb B  I16..4:  0.1%  0.4%  0.0%  B16..8: 26.2%  1.1%  0.1%  direct: 0.2%  skip:72.0%  L0:50.9% L1:44.4% BI: 4.7%
[libx264 @ 0x55d5d8c53580] 8x8 transform intra:80.4% inter:74.3%
[libx264 @ 0x55d5d8c53580] coded y,uvDC,uvAC intra: 71.3% 69.0% 14.6% inter: 8.7% 7.1% 0.1%
[libx264 @ 0x55d5d8c53580] i16 v,h,dc,p: 34% 30% 14% 23%
[libx264 @ 0x55d5d8c53580] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 27% 27%  3%  2%  3%  2%  3%  3%
[libx264 @ 0x55d5d8c53580] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 47% 21% 11%  3%  3%  6%  3%  4%  2%
[libx264 @ 0x55d5d8c53580] i8c dc,h,v,p: 39% 24% 32%  4%
[libx264 @ 0x55d5d8c53580] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x55d5d8c53580] ref P L0: 70.8% 12.7% 10.9%  5.5%
[libx264 @ 0x55d5d8c53580] ref B L0: 83.4% 13.1%  3.5%
[libx264 @ 0x55d5d8c53580] ref B L1: 98.5%  1.5%
[libx264 @ 0x55d5d8c53580] kb/s:199.50
[aac @ 0x55d5d8c4e3c0] Qavg: 5514.899
  • 生成视频时,报错module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' 如下:
root@localhost:/app# python inference.py --checkpoint_path checkpoints/wav2lip.pth --face /home/image.jpg --audio /home/voice.mp3
Traceback (most recent call last):
  File "inference.py", line 3, in <module>
    import scipy, cv2, os, sys, argparse, audio
  File "/usr/local/lib/python3.6/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/usr/local/lib/python3.6/site-packages/cv2/__init__.py", line 175, in bootstrap
    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "/usr/local/lib/python3.6/site-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.6/site-packages/cv2/gapi/__init__.py", line 323, in <module>
    cv.gapi.wip.GStreamerPipeline = cv.gapi_wip_gst_GStreamerPipeline
AttributeError: module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'
  • 这个问题在https://github.com/Rudrabha/Wav2Lip/issues/387得到解决,需要在容器内安装低版本的opencv-python-headless
  • 在容器中执行安装命令pip install "opencv-python-headless<4.3" -i https://pypi.tuna.tsinghua.edu.cn/simple/

对于本文内容有问题或建议的小伙伴,欢迎在文章底部留言交流讨论。