Download code/tools/hvs_core.py from zhizeng115/EchoScout-FourTask: direct link, hf CLI and curl.
- Browser
- Download file 5.31 kB
-
https://huggingface.co/datasets/zhizeng115/EchoScout-FourTask/resolve/main/code/tools/hvs_core.py
- Command line
-
hf download hf://datasets/zhizeng115/EchoScout-FourTask/code/tools/hvs_core.py
-
curl -L -o hvs_core.py https://huggingface.co/datasets/zhizeng115/EchoScout-FourTask/resolve/main/code/tools/hvs_core.py
5.31 kB
| """Lightweight H* compatible panorama interface, no Habitat dependency. | |
| The panorama is private environment state. Only rendered perspective images and | |
| public instructions/pose should be sent to a policy. Angles are degrees. | |
| """ | |
| import math, re | |
| import cv2 | |
| import numpy as np | |
| HFOV=100.0 | |
| SYSTEM = ('You perform visual search from a fixed position. You see only the current ' | |
| 'perspective image. Rotate to gather evidence, then center the requested object ' | |
| 'or face the requested traversable path. Take one action per turn: ' | |
| '<answer>rotate(delta_yaw,delta_pitch)</answer> or ' | |
| '<answer>submit(current_yaw,current_pitch)</answer>. Use integer degrees. ' | |
| 'Positive yaw turns right; positive pitch looks up. Submit the CURRENT pose. ' | |
| 'Do not infer that an unseen target is already visible.') | |
| def circular_delta(target, current): | |
| return (target-current+180)%360-180 | |
| def perspective(pano, yaw, pitch, height=360, fov=HFOV, crosshair=True): | |
| """Same spherical convention as Hstar: center longitude zero at W/2.""" | |
| width=height//9*16 | |
| focal=width/(2*math.tan(math.radians(fov)/2)) | |
| v,u=np.indices((height,width),dtype=np.float32) | |
| rays=np.stack(((u-width/2)/focal,(v-height/2)/focal,np.ones_like(u)),axis=-1) | |
| rays/=np.linalg.norm(rays,axis=-1,keepdims=True) | |
| a,b=math.radians(yaw),math.radians(pitch) | |
| x,y,z=rays[...,0],rays[...,1],rays[...,2] | |
| yp=y*math.cos(b)-z*math.sin(b);zp=y*math.sin(b)+z*math.cos(b) | |
| xp=x*math.cos(a)+zp*math.sin(a);zp=-x*math.sin(a)+zp*math.cos(a) | |
| longitude=np.arctan2(xp,zp);latitude=np.arcsin(np.clip(yp,-1,1)) | |
| ph,pw=pano.shape[:2] | |
| mx=((longitude/(2*np.pi)+.5)*pw).astype(np.float32) | |
| my=((latitude/np.pi+.5)*ph).astype(np.float32) | |
| # wrap only longitude; avoid pole-to-pole interpolation across vertical edge. | |
| result=cv2.remap(pano,mx%pw,np.clip(my,0,ph-1),cv2.INTER_LINEAR,borderMode=cv2.BORDER_WRAP) | |
| if crosshair: | |
| cx,cy=width//2,height//2 | |
| cv2.line(result,(cx-8,cy),(cx+8,cy),(0,255,0),1) | |
| cv2.line(result,(cx,cy-8),(cx,cy+8),(0,255,0),1) | |
| return result | |
| def bbox_angles(bbox,width,height): | |
| x1,y1,x2,y2=map(float,bbox) | |
| if not (0<=x1<x2<width and 0<=y1<y2<height): | |
| raise ValueError('invalid or seam-spanning bounding box') | |
| # Source bbox upper endpoints are inclusive pixel centers. | |
| yaw=[(x1/width*360-180)%360,(x2/width*360-180)%360] | |
| pitch=[90-y2/height*180,90-y1/height*180] | |
| center=[((x1+x2)/2/width*360-180)%360,sum(pitch)/2] | |
| return yaw,pitch,center | |
| def bbox_visible_fraction(bbox,shape,yaw,pitch=0,fov=HFOV,aspect=16/9): | |
| """Spherical-area-weighted bbox proxy, NOT a true instance-mask visibility.""" | |
| h,w=shape[:2];x1,y1,x2,y2=bbox | |
| xs=np.linspace(x1,x2,31);ys=np.linspace(y1,y2,21) | |
| lon,lat=np.meshgrid(xs/w*2*np.pi-np.pi,ys/h*np.pi-np.pi/2) | |
| dx=np.cos(lat)*np.sin(lon);dy=np.sin(lat);dz=np.cos(lat)*np.cos(lon) | |
| a,b=math.radians(yaw),math.radians(pitch) | |
| cx=dx*math.cos(a)-dz*math.sin(a);cz=dx*math.sin(a)+dz*math.cos(a) | |
| cy=dy*math.cos(b)+cz*math.sin(b);cz=-dy*math.sin(b)+cz*math.cos(b) | |
| tanx=math.tan(math.radians(fov)/2);tany=tanx/aspect | |
| inside=(cz>0)&(np.abs(cx)<=cz*tanx)&(np.abs(cy)<=cz*tany) | |
| weights=np.cos(lat) | |
| return float(weights[inside].sum()/weights.sum()) | |
| def in_interval(value,interval,circular=False,tolerance=0): | |
| lo,hi=interval | |
| if circular: | |
| span=(hi-lo)%360;center=(lo+span/2)%360 | |
| return abs(circular_delta(value,center))<=max(span/2,tolerance)+1e-6 | |
| return abs(value-(lo+hi)/2)<=max((hi-lo)/2,tolerance)+1e-6 | |
| class SearchEnv: | |
| def __init__(self,pano,annotation,start_yaw=0,height=360): | |
| self.pano=pano;self.annotation=annotation;self.yaw=float(start_yaw)%360 | |
| self.pitch=0.;self.height=height;self.done=False;self.steps=0 | |
| def observe(self): | |
| return perspective(self.pano,self.yaw,self.pitch,self.height) | |
| def public_text(self): | |
| return ('<image>\nTask: '+self.annotation['task']+ | |
| f'\nCurrent direction (yaw,pitch): ({self.yaw:g},{self.pitch:g}). Decide your next action.') | |
| def step(self,text): | |
| if self.done:raise ValueError('episode already ended') | |
| match=re.fullmatch(r'\s*<answer>(rotate|submit)\((-?\d+),\s*(-?\d+)\)</answer>\s*',text) | |
| if not match:raise ValueError('one integer rotate or submit action is required') | |
| op,a,b=match[1],int(match[2]),int(match[3]);self.steps+=1 | |
| if op=='rotate': | |
| self.yaw=(self.yaw+a)%360;self.pitch=max(-90,min(90,self.pitch+b)) | |
| return {'done':False,'pose':[self.yaw,self.pitch]} | |
| if abs(circular_delta(a,self.yaw))>.51 or abs(b-self.pitch)>.51: | |
| raise ValueError('submit must name the actual current pose; teleport submission rejected') | |
| if 'yaw' not in self.annotation: | |
| self.done=True | |
| return {'done':True,'success':None,'evaluated':False,'pose':[self.yaw,self.pitch]} | |
| is_hos='pitch' in self.annotation | |
| success=in_interval(self.yaw,self.annotation['yaw'],True,30 if is_hos else 10) | |
| if is_hos:success=success and in_interval(self.pitch,self.annotation['pitch'],False,20) | |
| self.done=True | |
| return {'done':True,'success':bool(success),'pose':[self.yaw,self.pitch]} | |