(ML) 利用 VIBE 製作虛擬解說員

YEN HUNG CHENG
18 min readOct 28, 2023

--

VIBE: Video Inference for Human Body Pose and Shape Estimation [CVPR-2020]

GitHub

步驟如下:

  1. 使用 VIBE 將影片轉成 pkl
  2. pkl 轉 fbx
  3. 使用 Blender 調整 fbx
  4. 匯入 Unity 做處理

⭐️Unity 操作到到一半 Scene 消失
How to add expression animation to MMD model in Unity

環境:

Ubuntu 20.04

macOS 13.6

Usage — Ubuntu 20.04

git clone https://github.com/mkocabas/VIBE.git
cd VIBE
conda create -n vibe python=3.7
conda activate vibe

Install package

pip install git+https://github.com/giacaglia/pytube.git --upgrade

Revise requirements.txt

tqdm==4.28.1
yacs==0.1.6
h5py==2.10.0
numpy==1.17.5
scipy==1.4.1
numba==0.47.0
smplx==0.1.26
gdown==4.5.1
PyYAML==5.3.1
joblib==0.14.1
pillow==7.1.0
trimesh==3.5.25
pyrender==0.1.36
progress==1.5
filterpy==1.4.5
matplotlib==3.1.3
tensorflow==1.15.4
tensorboard==1.15.0
scikit-image==0.16.2
scikit-video==1.1.11
opencv-python==4.1.2.30
llvmlite==0.32.1
git+https://github.com/mattloper/chumpy.git
git+https://github.com/mkocabas/yolov3-pytorch.git
git+https://github.com/mkocabas/multi-person-tracker.git

Remove torchvision

pip install -r requirements.txt

Install Pytorch

# RTX 30系列
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
# RTX 40系列
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.7 -c pytorch -c nvidia

Install according to the locally available CUDA version

Prepare data

mkdir -p data
cd data
# Manually download data from the cloud
unzip vibe_data.zip
rm vibe_data.zip
cd ..
mv data/vibe_data/sample_video.mp4 .
mkdir -p $HOME/.torch/models/
mv data/vibe_data/yolov3.weights $HOME/.torch/models/

Running the Demo

# Run on a local video
python demo.py --vid_file sample_video.mp4 --output_folder output/ --display

⭐️沒意外的話,會出現許多錯誤需要解決

Error pyglet 2.0.9 requires Python 3.8 or newer

pip install pyglet==1.5.26

No such file or directory: ‘ffmpeg’: ‘ffmpeg’

# 雖然使用 pip show ffmpeg 會顯示有套件,但是會失敗,所以必須執行下方的指令
sudo apt install ffmpeg

CUDA error: no kernel image is available for execution on the device

# 直接重裝 pytorch , cuda 11.3 配合本地端
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

ffmpeg: error while loading shared libraries: libopenh264.so.5

sudo apt-get remove ffmpeg
sudo apt-get purge ffmpeg

conda remove ffmpeg

sudo apt-get install ffmpeg

# 在 linux 沒意外,要再重裝 torch
# 直接重裝 pytorch , cuda 11.3 配合本地端
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

CUDA out of memory

開啟 demo.py 找到 308 行中的 batch size 將 default 調低 450 → 200

若都正常執行的話,最後會在 output/sample_vide 中看到兩個檔案,有兩個檔案分別為 mp4 與 pkl,在 Linux 上無法輸出 mp4 檔案,目前無解且錯誤是上述有提過的,也就是 ffmpeg: error while loading shared libraries: libopenh264.so.5 ,但是最主要有 pkl 檔案就可以了,所以就跳過不處理,還有最神奇的地方就是,你可能執行一次 demo.py 後,出現這個錯誤 AttributeError: ‘NoneType’ object has no attribute ‘shape’ ,這個錯誤請用重新安裝 ffmpeg 解決。

pkl to fbx

到 SMPL 先註冊帳號,並且認證要等 30 分鐘以上

經過漫長的等待後,登入後點選 Download 頁面,並拉到最下面有 SMPL for Unity的地方,點選 Download version 1.0.0 for Unity,下載後的檔案解壓縮後放到 data 資料夾下

Liunx install bpy

pip install bpy==2.91a0 && bpy_post_install
python lib/utils/fbx_output.py --input output/sample_video/vibe_output.pkl --output output/sample_video/fbx_output.fbx --fps_source 30 --fps_target 30 --gender female 

轉好後的 fbx 檔案就在 output/sample_video 的資料夾下

Blender — macOS

開啟一個全新的專案 General

按下按鍵 a 全選所有的物件,並點選 delete 刪除所有物件

將剛剛轉檔好的 .fbx 檔案匯入

File -> Import -> FBX (.fbx),在右側的下方會有 Automatic Bone Orientation 需要打勾,再按下 import FBX

匯入後的模型是倒在地上的,所以我們等等要進行調整。

點選右下角的收尋,輸入 In Front,找到後將其打勾

選取人物

找到左上角的 Object Mode,切換成 Pose Mode,並點選人物背後的物件

