Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
eec0975
1
Parent(s):
9855a0b
add
Browse files
app.py
CHANGED
|
@@ -12,8 +12,9 @@ from einops import rearrange
|
|
| 12 |
from shap_e.diffusion.sample import sample_latents
|
| 13 |
from shap_e.diffusion.gaussian_diffusion import diffusion_from_config
|
| 14 |
from shap_e.models.download import load_model, load_config
|
| 15 |
-
from shap_e.util.notebooks import create_pan_cameras, decode_latent_images
|
| 16 |
import spaces
|
|
|
|
| 17 |
|
| 18 |
from src.utils.train_util import instantiate_from_config
|
| 19 |
from src.utils.camera_util import (
|
|
@@ -25,6 +26,56 @@ from src.utils.camera_util import (
|
|
| 25 |
from src.utils.mesh_util import save_obj, save_glb
|
| 26 |
from src.utils.infer_util import remove_background, resize_foreground
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@spaces.GPU(duration=60)
|
| 29 |
def load_models():
|
| 30 |
"""Initialize and load all required models"""
|
|
|
|
| 12 |
from shap_e.diffusion.sample import sample_latents
|
| 13 |
from shap_e.diffusion.gaussian_diffusion import diffusion_from_config
|
| 14 |
from shap_e.models.download import load_model, load_config
|
| 15 |
+
from shap_e.util.notebooks import create_pan_cameras, decode_latent_images
|
| 16 |
import spaces
|
| 17 |
+
from shap_e.models.nn.camera import DifferentiableCameraBatch, DifferentiableProjectiveCamera
|
| 18 |
|
| 19 |
from src.utils.train_util import instantiate_from_config
|
| 20 |
from src.utils.camera_util import (
|
|
|
|
| 26 |
from src.utils.mesh_util import save_obj, save_glb
|
| 27 |
from src.utils.infer_util import remove_background, resize_foreground
|
| 28 |
|
| 29 |
+
def create_custom_cameras(size: int, device: torch.device, azimuths: list, elevations: list,
|
| 30 |
+
fov_degrees: float,distance) -> DifferentiableCameraBatch:
|
| 31 |
+
# Object is in a 2x2x2 bounding box (-1 to 1 in each dimension)
|
| 32 |
+
object_diagonal = distance # Correct diagonal calculation for the cube
|
| 33 |
+
|
| 34 |
+
# Calculate radius based on object size and FOV
|
| 35 |
+
fov_radians = math.radians(fov_degrees)
|
| 36 |
+
radius = (object_diagonal / 2) / math.tan(fov_radians / 2) # Correct radius calculation
|
| 37 |
+
# print(radius)
|
| 38 |
+
# exit(0)
|
| 39 |
+
origins = []
|
| 40 |
+
xs = []
|
| 41 |
+
ys = []
|
| 42 |
+
zs = []
|
| 43 |
+
|
| 44 |
+
for azimuth, elevation in zip(azimuths, elevations):
|
| 45 |
+
azimuth_rad = np.radians(azimuth-90)
|
| 46 |
+
elevation_rad = np.radians(elevation)
|
| 47 |
+
|
| 48 |
+
# Calculate camera position
|
| 49 |
+
x = radius * np.cos(elevation_rad) * np.cos(azimuth_rad)
|
| 50 |
+
y = radius * np.cos(elevation_rad) * np.sin(azimuth_rad)
|
| 51 |
+
z = radius * np.sin(elevation_rad)
|
| 52 |
+
origin = np.array([x, y, z])
|
| 53 |
+
|
| 54 |
+
# Calculate camera orientation
|
| 55 |
+
z_axis = -origin / np.linalg.norm(origin) # Point towards center
|
| 56 |
+
x_axis = np.array([-np.sin(azimuth_rad), np.cos(azimuth_rad), 0])
|
| 57 |
+
y_axis = np.cross(z_axis, x_axis)
|
| 58 |
+
|
| 59 |
+
origins.append(origin)
|
| 60 |
+
zs.append(z_axis)
|
| 61 |
+
xs.append(x_axis)
|
| 62 |
+
ys.append(y_axis)
|
| 63 |
+
|
| 64 |
+
return DifferentiableCameraBatch(
|
| 65 |
+
shape=(1, len(origins)),
|
| 66 |
+
flat_camera=DifferentiableProjectiveCamera(
|
| 67 |
+
origin=torch.from_numpy(np.stack(origins, axis=0)).float().to(device),
|
| 68 |
+
x=torch.from_numpy(np.stack(xs, axis=0)).float().to(device),
|
| 69 |
+
y=torch.from_numpy(np.stack(ys, axis=0)).float().to(device),
|
| 70 |
+
z=torch.from_numpy(np.stack(zs, axis=0)).float().to(device),
|
| 71 |
+
width=size,
|
| 72 |
+
height=size,
|
| 73 |
+
x_fov=fov_radians,
|
| 74 |
+
y_fov=fov_radians,
|
| 75 |
+
),
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
@spaces.GPU(duration=60)
|
| 80 |
def load_models():
|
| 81 |
"""Initialize and load all required models"""
|