Datasets:
Download code/robohome/src/tools/asset_curation/urdfunion.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/tools/asset_curation/urdfunion.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome/code/robohome/src/tools/asset_curation/urdfunion.py
-
curl -L -o urdfunion.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/tools/asset_curation/urdfunion.py
2.77 kB
| import curation_paths as paths | |
| import json,os,hashlib,numpy as np,sys,math,xml.etree.ElementTree as ET | |
| sys.path.insert(0,paths.AUDIT_SCRIPTS) | |
| from glbpts import read | |
| from multiprocessing import Pool | |
| A=paths.A | |
| def rpy(r,p,y): | |
| cr,sr,cp,sp,cy,sy=math.cos(r),math.sin(r),math.cos(p),math.sin(p),math.cos(y),math.sin(y) | |
| return np.array([[cy*cp,cy*sp*sr-sy*cr,cy*sp*cr+sy*sr],[sy*cp,sy*sp*sr+cy*cr,sy*sp*cr-cy*sr],[-sp,cp*sr,cp*cr]]) | |
| def T(org): | |
| M=np.eye(4) | |
| if org is not None: | |
| xyz=[float(v) for v in (org.get("xyz") or "0 0 0").split()]; r=[float(v) for v in (org.get("rpy") or "0 0 0").split()] | |
| M[:3,:3]=rpy(*r); M[:3,3]=xyz | |
| return M | |
| def union(path): | |
| root=ET.parse(path).getroot(); base=os.path.dirname(path) | |
| joints={} | |
| for j in root.findall("joint"): | |
| joints[j.find("child").get("link")]=(j.find("parent").get("link"),T(j.find("origin"))) | |
| def world(l,g=0): | |
| if l not in joints or g>12: return np.eye(4) | |
| p,M=joints[l]; return world(p,g+1)@M | |
| pts=[] | |
| for l in root.findall("link"): | |
| W=world(l.get("name")) | |
| for v in l.findall("visual"): | |
| m=v.find("geometry/mesh") if v.find("geometry") is not None else None | |
| if m is None: continue | |
| f=os.path.join(base,m.get("filename")) | |
| if not os.path.exists(f): continue | |
| P=read(f) | |
| if P is None: continue | |
| sc=np.array([float(x) for x in (m.get("scale") or "1 1 1").split()]); P=P*sc | |
| M=W@T(v.find("origin")); pts.append(P@M[:3,:3].T+M[:3,3]) | |
| return np.concatenate(pts) if pts else None | |
| def work(a): | |
| f=A+"/pc3/"+hashlib.md5(a["key"].encode()).hexdigest()+".npz" | |
| if os.path.exists(f): return | |
| try: | |
| P=union(a["urdf_path"]); err="" | |
| except Exception as e: P=None; err=str(e)[:60] | |
| if P is None: np.savez(f,key=a["key"],err=err or "no_mesh"); return | |
| rng=np.random.default_rng(0) | |
| if len(P)>6000: P=P[rng.choice(len(P),6000,replace=False)] | |
| np.savez(f,key=a["key"],pts=P.astype(np.float32),err="") | |
| if __name__=="__main__": | |
| import sqlite3,glob,collections as C | |
| os.makedirs(A+"/pc3",exist_ok=True) | |
| db=sqlite3.connect("file:"+paths.L+"/full_matrix_reviewed_compatibility_v3.sqlite3?mode=ro",uri=True) | |
| paths={a:json.loads(rj).get("canonical_path") for a,rj in db.execute("select asset_id,record_json from assets where asset_id like 'hssd:urdf/%'")} | |
| need={p[0] for p in json.load(open(A+"/pc_pairs.json"))} | |
| spec=[dict(key="U:"+u,urdf_path=paths[u]) for u in sorted(need)] | |
| with Pool(24) as p: p.map(work,spec,chunksize=6) | |
| c=C.Counter() | |
| for f in glob.glob(A+"/pc3/*.npz"): | |
| z=np.load(f,allow_pickle=True); c["pts" if "pts" in z.files else "fail:"+str(z["err"])]+=1 | |
| print(dict(c)) | |