私的AI研究会 > GanFOMM

静止画から作るフェイク動画:First Order Motion Model

 「First Order Motion Model」技術を使って静止画を動画にする。

※ 最終更新:2024/01/21 

サイト『First Order Motion Model で、モナリザをトランプ大統領のように動かす』 の検証

概要

Google Colaboratory に実行環境を作成

  1. オフィシャルサイトの Colab Demo を開く

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

  3. 『first-order-model-demo のコピー』のタイトルで開いた Google Colab のページで以降の操作を行う

  4. データファイルをダウンロードして解凍した「/work」を「/anaconda_win/work」に上書き配置(「update/work/FOMM/」を使用する)
     update_20231117.zip (18.3MB) <アップデート・データ>

  5. データファイルをダウンロードして解凍した「/work」を「/anaconda_win/work」に上書き配置
     update_20240117.zip (9.93MB) <アップデート・データ>

デモの起動

  1. 以下のセルを実行する ①(実行時間 12秒)
    %%capture
    %pip install ffmpeg-python imageio-ffmpeg
    !git init .
    !git remote add origin https://github.com/AliaksandrSiarohin/first-order-model
    !git pull origin master
    !git clone https://github.com/graphemecluster/first-order-model-demo demo
    ・「Google Colab」にプロジェクトを配置する

  2. 以下のセルを実行する ②(実行時間 17秒)
    import IPython.display
    import PIL.Image
    import cv2
    import ffmpeg
    import imageio
        :                   途中省略 詳しくは下記 ▼ を押す
        :
    loading.layout.display = 'none'
    complete.layout.display = 'none'
    select_image('00')
    select_video('0')
    ▼ セルのコード全体

    ・サンプルデータ・学習済みモデルなどをダウンロードし、セルの実行が終了するとセルの下に次の GUI の実行パネルが表示される

デモの動かし方

  1. 入力画像(Choose Image) を指定する
    ・あらかじめ用意されている画像は「People/Cartoons/Dolls/Games of Thrones/Status」のタブで選択可能
    ・任意の画像を使いたい場合は「Upload」ボタンを押して、ローカル PC から画像ファイルを指定する

    ・用意されているサンプル画像(「demo/images/」フォルダ内)
  2. 入力動画(Choose Video) を指定する
    ・あらかじめ用意されている動画から選択できる
    ・任意の画像を使いたい場合は「Upload」ボタンを押して、ローカル PC から画像ファイルを指定する

    ・用意されているサンプル動画(「demo/images/」フォルダ内)
  3. 実行(Generate) を押す
    ・しばらくすると生成した動画(左) と元の動画(右) が表示される
    ・「Download」ボタンを押すと生成された 256x256 サイズの生成動画をダウンロードする
    ・「Convert to 1920x1080」ボタンを押すと生成された 1920x1080 サイズに変換した生成動画をダウンロードする
    ・「Back」ボタンを押すと 1.入力画像の指定画面に戻る

生成された動画を編集

  1. Googleドライブ に接続する
    ・最後のセルに以下のセルを追加実行する
    from google.colab import drive
    drive.mount('/content/drive')
    ・Googleアカウント認証ダイアログが出る場合はログインする

    ・しばらくして Googleドライブに接続すると左側のファイルビューアに「drive」フォルダができる

    ・「drive」フォルダ内の「data」フォルダ(なければ作成する) でファイルのやり取りをすることにする

  2. 動画ファイルをリサイズする(入力動画を 256x256 サイズにする)
    ・以下のセルを実行する
    !ffmpeg.exe -i drive/MyDrive/data/h1_video.jpg -s 256:256 -q 2 h1_video_256.jpg

  3. 静止画、動画フレーム、生成動画フレームを表示する
    ・以下のセルを実行する (あらかじめ drive/MyDrive/data/フォルダ内に動画ファイルをアップロードおく)
    # 静止画 / 動画 / 生成動画 の表示
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from skimage.transform import resize
    from IPython.display import HTML
    import warnings
    warnings.filterwarnings("ignore")
     
    def display(source, driving, generated=None):
        fig = plt.figure(figsize=(8 + 4 * (generated is not None), 4), dpi=64)
        fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
    
        ims = []
        for i in range(len(driving)):
            cols = [source]
            cols.append(driving[i])
            if generated is not None:
                cols.append(generated[i])
            im = plt.imshow(np.concatenate(cols, axis=1), animated=True)
            plt.axis('off')
            ims.append([im])
     
        ani = animation.ArtistAnimation(fig, ims, interval=33, repeat_delay=1000)
        plt.close()
        return ani
    
    source_image = imageio.imread('demo/images/41.png')
    driving_video = imageio.mimread('demo/videos/1.mp4')
    generated_video = imageio.mimread('drive/MyDrive/data/result/41_1_output.mp4')
    
    # 256x256 サイズに統一
    source_image = resize(source_image, (256, 256))[..., :3]
    driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video]
    generated_video = [resize(frame, (256, 256))[..., :3] for frame in generated_video]
    ani = display(source_image, driving_video, generated_video)
    ani.save('output.gif')
    HTML(ani.to_html5_video())
    ・上記の場合、出力画像は「output.gif」で保存(必要に応じてダウンロードする)

  4. 生成動画(256x256) 6枚を 1つの動画にする
    ・以下のセルを実行する (あらかじめ drive/MyDrive/data/フォルダ内に動画ファイルをアップロードおく)
    ## 6枚の静止画の生成動画フレームを連結
    import imageio
    import numpy as np
    import matplotlib.animation as animation
    import matplotlib.pyplot as plt
    from IPython.display import HTML
    from tqdm import trange
    
    result = []
    result.append(imageio.mimread('/content/drive/MyDrive/data/result/2.mp4'))
    result.append(imageio.mimread('/content/drive/MyDrive/data/result/mon_output.mp4'))
    result.append(imageio.mimread('/content/drive/MyDrive/data/result/statue-02_output.mp4'))
    result.append(imageio.mimread('/content/drive/MyDrive/data/result/okegawa_m1_output.mp4'))
    result.append(imageio.mimread('/content/drive/MyDrive/data/result/nitta_m1_output.mp4'))
    result.append(imageio.mimread('/content/drive/MyDrive/data/result/yaoi_m1_output.mp4'))
    
    fig = plt.figure(figsize=(12, 8), dpi=64)
    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
    ims =[]
    for i in trange(len(result[0])):
        x = np.concatenate([result[0][i], result[1][i], result[2][i]],1)
        y = np.concatenate([result[3][i], result[4][i], result[5][i]],1)
        z = np.concatenate([x, y])
        im = plt.imshow(z, animated=True)
        plt.axis('off')
        ims.append([im])
    
    print('making animeation...')
    ani = animation.ArtistAnimation(fig, ims, interval=33, repeat_delay=1000)
    plt.close()
    ani.save('outvideo6.mp4')
    HTML(ani.to_html5_video())

生成される画像例

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

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

  2. 再接続の場合は上記の デモの起動 からもう一度実行同じ手順をする

動画編集 Tips

動画ファイルの一部を切り取る

動画に音を追加する

動画のサイズを変更する

YouTube 動画をダウンロードする

更新履歴

参考資料

 

Last-modified: 2024-01-21 (日) 04:12:50