Datasets:
EntropyDrop commited on
Commit ·
60df411
1
Parent(s): 586a73f
update
Browse files- DDJ_real2render/create_grid_template.py +271 -0
- DDJ_real2render/create_template.py +72 -55
- DDJ_real2render/real2render.py +4 -4
- DDJ_real2render/template/template18.png +3 -0
- DDJ_real2render/template/template_inner.png +3 -0
- DDJ_real2render/template/template_outer.png +3 -0
- DDJ_real2render/test_output/img6_template18.png +3 -0
DDJ_real2render/create_grid_template.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import argparse
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image, ImageColor
|
| 8 |
+
|
| 9 |
+
try:
|
| 10 |
+
from .create_template import VIEW_NAMES, parse_bg_color, resolve_renderer_path
|
| 11 |
+
except ImportError:
|
| 12 |
+
from create_template import VIEW_NAMES, parse_bg_color, resolve_renderer_path
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
DEFAULT_SIZE = "344x768"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def parse_rgb_color(color):
|
| 19 |
+
if isinstance(color, str):
|
| 20 |
+
return ImageColor.getrgb(color)
|
| 21 |
+
if isinstance(color, (tuple, list)) and len(color) >= 3:
|
| 22 |
+
return tuple(int(channel) for channel in color[:3])
|
| 23 |
+
raise ValueError(f"Invalid RGB color: {color!r}")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def render_visible_grid(mapping_path, skin_alpha, fill_color, grid_color):
|
| 27 |
+
import torch
|
| 28 |
+
|
| 29 |
+
mapping = torch.load(mapping_path, map_location="cpu")
|
| 30 |
+
required_keys = ("geometry_uv_layers", "geometry_masks", "geometry_sort_indices")
|
| 31 |
+
missing_keys = [key for key in required_keys if key not in mapping]
|
| 32 |
+
if missing_keys:
|
| 33 |
+
raise KeyError(
|
| 34 |
+
f"Geometry visibility data missing from {mapping_path}: {', '.join(missing_keys)}"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
geometry_uv = mapping["geometry_uv_layers"].cpu().numpy()
|
| 38 |
+
geometry_masks = mapping["geometry_masks"].cpu().numpy().astype(bool)
|
| 39 |
+
sort_indices = mapping["geometry_sort_indices"].cpu().numpy().astype(np.intp)
|
| 40 |
+
if geometry_uv.shape[0] == 0:
|
| 41 |
+
raise ValueError(f"No geometry layers found in mapping: {mapping_path}")
|
| 42 |
+
|
| 43 |
+
layer_count, height, width = geometry_masks.shape
|
| 44 |
+
u = geometry_uv[..., 0].astype(np.intp)
|
| 45 |
+
v = geometry_uv[..., 1].astype(np.intp)
|
| 46 |
+
valid_uv = (u >= 0) & (u < 64) & (v >= 0) & (v < 64)
|
| 47 |
+
safe_u = np.clip(u, 0, 63)
|
| 48 |
+
safe_v = np.clip(v, 0, 63)
|
| 49 |
+
layer_visible = geometry_masks & valid_uv & (skin_alpha[safe_v, safe_u] > 0)
|
| 50 |
+
|
| 51 |
+
# The sort order is back-to-front, matching DifferentiableRenderer's
|
| 52 |
+
# compositing order. Pick only the nearest visible geometry at every pixel.
|
| 53 |
+
sorted_visible = np.take_along_axis(layer_visible, sort_indices, axis=0)
|
| 54 |
+
has_visible_surface = sorted_visible.any(axis=0)
|
| 55 |
+
nearest_from_front = np.argmax(sorted_visible[::-1], axis=0)
|
| 56 |
+
nearest_position = layer_count - 1 - nearest_from_front
|
| 57 |
+
nearest_geometry = np.take_along_axis(
|
| 58 |
+
sort_indices,
|
| 59 |
+
nearest_position[np.newaxis, ...],
|
| 60 |
+
axis=0,
|
| 61 |
+
)[0]
|
| 62 |
+
|
| 63 |
+
rows, columns = np.indices((height, width))
|
| 64 |
+
selected_u = u[nearest_geometry, rows, columns]
|
| 65 |
+
selected_v = v[nearest_geometry, rows, columns]
|
| 66 |
+
|
| 67 |
+
# A grid edge is a boundary between visible UV texels/geometries, or the
|
| 68 |
+
# silhouette between visible geometry and the transparent background.
|
| 69 |
+
grid_edges = np.zeros((height, width), dtype=bool)
|
| 70 |
+
|
| 71 |
+
left_visible = has_visible_surface[:, :-1]
|
| 72 |
+
right_visible = has_visible_surface[:, 1:]
|
| 73 |
+
horizontal_change = (left_visible != right_visible) | (
|
| 74 |
+
left_visible
|
| 75 |
+
& right_visible
|
| 76 |
+
& (
|
| 77 |
+
(nearest_geometry[:, :-1] != nearest_geometry[:, 1:])
|
| 78 |
+
| (selected_u[:, :-1] != selected_u[:, 1:])
|
| 79 |
+
| (selected_v[:, :-1] != selected_v[:, 1:])
|
| 80 |
+
)
|
| 81 |
+
)
|
| 82 |
+
grid_edges[:, :-1] |= horizontal_change & left_visible
|
| 83 |
+
grid_edges[:, 1:] |= horizontal_change & ~left_visible & right_visible
|
| 84 |
+
|
| 85 |
+
top_visible = has_visible_surface[:-1, :]
|
| 86 |
+
bottom_visible = has_visible_surface[1:, :]
|
| 87 |
+
vertical_change = (top_visible != bottom_visible) | (
|
| 88 |
+
top_visible
|
| 89 |
+
& bottom_visible
|
| 90 |
+
& (
|
| 91 |
+
(nearest_geometry[:-1, :] != nearest_geometry[1:, :])
|
| 92 |
+
| (selected_u[:-1, :] != selected_u[1:, :])
|
| 93 |
+
| (selected_v[:-1, :] != selected_v[1:, :])
|
| 94 |
+
)
|
| 95 |
+
)
|
| 96 |
+
grid_edges[:-1, :] |= vertical_change & top_visible
|
| 97 |
+
grid_edges[1:, :] |= vertical_change & ~top_visible & bottom_visible
|
| 98 |
+
|
| 99 |
+
grid_edges[0, :] |= has_visible_surface[0, :]
|
| 100 |
+
grid_edges[-1, :] |= has_visible_surface[-1, :]
|
| 101 |
+
grid_edges[:, 0] |= has_visible_surface[:, 0]
|
| 102 |
+
grid_edges[:, -1] |= has_visible_surface[:, -1]
|
| 103 |
+
|
| 104 |
+
output = np.zeros((height, width, 4), dtype=np.uint8)
|
| 105 |
+
output[has_visible_surface, :3] = fill_color
|
| 106 |
+
output[has_visible_surface, 3] = 255
|
| 107 |
+
output[grid_edges, :3] = grid_color
|
| 108 |
+
output[grid_edges, 3] = 255
|
| 109 |
+
return Image.fromarray(output, mode="RGBA")
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def create_grid_template(
|
| 113 |
+
skin_path,
|
| 114 |
+
output_path,
|
| 115 |
+
renderer_path,
|
| 116 |
+
size=DEFAULT_SIZE,
|
| 117 |
+
bg_color="black",
|
| 118 |
+
fill_color="white",
|
| 119 |
+
grid_color="black",
|
| 120 |
+
):
|
| 121 |
+
"""Render four visible-surface Minecraft texel grids into one horizontal image."""
|
| 122 |
+
if not os.path.exists(skin_path):
|
| 123 |
+
raise FileNotFoundError(f"Skin image '{skin_path}' does not exist.")
|
| 124 |
+
|
| 125 |
+
dmr_path = resolve_renderer_path(renderer_path)
|
| 126 |
+
if dmr_path not in sys.path:
|
| 127 |
+
sys.path.insert(0, dmr_path)
|
| 128 |
+
|
| 129 |
+
from config import parse_sizes
|
| 130 |
+
|
| 131 |
+
if isinstance(size, str):
|
| 132 |
+
target_size = parse_sizes(size)[0]
|
| 133 |
+
elif isinstance(size, (tuple, list)) and len(size) == 2:
|
| 134 |
+
target_size = (int(size[0]), int(size[1]))
|
| 135 |
+
else:
|
| 136 |
+
target_size = tuple(int(value) for value in DEFAULT_SIZE.split("x"))
|
| 137 |
+
|
| 138 |
+
skin_img = Image.open(skin_path).convert("RGBA")
|
| 139 |
+
if skin_img.size != (64, 64):
|
| 140 |
+
raise ValueError(
|
| 141 |
+
f"Minecraft grid rendering requires a 64x64 skin, "
|
| 142 |
+
f"got {skin_img.size[0]}x{skin_img.size[1]}."
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
skin_np = np.array(skin_img)
|
| 146 |
+
alpha = skin_np[..., 3]
|
| 147 |
+
semi_transparent = (alpha > 0) & (alpha < 255)
|
| 148 |
+
skin_np[semi_transparent, 3] = 255
|
| 149 |
+
|
| 150 |
+
fill_rgb = parse_rgb_color(fill_color)
|
| 151 |
+
grid_rgb = parse_rgb_color(grid_color)
|
| 152 |
+
mappings_dir = os.path.join(dmr_path, f"mappings_{target_size[0]}x{target_size[1]}")
|
| 153 |
+
if not os.path.isdir(mappings_dir):
|
| 154 |
+
raise FileNotFoundError(
|
| 155 |
+
f"Differentiable renderer mappings not found for size "
|
| 156 |
+
f"{target_size[0]}x{target_size[1]}: {mappings_dir}"
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
rendered_images = []
|
| 160 |
+
for view_name in VIEW_NAMES:
|
| 161 |
+
print(f"Rendering visible grid view: {view_name} (Size: {target_size})...")
|
| 162 |
+
mapping_path = os.path.join(mappings_dir, f"{view_name}_mapping.pt")
|
| 163 |
+
if not os.path.isfile(mapping_path):
|
| 164 |
+
raise FileNotFoundError(f"View mapping not found: {mapping_path}")
|
| 165 |
+
rendered_image = render_visible_grid(
|
| 166 |
+
mapping_path=mapping_path,
|
| 167 |
+
skin_alpha=skin_np[..., 3],
|
| 168 |
+
fill_color=fill_rgb,
|
| 169 |
+
grid_color=grid_rgb,
|
| 170 |
+
)
|
| 171 |
+
if rendered_image.size != target_size:
|
| 172 |
+
raise ValueError(
|
| 173 |
+
f"Mapping size for {view_name} is {rendered_image.size}, expected {target_size}."
|
| 174 |
+
)
|
| 175 |
+
rendered_images.append(rendered_image)
|
| 176 |
+
|
| 177 |
+
widths, heights = zip(*(image.size for image in rendered_images))
|
| 178 |
+
merged_img = Image.new("RGBA", (sum(widths), max(heights)))
|
| 179 |
+
x_offset = 0
|
| 180 |
+
for image in rendered_images:
|
| 181 |
+
merged_img.paste(image, (x_offset, 0))
|
| 182 |
+
x_offset += image.width
|
| 183 |
+
|
| 184 |
+
parsed_bg = parse_bg_color(bg_color)
|
| 185 |
+
if parsed_bg is not None:
|
| 186 |
+
background = Image.new("RGBA", merged_img.size, parsed_bg)
|
| 187 |
+
merged_img = Image.alpha_composite(background, merged_img)
|
| 188 |
+
|
| 189 |
+
output_dir = os.path.dirname(os.path.abspath(output_path))
|
| 190 |
+
if output_dir:
|
| 191 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 192 |
+
|
| 193 |
+
merged_img.save(output_path)
|
| 194 |
+
print(f"Successfully generated grid template: {output_path} (Size: {merged_img.size})")
|
| 195 |
+
return output_path
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def main():
|
| 199 |
+
parser = argparse.ArgumentParser(
|
| 200 |
+
description=(
|
| 201 |
+
"Create a horizontal visible-surface grid for the front-left, "
|
| 202 |
+
"front-right, back-left, and back-right Minecraft views."
|
| 203 |
+
)
|
| 204 |
+
)
|
| 205 |
+
parser.add_argument(
|
| 206 |
+
"--skin",
|
| 207 |
+
"--skin_path",
|
| 208 |
+
dest="skin_path",
|
| 209 |
+
required=True,
|
| 210 |
+
help="Path to the input 64x64 Minecraft skin.",
|
| 211 |
+
)
|
| 212 |
+
parser.add_argument(
|
| 213 |
+
"--output",
|
| 214 |
+
"--output_path",
|
| 215 |
+
dest="output_path",
|
| 216 |
+
required=True,
|
| 217 |
+
help="Path to save the merged grid image.",
|
| 218 |
+
)
|
| 219 |
+
parser.add_argument(
|
| 220 |
+
"--renderer_path",
|
| 221 |
+
"--renderer",
|
| 222 |
+
dest="renderer_path",
|
| 223 |
+
required=True,
|
| 224 |
+
help="Path to the differentiable_minecraft_renderer directory.",
|
| 225 |
+
)
|
| 226 |
+
parser.add_argument(
|
| 227 |
+
"--size",
|
| 228 |
+
"--resolution",
|
| 229 |
+
default=DEFAULT_SIZE,
|
| 230 |
+
help=(
|
| 231 |
+
f"Resolution per view (default: {DEFAULT_SIZE}; four horizontal "
|
| 232 |
+
"views produce 1376x768)."
|
| 233 |
+
),
|
| 234 |
+
)
|
| 235 |
+
parser.add_argument(
|
| 236 |
+
"--bg_color",
|
| 237 |
+
"--bg-color",
|
| 238 |
+
"--background",
|
| 239 |
+
dest="bg_color",
|
| 240 |
+
default="black",
|
| 241 |
+
help="Output background color (default: black); use 'transparent' for transparency.",
|
| 242 |
+
)
|
| 243 |
+
parser.add_argument(
|
| 244 |
+
"--fill_color",
|
| 245 |
+
"--fill-color",
|
| 246 |
+
dest="fill_color",
|
| 247 |
+
default="white",
|
| 248 |
+
help="Visible surface fill color behind the black grid (default: white).",
|
| 249 |
+
)
|
| 250 |
+
parser.add_argument(
|
| 251 |
+
"--grid_color",
|
| 252 |
+
"--grid-color",
|
| 253 |
+
dest="grid_color",
|
| 254 |
+
default="black",
|
| 255 |
+
help="Grid line color (default: black).",
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
args = parser.parse_args()
|
| 259 |
+
create_grid_template(
|
| 260 |
+
skin_path=args.skin_path,
|
| 261 |
+
output_path=args.output_path,
|
| 262 |
+
renderer_path=args.renderer_path,
|
| 263 |
+
size=args.size,
|
| 264 |
+
bg_color=args.bg_color,
|
| 265 |
+
fill_color=args.fill_color,
|
| 266 |
+
grid_color=args.grid_color,
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
if __name__ == "__main__":
|
| 271 |
+
main()
|
DDJ_real2render/create_template.py
CHANGED
|
@@ -2,10 +2,12 @@
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import argparse
|
| 5 |
-
from pathlib import Path
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
def resolve_renderer_path(renderer_path):
|
| 10 |
if not renderer_path or not os.path.exists(renderer_path):
|
| 11 |
raise FileNotFoundError(f"Renderer directory '{renderer_path}' does not exist.")
|
|
@@ -26,15 +28,15 @@ def parse_bg_color(bg_color):
|
|
| 26 |
return bg_color
|
| 27 |
return bg_color
|
| 28 |
|
| 29 |
-
def create_template(skin_path, output_path, renderer_path, size="
|
| 30 |
"""
|
| 31 |
-
Renders
|
| 32 |
-
using
|
| 33 |
|
| 34 |
:param skin_path: Path to the input skin PNG image.
|
| 35 |
:param output_path: Destination path for the merged output template image.
|
| 36 |
:param renderer_path: Path to differentiable_minecraft_renderer directory (required).
|
| 37 |
-
:param size: Resolution tuple (W, H) or string (e.g. "
|
| 38 |
:param bg_color: Background color for the output template image (default: "black"). Set to None or "transparent" for transparent background.
|
| 39 |
:return: Path to generated output file.
|
| 40 |
"""
|
|
@@ -45,12 +47,9 @@ def create_template(skin_path, output_path, renderer_path, size="512x1024", bg_c
|
|
| 45 |
if dmr_path not in sys.path:
|
| 46 |
sys.path.insert(0, dmr_path)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
from config import
|
| 50 |
-
|
| 51 |
-
from merge_views import merge
|
| 52 |
-
except ImportError:
|
| 53 |
-
merge = None
|
| 54 |
|
| 55 |
if isinstance(size, str):
|
| 56 |
parsed = parse_sizes(size)
|
|
@@ -58,60 +57,70 @@ def create_template(skin_path, output_path, renderer_path, size="512x1024", bg_c
|
|
| 58 |
elif isinstance(size, (tuple, list)) and len(size) == 2:
|
| 59 |
target_size = (int(size[0]), int(size[1]))
|
| 60 |
else:
|
| 61 |
-
target_size = (
|
| 62 |
|
| 63 |
print(f"Loading skin image from: {skin_path}")
|
| 64 |
skin_img = Image.open(skin_path).convert("RGBA")
|
| 65 |
skin_np = np.array(skin_img)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# Clean semi-transparency to match build_target_img preprocessing
|
| 68 |
alpha = skin_np[..., 3]
|
| 69 |
semi_transparent = (alpha > 0) & (alpha < 255)
|
| 70 |
skin_np[semi_transparent, 3] = 255
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
print(f"Rendering view with PyVista: {view_name} (Size: {params['output_size']})...")
|
| 86 |
-
pv_render = mc_render.render_skin(
|
| 87 |
-
skin=clean_skin_img,
|
| 88 |
-
output_size=params["output_size"],
|
| 89 |
-
cam_front=params["cam_front"],
|
| 90 |
-
zoom=params["zoom"],
|
| 91 |
-
look_at_y=params["look_at_y"],
|
| 92 |
-
use_voxels=False,
|
| 93 |
-
light=False,
|
| 94 |
-
transparent_background=True,
|
| 95 |
-
core_display=params["core_display"],
|
| 96 |
-
decor_display=params["decor_display"],
|
| 97 |
-
rot_args=rot_args,
|
| 98 |
-
offset_args=offset_args,
|
| 99 |
-
ortho=params.get("ortho", False),
|
| 100 |
-
off_screen=True
|
| 101 |
)
|
| 102 |
-
rendered_images[view_name] = Image.fromarray(pv_render)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
parsed_bg = parse_bg_color(bg_color)
|
| 117 |
if parsed_bg is not None:
|
|
@@ -128,12 +137,20 @@ def create_template(skin_path, output_path, renderer_path, size="512x1024", bg_c
|
|
| 128 |
|
| 129 |
def main():
|
| 130 |
parser = argparse.ArgumentParser(
|
| 131 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 132 |
)
|
| 133 |
parser.add_argument("--skin", "--skin_path", dest="skin_path", required=True, help="Path to input skin image file.")
|
| 134 |
parser.add_argument("--output", "--output_path", dest="output_path", required=True, help="Path to save the merged output template image.")
|
| 135 |
parser.add_argument("--renderer_path", "--renderer", dest="renderer_path", required=True, help="Path to differentiable_minecraft_renderer directory.")
|
| 136 |
-
parser.add_argument(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
parser.add_argument("--bg_color", "--bg-color", "--background", dest="bg_color", default="black", help="Background color for the output template image (default: black). Set to 'none' or 'transparent' for transparency.")
|
| 138 |
|
| 139 |
args = parser.parse_args()
|
|
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import argparse
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
VIEW_NAMES = ("front_left", "front_right", "back_left", "back_right")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
def resolve_renderer_path(renderer_path):
|
| 12 |
if not renderer_path or not os.path.exists(renderer_path):
|
| 13 |
raise FileNotFoundError(f"Renderer directory '{renderer_path}' does not exist.")
|
|
|
|
| 28 |
return bg_color
|
| 29 |
return bg_color
|
| 30 |
|
| 31 |
+
def create_template(skin_path, output_path, renderer_path, size="256x576", bg_color="black"):
|
| 32 |
"""
|
| 33 |
+
Renders front-left, front-right, back-left, and back-right views for a given
|
| 34 |
+
skin using the differentiable renderer, then merges them into one horizontal image.
|
| 35 |
|
| 36 |
:param skin_path: Path to the input skin PNG image.
|
| 37 |
:param output_path: Destination path for the merged output template image.
|
| 38 |
:param renderer_path: Path to differentiable_minecraft_renderer directory (required).
|
| 39 |
+
:param size: Resolution tuple (W, H) or string (e.g. "256x576"). Default is "256x576".
|
| 40 |
:param bg_color: Background color for the output template image (default: "black"). Set to None or "transparent" for transparent background.
|
| 41 |
:return: Path to generated output file.
|
| 42 |
"""
|
|
|
|
| 47 |
if dmr_path not in sys.path:
|
| 48 |
sys.path.insert(0, dmr_path)
|
| 49 |
|
| 50 |
+
import torch
|
| 51 |
+
from config import parse_sizes
|
| 52 |
+
from differentiable_renderer import DifferentiableRenderer
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if isinstance(size, str):
|
| 55 |
parsed = parse_sizes(size)
|
|
|
|
| 57 |
elif isinstance(size, (tuple, list)) and len(size) == 2:
|
| 58 |
target_size = (int(size[0]), int(size[1]))
|
| 59 |
else:
|
| 60 |
+
target_size = (256, 576)
|
| 61 |
|
| 62 |
print(f"Loading skin image from: {skin_path}")
|
| 63 |
skin_img = Image.open(skin_path).convert("RGBA")
|
| 64 |
skin_np = np.array(skin_img)
|
| 65 |
|
| 66 |
+
if skin_img.size != (64, 64):
|
| 67 |
+
raise ValueError(
|
| 68 |
+
f"DifferentiableRenderer requires a 64x64 skin, got {skin_img.size[0]}x{skin_img.size[1]}."
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
# Clean semi-transparency to match build_target_img preprocessing
|
| 72 |
alpha = skin_np[..., 3]
|
| 73 |
semi_transparent = (alpha > 0) & (alpha < 255)
|
| 74 |
skin_np[semi_transparent, 3] = 255
|
| 75 |
+
skin_tensor = (
|
| 76 |
+
torch.from_numpy(skin_np)
|
| 77 |
+
.permute(2, 0, 1)
|
| 78 |
+
.unsqueeze(0)
|
| 79 |
+
.to(dtype=torch.float32)
|
| 80 |
+
/ 255.0
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
mappings_dir = os.path.join(dmr_path, f"mappings_{target_size[0]}x{target_size[1]}")
|
| 84 |
+
if not os.path.isdir(mappings_dir):
|
| 85 |
+
raise FileNotFoundError(
|
| 86 |
+
f"Differentiable renderer mappings not found for size "
|
| 87 |
+
f"{target_size[0]}x{target_size[1]}: {mappings_dir}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
|
|
|
| 89 |
|
| 90 |
+
renderer = DifferentiableRenderer(mappings_dir=mappings_dir, bg_color=(0.0, 0.0, 0.0))
|
| 91 |
+
missing_views = [view_name for view_name in VIEW_NAMES if view_name not in renderer.views]
|
| 92 |
+
if missing_views:
|
| 93 |
+
raise KeyError(
|
| 94 |
+
f"Views missing from renderer mappings: {', '.join(missing_views)}. "
|
| 95 |
+
f"Available views: {', '.join(renderer.views)}"
|
| 96 |
+
)
|
| 97 |
|
| 98 |
+
rendered_images = []
|
| 99 |
+
with torch.inference_mode():
|
| 100 |
+
for view_name in VIEW_NAMES:
|
| 101 |
+
print(
|
| 102 |
+
f"Rendering view with DifferentiableRenderer: {view_name} "
|
| 103 |
+
f"(Size: {target_size})..."
|
| 104 |
+
)
|
| 105 |
+
rendered = renderer.forward_view(skin_tensor, view_name)
|
| 106 |
+
rendered_np = (
|
| 107 |
+
rendered.squeeze(0)
|
| 108 |
+
.permute(1, 2, 0)
|
| 109 |
+
.clamp(0.0, 1.0)
|
| 110 |
+
.mul(255.0)
|
| 111 |
+
.round()
|
| 112 |
+
.to(torch.uint8)
|
| 113 |
+
.cpu()
|
| 114 |
+
.numpy()
|
| 115 |
+
)
|
| 116 |
+
rendered_images.append(Image.fromarray(rendered_np, mode="RGBA"))
|
| 117 |
+
|
| 118 |
+
widths, heights = zip(*(image.size for image in rendered_images))
|
| 119 |
+
merged_img = Image.new("RGBA", (sum(widths), max(heights)))
|
| 120 |
+
x_offset = 0
|
| 121 |
+
for image in rendered_images:
|
| 122 |
+
merged_img.paste(image, (x_offset, 0))
|
| 123 |
+
x_offset += image.width
|
| 124 |
|
| 125 |
parsed_bg = parse_bg_color(bg_color)
|
| 126 |
if parsed_bg is not None:
|
|
|
|
| 137 |
|
| 138 |
def main():
|
| 139 |
parser = argparse.ArgumentParser(
|
| 140 |
+
description=(
|
| 141 |
+
"Create a horizontal front-left, front-right, back-left, and back-right "
|
| 142 |
+
"16:9 reference template using the differentiable renderer."
|
| 143 |
+
)
|
| 144 |
)
|
| 145 |
parser.add_argument("--skin", "--skin_path", dest="skin_path", required=True, help="Path to input skin image file.")
|
| 146 |
parser.add_argument("--output", "--output_path", dest="output_path", required=True, help="Path to save the merged output template image.")
|
| 147 |
parser.add_argument("--renderer_path", "--renderer", dest="renderer_path", required=True, help="Path to differentiable_minecraft_renderer directory.")
|
| 148 |
+
parser.add_argument(
|
| 149 |
+
"--size",
|
| 150 |
+
"--resolution",
|
| 151 |
+
default="344x768",
|
| 152 |
+
help="Resolution per view (default: 344x768; four horizontal views produce 1376x768).",
|
| 153 |
+
)
|
| 154 |
parser.add_argument("--bg_color", "--bg-color", "--background", dest="bg_color", default="black", help="Background color for the output template image (default: black). Set to 'none' or 'transparent' for transparency.")
|
| 155 |
|
| 156 |
args = parser.parse_args()
|
DDJ_real2render/real2render.py
CHANGED
|
@@ -12,7 +12,7 @@ DEFAULT_TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
|
| 12 |
# Default prompt for real-to-render Minecraft character generation
|
| 13 |
DEFAULT_PROMPT = """生成[图1]中角色的Minecraft形象
|
| 14 |
|
| 15 |
-
- 人物模型必须与minecraft的玩家模型相同,且为包含内层/外层贴图的双图层模型,不能有额外元素。texture不能高于Minecraft所支持的分辨率。模型参考[图2]
|
| 16 |
|
| 17 |
- 生成的minecraft角色的尺寸、朝向、姿势与[图2]完全一致,且必须包含正面图与背面图,并排放置。
|
| 18 |
|
|
@@ -73,10 +73,10 @@ def main():
|
|
| 73 |
parser.add_argument("-t2", "--template2", default=None, help="Path to second reference template image (Graph 3).")
|
| 74 |
parser.add_argument("-o", "--output", help="Output path for the generated image.")
|
| 75 |
parser.add_argument("-p", "--prompt", default=DEFAULT_PROMPT, help="Prompt for image generation.")
|
| 76 |
-
parser.add_argument("-a", "--aspect-ratio", default="
|
| 77 |
choices=["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"],
|
| 78 |
-
help="Aspect ratio
|
| 79 |
-
parser.add_argument("-s", "--image-size", default="
|
| 80 |
args = parser.parse_args()
|
| 81 |
|
| 82 |
try:
|
|
|
|
| 12 |
# Default prompt for real-to-render Minecraft character generation
|
| 13 |
DEFAULT_PROMPT = """生成[图1]中角色的Minecraft形象
|
| 14 |
|
| 15 |
+
- 人物模型必须与minecraft的玩家模型相同,且为包含内层/外层贴图的双图层模型,不能有额外元素。texture不能高于Minecraft所支持的分辨率。模型参考[图2]
|
| 16 |
|
| 17 |
- 生成的minecraft角色的尺寸、朝向、姿势与[图2]完全一致,且必须包含正面图与背面图,并排放置。
|
| 18 |
|
|
|
|
| 73 |
parser.add_argument("-t2", "--template2", default=None, help="Path to second reference template image (Graph 3).")
|
| 74 |
parser.add_argument("-o", "--output", help="Output path for the generated image.")
|
| 75 |
parser.add_argument("-p", "--prompt", default=DEFAULT_PROMPT, help="Prompt for image generation.")
|
| 76 |
+
parser.add_argument("-a", "--aspect-ratio", default="16:9",
|
| 77 |
choices=["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"],
|
| 78 |
+
help="Aspect ratio.")
|
| 79 |
+
parser.add_argument("-s", "--image-size", default="2K", choices=["1K", "2K", "4K"], help="Resolution size (default: 2K).")
|
| 80 |
args = parser.parse_args()
|
| 81 |
|
| 82 |
try:
|
DDJ_real2render/template/template18.png
ADDED
|
Git LFS Details
|
DDJ_real2render/template/template_inner.png
ADDED
|
Git LFS Details
|
DDJ_real2render/template/template_outer.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img6_template18.png
ADDED
|
Git LFS Details
|