Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
e03a824
1
Parent(s):
f41b4fa
add
Browse files- requirements.txt +4 -1
- util.py +76 -0
requirements.txt
CHANGED
|
@@ -16,4 +16,7 @@ PyMCubes
|
|
| 16 |
git+https://github.com/openai/shap-e.git
|
| 17 |
ipywidgets
|
| 18 |
opencv-python
|
| 19 |
-
git+https://github.com/NVlabs/nvdiffrast.git
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
git+https://github.com/openai/shap-e.git
|
| 17 |
ipywidgets
|
| 18 |
opencv-python
|
| 19 |
+
git+https://github.com/NVlabs/nvdiffrast.git
|
| 20 |
+
rembg
|
| 21 |
+
onnxruntime
|
| 22 |
+
kiui
|
util.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
from shap_e.models.nn.camera import DifferentiableCameraBatch, DifferentiableProjectiveCamera
|
| 4 |
+
from shap_e.util.collections import AttrDict
|
| 5 |
+
|
| 6 |
+
def create_custom_cameras(size: int, device: torch.device, azimuths=None, elevations=None, fov_degrees=30, distance=3.0) -> DifferentiableCameraBatch:
|
| 7 |
+
"""
|
| 8 |
+
Create custom camera angles for rendering.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
size: The width and height of the rendered image.
|
| 12 |
+
device: The device to put the camera parameters on.
|
| 13 |
+
azimuths: List of azimuth angles in degrees.
|
| 14 |
+
elevations: List of elevation angles in degrees.
|
| 15 |
+
fov_degrees: Field of view in degrees.
|
| 16 |
+
distance: Distance from the origin.
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
A DifferentiableCameraBatch containing the specified cameras.
|
| 20 |
+
"""
|
| 21 |
+
if azimuths is None:
|
| 22 |
+
azimuths = [0]
|
| 23 |
+
if elevations is None:
|
| 24 |
+
elevations = [0]
|
| 25 |
+
|
| 26 |
+
origins = []
|
| 27 |
+
xs = []
|
| 28 |
+
ys = []
|
| 29 |
+
zs = []
|
| 30 |
+
|
| 31 |
+
for azimuth, elevation in zip(azimuths, elevations):
|
| 32 |
+
# Convert to radians
|
| 33 |
+
azimuth_rad = np.deg2rad(azimuth)
|
| 34 |
+
elevation_rad = np.deg2rad(elevation)
|
| 35 |
+
|
| 36 |
+
# Calculate camera position
|
| 37 |
+
x_pos = distance * np.cos(elevation_rad) * np.sin(azimuth_rad)
|
| 38 |
+
y_pos = distance * np.cos(elevation_rad) * np.cos(azimuth_rad)
|
| 39 |
+
z_pos = distance * np.sin(elevation_rad)
|
| 40 |
+
|
| 41 |
+
# Camera origin (position)
|
| 42 |
+
origin = np.array([x_pos, y_pos, z_pos])
|
| 43 |
+
|
| 44 |
+
# Camera z-axis (looking at the origin)
|
| 45 |
+
z = -origin / np.linalg.norm(origin)
|
| 46 |
+
|
| 47 |
+
# Camera x-axis (right)
|
| 48 |
+
x = np.array([np.cos(azimuth_rad + np.pi/2), np.sin(azimuth_rad + np.pi/2), 0.0])
|
| 49 |
+
x = x - np.dot(x, z) * z # Make orthogonal to z
|
| 50 |
+
x = x / np.linalg.norm(x) # Normalize
|
| 51 |
+
|
| 52 |
+
# Camera y-axis (up, computed as z cross x)
|
| 53 |
+
y = np.cross(z, x)
|
| 54 |
+
y = y / np.linalg.norm(y) # Normalize
|
| 55 |
+
|
| 56 |
+
origins.append(origin)
|
| 57 |
+
xs.append(x)
|
| 58 |
+
ys.append(y)
|
| 59 |
+
zs.append(z)
|
| 60 |
+
|
| 61 |
+
# Convert from radians to the appropriate x and y fov
|
| 62 |
+
fov_rad = np.deg2rad(fov_degrees)
|
| 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_rad,
|
| 74 |
+
y_fov=fov_rad,
|
| 75 |
+
),
|
| 76 |
+
)
|