14
13

AIを使って2Dアニメーション作ってみた(やったことまとめ)

Last updated at Posted at 2022-12-10

4koma_named.jpg

で、出来た動画がこちらです。是非音楽付きで見てやってください。
(今見返すといろいろ粗くて作り直したいところもありますが、)途中でミクさんがキラキラして謎のエフェクトかかってるところが自分でも超お気に入りです。

※ 気に入って頂けましたらいいねなどしてやってもらえると嬉しいです!

はじめに

この記事は、stable diffusionのimg2imgを使った2Dアニメーションの作りかた、自分がやったことのまとめ記事です。

対象読者

  • stable diffusionで出来ることをおおかまに知っていて、webuiを動かしたことがある
  • img2imgでAIアニメーションを作成する方法に興味がある (例えばスキルとして習得して自分の3Dモデルに付加価値をつけて人気vtuberへの道を歩み始めたい?)
  • これからimg2imgでAIアニメーション作成に取り組もうとしていて、具体的なパラメータ設定の事例を参考として知りたい

この手順で出来ること、出来ないこと

  • プロンプトから自動で動画を作成する方法の説明ではなく、まず、元になる動画が必要となります。
  • 元動画から大幅なクオリティアップは出来ません。元動画の人物のモーションやカメラワークに十分魅力がある必要があります。
  • 元の人物と全く別の人物の動画にする方法も、ここでは解説しません。

手順概要

1行で説明するなら、元動画からフレーム画像を抽出し、webuiのimg2imgのbatch処理を使って、パラメータ設定はstrength0.3~0.5、promptはtxt2imgで近い構図が出るようなprompt(例えばclip decorder等使う)にしてseed固定でAI画像を出力し、出来た画像を連結して動画にする、という手順になります。

  • 元動画の準備
  • 動画からのフレーム抽出
  • stable diffusionのimg2img変換のバッチ処理
  • 変換後の画像を動画に結合

という手順です。

手順詳細

以下、最初に紹介した動画事例の作成手順です。

元動画の準備

特に動きのない動画はAIで変換かけても映えません。
全く違う人物にするように変換する場合は、変換のstrengthをあげて強めの変換をかけないといけないのですが、そうすると各々の画像が元の構図から乖離してしまって動画としてばらつきがひどくなってしまいます。
制御が難しそうなので、私は一旦このアプローチはとらず、promptに指定する人物と元の動画を同じ人物としました。
また、元動画として使える肖像権、権利関係の問題がクリアなものである必要があります。版権まわりは、コミケ等のおかげか二次創作に比較的寛容な文化はありますが、自己責任と作者へのリスペクトを持って行う必要があります。
初音ミクさんは、商用ではなく個人でお金をとらずに利用するのは問題ないので、今回は初音ミクさんをモデルにしています。
初音ミクさんと言えばMMDなので、人物モデル、モーション、カメラワークの配布フリーのものを使用して元動画とすることにしました。(人物モデル、モーション、カメラワークは著作権があるので、基本的には各々Readmeで使用する場合の権利表示の注意書きがありますので必ず従います)

Tipsとして、遠景や、身長の高いキャラクターで顔が小さくなってしまった場合に、解像度を1024x1024、またはそれ以上の解像度をあげて変換しないと、顔や特に目が潰れてしまうという問題がありました。
顔と目が潰れてしまっている例
000617.jpg

この対策として、モデルとカメラワークに以下の考慮を行いました。

  • デフォルメ/ちびキャラのモデルを使う
  • カメラワークは基本、遠景は使わずアップを多用する(このため自由に改変してよいとお断りのあるカメラワークを選定する必要があります)

モーションは、自分でモーショントレースする、カメラや機器を付けたりして自分の動きをモーションキャプチャする、動画からモーションを抽出するAIを使う等、様々な方法がありそうです。
また、プロンプトから音楽を作成するAI、音楽からモーションを作成するAIが登場しているので、プロンプトから動画を作成する方法もありそうです。
(この組み合わせでプロンプトから2Dアニメ作れないかな)
GitHub - MubertAI/Mubert-Text-to-Music: A simple notebook demonstrating prompt-based music generation via Mubert API
EDGE: Editable Dance Generation from Music

