ジャコ Lab

プログラミング関連のメモ帳的ブログです

Stability AI 及び Tripo AI からリリースされた TripoSR を Google Colab で使ってみる

ja.stability.ai

数日前に Stability AI 及び Tripo AI からリリースされた TripoSR というものがあります。 Image-to-3dModel にあたるようです。

普段は、diffusers 経由で Stable Diffusion を触っていて、まだ触り始めて間もないので過去のリリースノートから追って行っている感じです。ですが、個人的に昨年は Unity とか触ってたので、3D モデル生成の TripoSR は思わず飛び付き、急に最新の情報に触れることになりました。3Dモデル生成はすごいワクワクします。

早速、使わせていただきましょう!!Google Colab で!!

TripoSR とは?

Stability AITripo AI が共同開発した Text-to-3dModel のようです。

ソースコードは MIT ライセンスで GitHub 上に公開されており、Python で実行できるようになっていました。

準備

リポジトリをクローンする

!git clone https://github.com/VAST-AI-Research/TripoSR.git

ワーキングディレクトリを変更する

%cd TripoSR

必要モジュールをインストール

!pip install -r requirements.txt

実行してみる

!python run.py examples/chair.png --output-dir output/
とりあえず何も考えずに README 通りに叩く!

output/ ディレクトリ以下に input.pngmesh.obj が出力されました。

mesh.obj を Blender で開いてみる

(左) input.png | (右) mesh.obj

ちゃんと椅子の 3D モデルができました! ちなみにマテリアルは持っていないですが、頂点カラーを持っています。

背もたれ無くなってるけど!

ハンバーガーも作ってみる

!python run.py examples/hamburger.png --output-dir output2

(左) input.png | (右) mesh.obj

ハンバーガー!!

run.py に渡している画像が input.png ではない

(左) example/hamburger.png | (右) 出力された input.png

どうやら少し加工をしているようです。

        image = remove_background(Image.open(image_path), rembg_session)
        image = resize_foreground(image, args.foreground_ratio)
        image = np.array(image).astype(np.float32) / 255.0
        image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
        image = Image.fromarray((image * 255.0).astype(np.uint8))
        if not os.path.exists(os.path.join(output_dir, str(i))):
            os.makedirs(os.path.join(output_dir, str(i)))
        image.save(os.path.join(output_dir, str(i), f"input.png"))

この辺のコードでしょうか。 remove_background をしたり resize_foreground をしたりしているようです。

だからお皿が無くなっちゃったのか!

run.py のヘルプを見る

!python run.py --help
--
usage: run.py [-h] [--device DEVICE]
              [--pretrained-model-name-or-path PRETRAINED_MODEL_NAME_OR_PATH]
              [--chunk-size CHUNK_SIZE] [--no-remove-bg] [--foreground-ratio FOREGROUND_RATIO]
              [--output-dir OUTPUT_DIR] [--model-save-format {obj,glb}] [--render]
              image [image ...]

positional arguments:
  image                 Path to input image(s).

options:
  -h, --help            show this help message and exit
  --device DEVICE       Device to use. If no CUDA-compatible device is found, will fallback to
                        'cpu'. Default: 'cuda:0'
  --pretrained-model-name-or-path PRETRAINED_MODEL_NAME_OR_PATH
                        Path to the pretrained model. Could be either a huggingface model id is or
                        a local path. Default: 'stabilityai/TripoSR'
  --chunk-size CHUNK_SIZE
                        Evaluation chunk size for surface extraction and rendering. Smaller chunk
                        size reduces VRAM usage but increases computation time. 0 for no chunking.
                        Default: 8192
  --no-remove-bg        If specified, the background will NOT be automatically removed from the
                        input image, and the input image should be an RGB image with gray
                        background and properly-sized foreground. Default: false
  --foreground-ratio FOREGROUND_RATIO
                        Ratio of the foreground size to the image size. Only used when --no-
                        remove-bg is not specified. Default: 0.85
  --output-dir OUTPUT_DIR
                        Output directory to save the results. Default: 'output/'
  --model-save-format {obj,glb}
                        Format to save the extracted mesh. Default: 'obj'
  --render              If specified, save a NeRF-rendered video. Default: false
--render 付けると動画が保存されそう?

--render オプションを付けてみる

!python run.py examples/hamburger.png --render --output-dir output2

アウトプットディレクトリ以下に render.mp4 が出力されましたね。

--render オプションで出力された動画

あ、README にあったクルクル回るやつだ!

おまけ

お寿司はできるかな?

写真AC からまぐろの握り寿司 を使わせていただきます。

(左) 投入画像 | (中) 出力された input.png | (右) mesh.obj を Blender で開いたやつ

お寿司はお刺身になりました 🙃

シャリと背景が同色だったのが悪かったですね...

まとめ

3D モデルがこんなに簡単にできるの凄いです。

すぐゲームに展開できそうです。