Daankular commited on
Commit
ac148ac
·
verified ·
1 Parent(s): a4521fe

Upload v2/check.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. v2/check.py +104 -0
v2/check.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import trimesh
3
+ import numpy as np
4
+
5
+
6
+ def check_head_model(model_path: str | Path) -> dict:
7
+ """Validate a head model mesh."""
8
+ model_path = Path(model_path)
9
+ if not model_path.exists():
10
+ return {"status": "error", "message": f"Model not found: {model_path}"}
11
+
12
+ try:
13
+ scene = trimesh.load(str(model_path))
14
+ except Exception as e:
15
+ return {"status": "error", "message": f"Failed to load model: {e}"}
16
+
17
+ results = []
18
+ if isinstance(scene, trimesh.Scene):
19
+ for name, g in scene.geometry.items():
20
+ if isinstance(g, trimesh.Trimesh):
21
+ centroid = g.vertices.mean(0)
22
+ y_min, y_max = g.vertices[:, 1].min(), g.vertices[:, 1].max()
23
+ results.append({
24
+ "mesh": name,
25
+ "centroid": centroid.round(3).tolist(),
26
+ "y_range": [float(y_min), float(y_max)]
27
+ })
28
+ else:
29
+ centroid = scene.vertices.mean(0)
30
+ y_min, y_max = scene.vertices[:, 1].min(), scene.vertices[:, 1].max()
31
+ results.append({
32
+ "mesh": model_path.stem,
33
+ "centroid": centroid.round(3).tolist(),
34
+ "y_range": [float(y_min), float(y_max)]
35
+ })
36
+
37
+ return {"status": "ok", "results": results}
38
+
39
+
40
+ def check_body_model(model_path: str | Path) -> dict:
41
+ """Validate a body model mesh with anatomical checks."""
42
+ model_path = Path(model_path)
43
+ if not model_path.exists():
44
+ return {"status": "error", "message": f"Model not found: {model_path}"}
45
+
46
+ try:
47
+ scene = trimesh.load(str(model_path))
48
+ except Exception as e:
49
+ return {"status": "error", "message": f"Failed to load model: {e}"}
50
+
51
+ if isinstance(scene, trimesh.Scene):
52
+ body = trimesh.util.concatenate(list(scene.geometry.values()))
53
+ else:
54
+ body = scene
55
+
56
+ v = body.vertices
57
+ y_min, y_max = v[:, 1].min(), v[:, 1].max()
58
+
59
+ checks = [
60
+ ('feet', 0.01),
61
+ ('hips', 0.50),
62
+ ('chest', 0.65),
63
+ ('neck', 0.75),
64
+ ('chin', 0.87),
65
+ ('crown', 0.99)
66
+ ]
67
+
68
+ results = []
69
+ for name, frac in checks:
70
+ y = y_min + (y_max - y_min) * frac
71
+ pts = v[(v[:, 1] > y - 0.03) & (v[:, 1] < y + 0.03)]
72
+ if len(pts) < 3:
73
+ continue
74
+ results.append({
75
+ "location": name,
76
+ "fraction": frac,
77
+ "x_mean": float(pts[:, 0].mean()),
78
+ "x_std": float(pts[:, 0].std()),
79
+ "z_mean": float(pts[:, 2].mean()),
80
+ "z_std": float(pts[:, 2].std()),
81
+ "points": int(len(pts))
82
+ })
83
+
84
+ return {"status": "ok", "y_range": [float(y_min), float(y_max)], "results": results}
85
+
86
+
87
+ def check_model(model_path: str | Path, model_type: str = "auto") -> dict:
88
+ """Check a model, auto-detecting type if needed."""
89
+ model_path = Path(model_path)
90
+
91
+ if model_type == "auto":
92
+ if "head" in model_path.name.lower():
93
+ model_type = "head"
94
+ elif "body" in model_path.name.lower():
95
+ model_type = "body"
96
+ else:
97
+ return {"status": "error", "message": "Could not auto-detect model type. Use --model-type to specify (head/body)"}
98
+
99
+ if model_type == "head":
100
+ return check_head_model(model_path)
101
+ elif model_type == "body":
102
+ return check_body_model(model_path)
103
+ else:
104
+ return {"status": "error", "message": f"Unknown model type: {model_type}"}