Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,394 Bytes
7734c01 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copyright (c) Meta Platforms, Inc. and affiliates."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Imports and Model Loading"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import imageio\n",
"import uuid\n",
"from IPython.display import Image as ImageDisplay\n",
"from inference import Inference, ready_gaussian_for_video_rendering, render_video, load_image, load_single_mask, display_image, make_scene, interactive_visualizer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"PATH = os.getcwd()\n",
"TAG = \"hf\"\n",
"config_path = f\"{PATH}/../checkpoints/{TAG}/pipeline.yaml\"\n",
"inference = Inference(config_path, compile=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Load input image to lift to 3D (single object)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"IMAGE_PATH = f\"{PATH}/images/shutterstock_stylish_kidsroom_1640806567/image.png\"\n",
"IMAGE_NAME = os.path.basename(os.path.dirname(IMAGE_PATH))\n",
"\n",
"image = load_image(IMAGE_PATH)\n",
"mask = load_single_mask(os.path.dirname(IMAGE_PATH), index=14)\n",
"display_image(image, masks=[mask])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Generate Gaussian Splat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run model\n",
"output = inference(image, mask, seed=42)\n",
"\n",
"# export gaussian splat (as point cloud)\n",
"output[\"gs\"].save_ply(f\"{PATH}/gaussians/single/{IMAGE_NAME}.ply\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Visualize Gaussian Splat\n",
"### a. Animated Gif"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# render gaussian splat\n",
"scene_gs = make_scene(output)\n",
"scene_gs = ready_gaussian_for_video_rendering(scene_gs)\n",
"\n",
"video = render_video(\n",
" scene_gs,\n",
" r=1,\n",
" fov=60,\n",
" pitch_deg=15,\n",
" yaw_start_deg=-45,\n",
" resolution=512,\n",
")[\"color\"]\n",
"\n",
"# save video as gif\n",
"imageio.mimsave(\n",
" os.path.join(f\"{PATH}/gaussians/single/{IMAGE_NAME}.gif\"),\n",
" video,\n",
" format=\"GIF\",\n",
" duration=1000 / 30, # default assuming 30fps from the input MP4\n",
" loop=0, # 0 means loop indefinitely\n",
")\n",
"\n",
"# notebook display\n",
"ImageDisplay(url=f\"gaussians/single/{IMAGE_NAME}.gif?cache_invalidator={uuid.uuid4()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### b. Interactive Visualizer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# might take a while to load (black screen)\n",
"interactive_visualizer(f\"{PATH}/gaussians/single/{IMAGE_NAME}.ply\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "sam3d_objects-3dfy",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|