私的AI研究会 > TalkFace

音声で顔画像を動かす:One Shot Talking Face

 音声と1枚の顔画像を使って、顔画像がまるで話しているような動画を作成する技術「One Shot Talking Face」を検証する

※ 最終更新:2023/12/09 

サイト『One Shot Talking Face を使って音声で顔画像を動かす』 の検証

概要

音声と1枚の顔画像を使って、顔画像がまるで話しているような動画を作成する「One Shot Talking Face」を、上記サイトの手順に従って検証してみる。
現在の「Google Colaboratory」環境で動作する。

One Shot Talking Face 概念図

Google Colaboratory に実行環境を作成

  1. 上記サイト作者の デモサイト を開き「Open in Colab」① ボタンを押す

  2. 『one_shot_talking_face.ipynb』の Google Colab が開くので「ファイル」メニューから「ドライブにコピーを保存」を選択

  3. 『one_shot_talking_face.ipynb のコピー』のタイトルで開いた Google Colab のページで以降の操作を行う

  4. データファイルをダウンロードして解凍する(解凍した「update/work/one_shot_talking_face/」を使用する
     update_20231209.zip (115MB) <アップデート・データ>

環境設定

  1. 以下のセルを実行する ①(実行時間 1分)
    #@title #**Setup**
    !git lfs install
    %cd /content
    !git clone https://huggingface.co/camenduru/pocketsphinx-20.04-t4 pocketsphinx
    %cd  /content/pocketsphinx
    !sudo cmake --build build --target install
    %cd /content
    !git clone https://huggingface.co/camenduru/one-shot-talking-face-20.04-t4 one-shot-talking-face
    %cd /content/one-shot-talking-face
    !pip install -r /content/one-shot-talking-face/requirements.txt
    !chmod 755 /content/one-shot-talking-face/OpenFace/FeatureExtraction
    !mkdir /content/out
    !apt install -qq libgtk2.0-0 jq -y
    !pip install -q imageio-ffmpeg
    ▼ - log - GoogleColab Tesla T4

  2. セルの実行終了② 後、左サイドバーの「ファイルボタン」を押す
    「one-shot-talking-face」③ の下に「sample」」④ フォルダがあることを確認する

動画の生成

  1. 動画の生成
    ・用意した画像を使用する場合は、「one-shot-talking-face/samples/imgs」フォルダへ、
     音声ファイルは「one-shot-talking-face/samples/audios」フォルダへアップロードする
    ・画像ファイル(image_file_name)/ 音声ファイル(wav_file_name)を指定する

    ・以下のセルを実行する(実行時間 2分)
    #@title #**make movie**
    import os, random, torchaudio
    from IPython.display import HTML
    from base64 import b64encode
    from IPython.display import clear_output
    
    def show_video(video_path, video_width = 256):
      video_file = open(video_path, "r+b").read()
      video_url = f"data:video/mp4;base64,{b64encode(video_file).decode()}"
      return HTML(f"""<video width={video_width} controls><source src="{video_url}"></video>""")
    
    #@markdown -Select wav_file_name from the one-shot-talking-face/samples/audios folder.\
    #@markdown -Select image_file_name from the one-shot-talking-face/samples/imgs folder.
    
    wav_file_name = 'obama2.wav' #@param {type:"string"}
    image_file_name = 'd5.jpg' #@param {type:"string"}
    
    wav_file = "/content/one-shot-talking-face/samples/audios/" + wav_file_name
    image_file = "/content/one-shot-talking-face/samples/imgs/" + image_file_name
    
    waveform, sample_rate = torchaudio.load(wav_file)
    torchaudio.save(wav_file, waveform, sample_rate, encoding="PCM_S", bits_per_sample=16)
    
    os.environ['wav_file'] = wav_file
    os.environ['image_file'] = image_file
    random_int = str(random.randint(1, 1000000))
    
    !mkdir /content/train
    !cp $wav_file /content/train/audio.wav
    !cp $image_file /content/train/image.png
    
    !pocketsphinx -phone_align yes single /content/train/audio.wav $text | jq '[.w[]|{word: (.t | ascii_upcase | sub("<S>"; "sil") | sub("<SIL>"; "sil") | sub("\\(2\\)"; "") | sub("\\(3\\)"; "") | sub("\\(4\\)"; "") | sub("\\[SPEECH\\]"; "SIL") | sub("\\[NOISE\\]"; "SIL")), phones: [.w[]|{ph: .t | sub("\\+SPN\\+"; "SIL") | sub("\\+NSN\\+"; "SIL"), bg: (.b*100)|floor, ed: (.b*100+.d*100)|floor}]}]' > /content/test.json
    %cd /content/one-shot-talking-face
    !python -B test_script.py --img_path /content/train/image.png --audio_path /content/train/audio.wav --phoneme_path /content/test.json --save_dir /content/train
    
    os.environ['ran_num'] = random_int
    !cp /content/train/image_audio.mp4 "/content/out/${ran_num}.mp4"
    
    clear_output()
    
    show_video(f"/content/out/{random_int}.mp4")

  2. 動画のダウンロード
    ・以下のセルを実行する(実行時間 数秒)
    #@title #**Download movie** ( for google chrome)
    from google.colab import files
    file_path =f"/content/out/{random_int}.mp4"
    files.download(file_path)

別の動画の生成

編集の終了・再接続後の実行

  1. 編集を終えるときは Colab「ランタイム」→「ランタイムを接続解除して削除」を選択する
    ・GPU 占有時間を少なくするためすべての実行作業が終了した場合は接続解除しておくことが望ましい
    ・接続解除して削除を実行しても、ノートブック上の実行結果はそのまま残る

  2. 再接続の場合は上記の 環境設定 からもう一度実行同じ手順をする

更新履歴

参考資料

 

Last-modified: 2023-12-09 (土) 04:55:05