按下 r 進行調整,調整好後就可以按下 Enter,接下來按下 r 再按下 y 最後輸入 90 再按下 Enter

最後是調整上下的部分,目前人有一半的身體插在地面上,所以接下來點選人物背面的那一根,按下 g 即可進行調整,最後一樣按下 Enter 即可

插入Keyframe

選取人物後面的那一根,按下 i 並選 Location&Rotation

接下來點選右鍵,選取 Duplicate,拖移你的 keyframe 到右邊的點,移動到記得按下 Enter

這時候可以點選下方的播放按鍵進行測試,但可以發現人物的動作是相當卡頓的,所以必需進一步做處理

按下 a 選取所有骨頭,並點選左下角切換到 Graph Editor,點選 Key -> Smooth -> Smooth(Legacy),最後點選 Key -> Density -> Clean Keyframes

這時候你的人物動起來就會比較順暢了,再來點選 Dope Sheet -> Action Editor -> Push Down,最後就是點選 File -> FBX 即可完成

Unity — macOS

記得要先註冊 Unity 帳號

網頁下拉後,下載 Unity Hub

打開 Unity Hub 後,點選 Installs -> install (Recommended version) -> install (取消選取 Visual Studio for Mac)

Add Licenses

點選 Preferences -> Licenses -> Add -> Get a free personal license

Create Project

點選 New Project -> 3D -> create project

下載場景

記得要先申請帳號,再點選免費下載

若是使用 Macbook 的用戶,請先下載 The Unarchiver,點選 2階建ての開放感ある美術館.zip -> 右鍵 Open With -> The Unarchiver

將解壓縮後的場景拖入專案中的 Assets,可以將 texture 取消點選,再點選 import

匯入成功後,點選建物並拖移到場景中

刪這時候可以點選左上角的建物,刪除不必要的場景

接下來就是慢慢使用滑鼠跟紅色標示的工具,移動到你的場景

將畫作拉到 Assets 中

將滑鼠移動到左方,點選右鍵 -> 3D Object -> Cube

移動到 Assets,點選右鍵 -> Create -> Material

點選 New Material -> Shader -> Texture -> 點選 Select -> 選擇匯入的圖片 -> Cube -> Materials -> New Material

調整畫作位置

匯入 3D 人物

下載 MMD4Mecanim_Beta_20200105.zip

透過下方任一網址下載 MMD 模型

記得將下載後的 MMD4Mecanim_Beta_20200105 與 模型 解壓縮

點選 Assets -> Import Package -> Custom Package.. -> MMD4Mecanim.unitypackage -> import

接下來將你下載好的 MMD 模型拖入 Assets 中

點選剛剛的資料夾,勾選右下方三個選項,點選 Process

這時會出現警示窗,打開 System Settings -> Privacy & Security -> 往下拉 -> 點選 Allow Anyway

最終完成 Process 後,就會在當前資料夾出現一個 fbx 檔案

⭐️點選檔案,Animation Type -> Humanoid

將 fbx 檔案拖入到場景中,並調整 Rotation Y -> 180

再來將我們透過 Blender 調整後的 fbx 拉到 Assets 中,記得也要設定 Animation Type -> Humanoid

加入解說音檔

先將 解説檔案拉到 Assets 中,再將滑鼠移動到左視窗,按右鍵 -> Audio -> Audio Source -> 點選 Audio Source -> AudioClip -> 選取解說檔案

錄製影片

點選左上角的 window -> Package Manager -> Unity Registry -> 安裝 Cinemachine -> 安裝 Recorder

接下來將滑鼠移動到左方,點選右鍵 -> Create Empty -> 取名為 Sequence -> window -> Sequencing -> Timeline -> 點選 Sequence -> Timeline -> Create

先將 project 拖移出來,再將場景中的人物拖移到 Timeline -> Add Animation Track

接下來將動作檔的 fbx 打開,將 Armature Armature Armature Action (Animation Clip) 拖入其中,沒意外的人會跑掉

再次調整人物的位置

滑鼠移至左下方,點擊右鍵 -> Audio Track -> 將解說檔案拖入

調整 Main Camera ,跟剛才的操作一樣,可以先輸入人物的大概位置,再來用手調整

滑鼠移至左下方,點擊右鍵 -> Cinemachine Track -> 將 Main Camera 拖入 -> Create CinemachineBrain on Main Camera -> 點擊右鍵 -> Add Cinemachine Shot

調整各個時間軸,點選 Cinemachine Shot -> Create

滑鼠移至左下方,點擊右鍵 UnityEditor.Recorder.Timeline -> Recorder Track -> 點擊右鍵 -> Add Recorder Clip -> 調整長度

簡單調整一下錄影的格式,點選齒輪 -> Frame Rate -> HD: 30fps -> Output Resolution -> HD-720p -> Quality -> Medium

最後你可以調整一下畫作的位置,點選上方的播放按鈕,即可開始錄製影片

Youtube

Unity 操作到到一半 Scene 消失

點擊 Window -> Layouts -> Reset All Layouts

How to add expression animation to MMD model in Unity

--

--