モデルは、VRMの3DモデルをBlenderで作成したり動かしたり出来るみたいなので、こちらもいつかやってみたいです。

動画からのフレーム抽出

動画が出来たら、動画の画面サイズと、抽出するフレームレートを決める必要があります。
画面サイズは768x768で出力しています。
今回の70秒の動画では、抽出フレーム枚数は842枚です。
フレームレートは低レートだとカクカクしますが、高レートだとフレーム枚数が多くなって変換時間がかかります。

stable diffusionのimg2img変換

img2imgでAIアニメーションを作成する一番のネックはwebuiを動かすGPUマシンの調達となります。Colabで842枚の動画変換はかなりツライです。
私はたまたまSagemaker Studio Labにつながったので作業をある程度この環境で進められました(ただし、基本的にはSagemaker Studio Labはつながらないです。その後、何回も試しているのですが一回もつながってないです)

このフェーズでは以下をまず決めます。

  • 使用するDiffusionモデル
    • DreamBooth学習を行うかどうか (学習させるデータセット)
  • 使用するvaeモデル
  • prompt
    • 全身
    • バフ
    • 髪型
    • 表情
    • style
  • strength
  • scale
  • sampler
  • step
  • seed

promptは、何枚か画像を事前にCLIP decorderや、DeepBooruでpromptを抽出させて調べる方法がimg2imgの場合有効です。全体的に構図が変わらない場合それで良いと思いますが、今回カメラワークが結構激しいので、参考程度に見て基本一から書いてます。
promptは今回全体通して固定でやってます。
promptが決まったらseed探しをします。出力画像サイズは同じ768x768(ここがミソ)、step数はDDIMの場合でseed探しする場合は多くなくてよく10ぐらいで、txt2imgで良さげな感じのミクさんが出るseed候補を何個か出しておきます。実際の変換自体はこのseedを固定にしてやります。

今回の動画の例

  • 使用するDiffusionモデル: 使用したモデルは最近のトレンドにあわせて念のため非公開とします
    • DreamBooth学習を行うかどうか (学習させるデータセット): 今回はDreamBooth学習は行っていません
  • 使用するvaeモデル: sd1.5付属のvae-ft-mse-840000-ema-pruned.ckpt
  • prompt: promptは全フレーム通して固定
    • 全身: hatsune miku with long aqua hair and black boots standing in front of a screen with a white background, solo, 1girl, loli, kawaii moe bishoujo,
    • バフ: masterpiece, best quality, extremely detailed, by Toei Animations,
    • 髪型: long tail,
    • 目: aqua eyes, ((((big eyes)))), ((((tareme)))), ((kyoto animation eyes)),
    • 口: headphones, headset,
    • 頬: ((blush)), perfect symmetrical pretty face with blush cheeks,
    • 表情: light smile, cute anime face, ^_^ ♡ 😘 😍,
    • 胸: (small breasts),
    • 服: aqua nails, short sleeve, detached sleeves, pleated skirt, black skirt,
    • style: ((thick outline)), ((black outline))
  • strength: 0.42(アップ時) / 0.3(遠景時)
  • scale: 11
  • sampler: DDIM
  • step: 70
  • seed: 固定値

最初に画像サイズ768x768で全フレーム変換バッチを実行しました。バッチ処理時間は4時間ぐらいだったと思います。
結果を確認したところ、遠景で出てきた結果が全て使い物にならなかったので、遠景フレームを全て1024x1024の出力解像度で出力し、その後解像度を768x868にリサイズしました。
出力結果におかしなフレーム(手がおかしい、頭の後に顔が出てきている!?)もそれなりに出ていたので、それらのフレームは別seedで生成しなおしたり細かな再生成を少しだけしています。

negative promptは脳死でこれを突っ込んでます。

