0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AI動画の生成条件をJSONに残し、プロンプトだけ変えたかPythonで確認する

0
Posted at

AI動画のプロンプトを少し書き換えて、前の動画と見比べる。ここで終了フレームも差し替えていたら、どちらの変更が映像に効いたのか切り分けにくい。画像を上書きしていると、ファイル名だけを見ても差し替えに気づけない。

たとえば、JXPの Kling 4.0 AI Video Generator のキーフレームモードで、机の上の紙飛行機が滑る短い動画を作るとする。始点と終点の画像をそろえたまま、動きの指示だけを書き換えたい。そこで、生成画面に入れた設定を毎回JSONに記録し、画像はファイルの中身まで比較する。

生成ごとに設定を保存する

Python 3.11以上を使う。run-a.json と run-b.json を作り、キーフレーム画像は各JSONからの相対パスで指定する。画像の順番は、生成画面に指定した順番にそろえる。

run-a.json の例

{
  "model": "Kling 4.0",
  "mode": "keyframes",
  "duration_seconds": 6,
  "aspect_ratio": "16:9",
  "resolution": "720p",
  "keyframes": ["frames/start.png", "frames/end.png"],
  "prompt": "固定カメラ。紙飛行機が机の上を右へ滑る。"
}

run-b.json では、たとえば prompt だけを「固定カメラ。紙飛行機が机の上を右へゆっくり滑り、止まる。」に変える。このJSONは比較用に手元で管理する記録だ。画像を修正するときは別ファイルに保存しておくと、前の実行で使った画像も比較に残せる。

画像のハッシュも含めて差分を出す

check_run.py として保存する。

import argparse
import hashlib
import json
from pathlib import Path


def load_run(path):
    data = json.loads(path.read_text(encoding="utf-8"))
    hashes = []
    for name in data["keyframes"]:
        with (path.parent / name).open("rb") as file:
            hashes.append(hashlib.file_digest(file, "sha256").hexdigest())
    return {**data, "keyframes": hashes}


parser = argparse.ArgumentParser()
parser.add_argument("before", type=Path)
parser.add_argument("after", type=Path)
args = parser.parse_args()

before = load_run(args.before)
after = load_run(args.after)
missing = object()

changed = [
    key for key in sorted(before.keys() | after.keys())
    if before.get(key, missing) != after.get(key, missing)
]

print("changed:", ", ".join(changed) or "(none)")
raise SystemExit(0 if changed == ["prompt"] else 1)
python check_run.py run-a.json run-b.json

changed: prompt と表示されれば、記録上の変更はプロンプトだけ。終了コードは 0 になる。画像の並びも変えた場合は changed: keyframes, prompt と表示され、終了コードは 1 になる。名前が違っても中身が同じ画像は同じ入力として扱う。逆に、同じ見た目でも再保存してファイルの中身が変われば、差分として扱う。

映像を見比べるとき

このスクリプトで確認できるのは、手元に記録した入力の差分まで。同じ条件で生成しても出力が揺れることはある。紙飛行機の例なら、まず「止まる」という指示を足した版を見て、停止する位置やタイミングを確認する。その違いをプロンプトの効果として判断したい場合は、各条件で何本か生成して、同じ傾向が出るかを見ておく。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?