Spaces:
Running on Zero
Running on Zero
File size: 10,769 Bytes
c9e90a8 | 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | from __future__ import annotations
import argparse
import csv
import sys
from pathlib import Path
from typing import Sequence
import torch
from PIL import Image
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
import demo
import train
from utils.fbx_to_tensor import AU_NAME
def main() -> None:
args = parse_args()
mesh_path = args.mesh.expanduser().resolve()
checkpoint_path = args.checkpoint.expanduser().resolve()
output_dir = args.output_dir.expanduser().resolve()
output_dir.mkdir(parents=True, exist_ok=True)
checkpoint = demo.load_checkpoint(checkpoint_path)
config = demo.checkpoint_config(checkpoint, args.config)
device = demo.resolve_device(args.device, config)
model = demo.load_model(checkpoint, config, device)
raw_mesh = demo.load_mesh_for_model(mesh_path)
input_landmark_json = demo.ensure_mediapipe_landmarks(
mesh_path=mesh_path,
cache_dir=args.landmark_cache_dir.expanduser(),
mapper_path=args.mediapipe_mapper.expanduser(),
blender=args.blender,
force=args.refresh_landmarks,
)
reference_mesh_path = demo.metahuman_sample_mesh(
args.metahuman_sample_dir.expanduser()
)
reference_landmark_json = demo.ensure_mediapipe_landmarks(
mesh_path=reference_mesh_path,
cache_dir=args.landmark_cache_dir.expanduser(),
mapper_path=args.mediapipe_mapper.expanduser(),
blender=args.blender,
force=args.refresh_landmarks,
prefer_sidecar=True,
)
mesh = preprocess_mesh_for_batch(
raw_mesh=raw_mesh,
config=config,
mesh_path=mesh_path,
input_convention=args.input_convention,
input_landmarks=demo.load_landmark_mapping(input_landmark_json),
reference_mesh_path=reference_mesh_path,
reference_landmarks=demo.load_landmark_mapping(reference_landmark_json),
min_alignment_landmarks=args.min_alignment_landmarks,
alignment_mode=args.alignment_mode,
)
action_unit_ids = parse_au_ids(args.au_ids)
prefix = args.prefix or mesh_path.stem
rows: list[dict[str, str]] = []
image_paths: list[Path] = []
write_neutral_mesh(
mesh=mesh,
output_dir=output_dir,
prefix=prefix,
output_format=args.output_format,
output_convention=args.output_convention,
)
for index, action_unit_id in enumerate(action_unit_ids, start=1):
action_unit_name = AU_NAME[action_unit_id]
stem = f"{prefix}_au{action_unit_id:02d}_{action_unit_name}"
image_path = output_dir / f"{stem}.jpg"
output_mesh_path = output_dir / f"{stem}.{args.output_format}"
deformed_vertices, predicted_delta = demo.run_model(
model=model,
mesh=mesh,
action_unit_id=action_unit_id,
config=config,
device=device,
)
image = demo.render_output_image(
neutral_vertices=mesh["vertices"],
deformed_vertices=deformed_vertices,
normals=mesh.get("normals"),
faces=mesh["faces"],
action_unit_id=action_unit_id,
config=config,
device=device,
)
image.save(image_path, quality=95)
export_vertices = demo.output_vertices_for_convention(
deformed_vertices,
args.output_convention,
)
demo.export_mesh(
vertices=export_vertices,
faces=mesh["faces"],
normals=demo.recompute_vertex_normals(export_vertices, mesh["faces"]),
path=output_mesh_path,
)
row = {
"action_unit_id": str(action_unit_id),
"action_unit_name": action_unit_name,
"delta_mean_norm": f"{predicted_delta.norm(dim=-1).mean().item():.9f}",
"delta_max_norm": f"{predicted_delta.norm(dim=-1).max().item():.9f}",
"image_path": str(image_path),
"mesh_path": str(output_mesh_path),
}
rows.append(row)
image_paths.append(image_path)
print(
f"[{index:03d}/{len(action_unit_ids):03d}] "
f"AU{action_unit_id:02d} {action_unit_name} "
f"mean={row['delta_mean_norm']} max={row['delta_max_norm']}",
flush=True,
)
if device.type == "cuda":
torch.cuda.empty_cache()
summary_path = output_dir / "summary.csv"
write_summary(summary_path, rows)
if not args.no_contact_sheet:
write_contact_sheet(output_dir / "contact_sheet.jpg", image_paths)
print(f"Loaded checkpoint: {checkpoint_path}")
print(f"Loaded mesh: {mesh_path}")
print(f"Wrote summary: {summary_path}")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Run a TopoRig checkpoint over multiple action units."
)
parser.add_argument("--mesh", type=Path, required=True)
parser.add_argument("--checkpoint", type=Path, required=True)
parser.add_argument("--output-dir", type=Path, required=True)
parser.add_argument("--config", type=Path)
parser.add_argument(
"--au-ids",
nargs="+",
default=["all"],
help="Action-unit ids or names. Use 'all' for every AU_NAME entry.",
)
parser.add_argument("--prefix", default=None)
parser.add_argument("--output-format", choices=("glb", "obj"), default="glb")
parser.add_argument(
"--output-convention",
choices=("metahuman", "model"),
default="metahuman",
)
parser.add_argument("--device", default=None)
parser.add_argument(
"--input-convention",
choices=(
"auto",
"fbx",
"custom-glb",
"custom-glb-y-front",
"custom-glb-neg-y-front",
"custom-glb-y-up-z-front",
"custom-glb-y-up-neg-z-front",
),
default="auto",
)
parser.add_argument(
"--alignment-mode",
choices=("none", "similarity", "scale_translation"),
default="similarity",
help=(
"Landmark alignment mode. Use none for an already canonicalized "
"mesh, or scale_translation for custom GLBs whose orientation "
"should be preserved."
),
)
parser.add_argument(
"--metahuman-sample-dir",
type=Path,
default=demo.DEFAULT_METAHUMAN_SAMPLE_DIR,
)
parser.add_argument(
"--landmark-cache-dir",
type=Path,
default=demo.DEFAULT_LANDMARK_CACHE_DIR,
)
parser.add_argument(
"--mediapipe-mapper",
type=Path,
default=demo.DEFAULT_MEDIAPIPE_MAPPER,
)
parser.add_argument("--blender", default=None)
parser.add_argument("--refresh-landmarks", action="store_true")
parser.add_argument("--min-alignment-landmarks", type=int, default=32)
parser.add_argument("--no-contact-sheet", action="store_true")
return parser.parse_args()
def preprocess_mesh_for_batch(
raw_mesh: dict[str, torch.Tensor],
config: dict,
mesh_path: Path,
input_convention: str,
input_landmarks: dict[int, int],
reference_mesh_path: Path,
reference_landmarks: dict[int, int],
min_alignment_landmarks: int,
alignment_mode: str,
) -> dict[str, torch.Tensor]:
processed_mesh = train.preprocess_custom_visualization_mesh(
raw_mesh,
config,
input_suffix=mesh_path.suffix.lower(),
input_convention=input_convention,
)
if alignment_mode == "none":
processed_mesh["landmarks_3d"] = train.landmark_payload_from_mapping(
processed_mesh,
input_landmarks,
)
return processed_mesh
reference_mesh = train.preprocess_custom_visualization_mesh(
demo.load_mesh_for_model(reference_mesh_path),
config,
input_suffix=reference_mesh_path.suffix.lower(),
input_convention="auto",
)
aligned_mesh = train.align_mesh_to_reference_landmarks(
mesh=processed_mesh,
input_landmarks=input_landmarks,
reference_mesh=reference_mesh,
reference_landmarks=reference_landmarks,
min_landmarks=min_alignment_landmarks,
alignment_mode=alignment_mode,
)
aligned_mesh["landmarks_3d"] = train.landmark_payload_from_mapping(
aligned_mesh,
input_landmarks,
)
return aligned_mesh
def write_neutral_mesh(
mesh: dict[str, torch.Tensor],
output_dir: Path,
prefix: str,
output_format: str,
output_convention: str,
) -> None:
neutral_vertices = demo.output_vertices_for_convention(
mesh["vertices"],
output_convention,
)
demo.export_mesh(
vertices=neutral_vertices,
faces=mesh["faces"],
normals=demo.recompute_vertex_normals(neutral_vertices, mesh["faces"]),
path=output_dir / f"{prefix}_canonical_neutral.{output_format}",
)
def parse_au_ids(values: Sequence[str]) -> list[int]:
if len(values) == 1 and values[0].strip().lower() == "all":
return sorted(AU_NAME)
parsed = [demo.parse_au_id(value) for value in values]
unknown = [action_unit_id for action_unit_id in parsed if action_unit_id not in AU_NAME]
if unknown:
raise ValueError(f"Unknown AU ids: {unknown}")
return parsed
def write_summary(path: Path, rows: Sequence[dict[str, str]]) -> None:
fieldnames = [
"action_unit_id",
"action_unit_name",
"delta_mean_norm",
"delta_max_norm",
"image_path",
"mesh_path",
]
with path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
def write_contact_sheet(path: Path, image_paths: Sequence[Path]) -> None:
if not image_paths:
return
thumbnails: list[Image.Image] = []
for image_path in image_paths:
image = Image.open(image_path).convert("RGB")
image.thumbnail((420, 220), Image.Resampling.LANCZOS)
thumbnails.append(image.copy())
image.close()
columns = 4
cell_width = max(image.width for image in thumbnails)
cell_height = max(image.height for image in thumbnails)
rows = (len(thumbnails) + columns - 1) // columns
sheet = Image.new("RGB", (columns * cell_width, rows * cell_height), "white")
for index, image in enumerate(thumbnails):
x = (index % columns) * cell_width + (cell_width - image.width) // 2
y = (index // columns) * cell_height + (cell_height - image.height) // 2
sheet.paste(image, (x, y))
sheet.save(path, quality=95)
if __name__ == "__main__":
main()
|