(((deformed))), blurry, bad anatomy, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), fused fingers, messy drawing, broken legs censor, censored, censor_bar, multiple breasts, (mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), black-white, bad anatomy, liquid body, liquidtongue, disfigured, malformed, mutated, anatomical nonsense, text font ui, error, malformed hands, long neck, blurred, lowers, low res, bad anatomy, bad proportions, bad shadow, uncoordinated body, unnatural body, fused breasts, bad breasts, huge breasts, poorly drawn breasts, extra breasts, liquid breasts, heavy breasts, missingbreasts, huge haunch, huge thighs, huge calf, bad hands, fused hand, missing hand, disappearing arms, disappearing thigh, disappearing calf, disappearing legs, fusedears, bad ears, poorly drawn ears, extra ears, liquid ears, heavy ears, missing ears, fused animal ears, bad animal ears, poorly drawn animal ears, extra animal ears, liquidanimal ears, heavy animal ears, missing animal ears, text, ui, error, missing fingers, missing limb, fused fingers, one hand with more than 5 fingers, one hand with less than5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, colorful tongue, blacktongue, cropped, watermark, username, blurry, JPEG artifacts, signature, 3D, 3D game, 3D game scene, 3D character, malformed feet, extra feet, bad feet, poorly drawnfeet, fused feet, missing feet, extra shoes, bad shoes, fused shoes, more than two shoes, poorly drawn shoes, bad gloves, poorly drawn gloves, fused gloves, bad cum, poorly drawn cum, fused cum, bad hairs, poorly drawn hairs, fused hairs, big muscles, ugly, bad face, fused face, poorly drawn face, cloned face, big face, long face, badeyes, fused eyes poorly drawn eyes, extra eyes, malformed limbs, more than 2 nipples, missing nipples, different nipples, fused nipples, bad nipples, poorly drawnnipples, black nipples, colorful nipples, gross proportions. short arm, (((missing arms))), missing thighs, missing calf, missing legs, mutation, duplicate, morbid, mutilated, poorly drawn hands, more than 1 left hand, more than 1 right hand, deformed, (blurry), disfigured, missing legs, extra arms, extra thighs, more than 2 thighs, extra calf,fused calf, extra legs, bad knee, extra knee, more than 2 legs, bad tails, bad mouth, fused mouth, poorly drawn mouth, bad tongue, tongue within mouth, too longtongue, black tongue, big mouth, cracked mouth, bad mouth, dirty face, dirty teeth, dirty pantie, fused pantie, poorly drawn pantie, fused cloth, poorly drawn cloth, badpantie, yellow teeth, thick lips, bad camel toe, colorful camel toe, bad asshole, poorly drawn asshole, fused asshole, missing asshole, bad anus, bad pussy, bad crotch, badcrotch seam, fused anus, fused pussy, fused anus, fused crotch, poorly drawn crotch, fused seam, poorly drawn anus, poorly drawn pussy, poorly drawn crotch, poorlydrawn crotch seam, bad thigh gap, missing thigh gap, fused thigh gap, liquid thigh gap, poorly drawn thigh gap, poorly drawn anus, bad collarbone, fused collarbone, missing collarbone, liquid collarbone, strong girl, obesity, worst quality, low quality, normal quality, liquid tentacles, bad tentacles, poorly drawn tentacles, split tentacles, fused tentacles, missing clit, bad clit, fused clit, colorful clit, black clit, liquid clit, QR code, bar code, censored, safety panties, safety knickers, beard, furry, pony, pubic hair, mosaic, futa, testis, (((deformed))), blurry, bad anatomy, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), fused fingers, messy drawing, broken legs censor, censored, censor_bar, multiple breasts, (mutated hands and fingers:1.5), (long body :1.3), (mutation, poorly drawn :1.2), black-white, bad anatomy, liquid body, liquidtongue, disfigured, malformed, mutated, anatomical nonsense, text font ui, error, malformed hands, long neck, blurred, lowers, low res, bad anatomy, bad proportions, bad shadow, uncoordinated body, unnatural body, fused breasts, bad breasts, huge breasts, poorly drawn breasts, extra breasts, liquid breasts, heavy breasts, missingbreasts, huge haunch, huge thighs, huge calf, bad hands, fused hand, missing hand, disappearing arms, disappearing thigh, disappearing calf, disappearing legs, fusedears, bad ears, poorly drawn ears, extra ears, liquid ears, heavy ears, missing ears, fused animal ears, bad animal ears, poorly drawn animal ears, extra animal ears, liquidanimal ears, heavy animal ears, missing animal ears, text, ui, error, missing fingers, missing limb, fused fingers, one hand with more than 5 fingers, one hand with less than5 fingers, one hand with more than 5 digit, one hand with less than 5 digit, extra digit, fewer digits, fused digit, missing digit, bad digit, liquid digit, colorful tongue, blacktongue, cropped, watermark, username, blurry, JPEG artifacts, signature, 3D, 3D game, 3D game scene, 3D character, malformed feet, extra feet, bad feet, poorly drawnfeet, fused feet, missing feet, extra shoes, bad shoes, fused shoes, more than two shoes, poorly drawn shoes, bad gloves, poorly drawn gloves, fused gloves, bad cum, poorly drawn cum, fused cum, bad hairs, poorly drawn hairs, fused hairs, big muscles, ugly, bad face, fused face, poorly drawn face, cloned face, big face, long face, badeyes, fused eyes poorly drawn eyes, extra eyes, malformed limbs, more than 2 nipples, missing nipples, different nipples, fused nipples, bad nipples, poorly drawnnipples, black nipples, colorful nipples, gross proportions. short arm, (((missing arms))), missing thighs, missing calf, missing legs, mutation, duplicate, morbid, mutilated, poorly drawn hands, more than 1 left hand, more than 1 right hand, deformed, (blurry), disfigured, missing legs, extra arms, extra thighs, more than 2 thighs, extra calf,fused calf, extra legs, bad knee, extra knee, more than 2 legs, bad tails, bad mouth, fused mouth, poorly drawn mouth, bad tongue, tongue within mouth, too longtongue, black tongue, big mouth, cracked mouth, bad mouth, dirty face, dirty teeth, dirty pantie, fused pantie, poorly drawn pantie, fused cloth, poorly drawn cloth, badpantie, yellow teeth, thick lips, bad camel toe, colorful camel toe, bad asshole, poorly drawn asshole, fused asshole, missing asshole, bad anus, bad pussy, bad crotch, badcrotch seam, fused anus, fused pussy, fused anus, fused crotch, poorly drawn crotch, fused seam, poorly drawn anus, poorly drawn pussy, poorly drawn crotch, poorlydrawn crotch seam, bad thigh gap, missing thigh gap, fused thigh gap, liquid thigh gap, poorly drawn thigh gap, poorly drawn anus, bad collarbone, fused collarbone, missing collarbone, liquid collarbone, strong girl, obesity, worst quality, low quality, normal quality, liquid tentacles, bad tentacles, poorly drawn tentacles, split tentacles, fused tentacles, missing clit, bad clit, fused clit, colorful clit, black clit, liquid clit, QR code, bar code, censored, safety panties, safety knickers, beard, furry, pony, pubic hair, mosaic, futa, testis

変換後の画像を動画に結合

単純に出力画像を結合するだけです。ffmpegコマンド一発です。

終わりに(所感)

動画を作った後に(GitHub - OedoSoldier/enhanced-img2img: Enhanced img2img extension for AUTOMATIC111's WebUI)で、個別の画像毎にpromptやstrengthを変えられるwebuiのextensionが新しく出てきました。遠景のstrengthは弱く、アップのstrengthは強く、というような使い方ができそうです。

冒頭の漫画ですが実はAI使って描いてます。なんだかimg2imgのやりかたを確認している間に、AI四コマ漫画絵師としてAI画力があがっていたようです!
この記事絵心ゼロのおっさんがAI(stable diffusion)使って気付けば四コマ漫画を書いてしまった件 - Qiitaを書いてから3カ月ですが、地味にAI画力成長しております。

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13