File size: 1,441 Bytes
20033c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np,cv2

def locate(rgb):
 rgb={k:cv2.resize(np.asarray(v).squeeze(),(240,320) if k=="top_camera" else (320,240),interpolation=cv2.INTER_AREA) for k,v in rgb.items() if k in ("front_camera","top_camera")}
 out=[]
 for cam in ('front_camera','top_camera'):
  x=np.asarray(rgb[cam]).squeeze().astype(float)/255;h,w=x.shape[:2]
  for col in ('red','blue'):
   if col=='red':m=(x[:,:,0]>x[:,:,1]*1.6)&(np.abs(x[:,:,1]-x[:,:,2])<.08)&(x[:,:,0]>.45)
   else:m=(x[:,:,2]>x[:,:,0]*1.4)&(x[:,:,2]>x[:,:,1]*1.2)&(x[:,:,2]>.3)
   y,xx=np.nonzero(m)
   out.extend([xx.mean()/w if len(y) else .5,y.mean()/h if len(y) else .5])
 x=np.asarray(rgb['top_camera']).squeeze().astype(float)/255;h,w=x.shape[:2]
 yy,xx=np.mgrid[:h,:w]
 m=(xx>w*.2)&(xx<w*.43)&(yy>h*.15)&(yy<h*.38)&(x.max(2)-x.min(2)<.10)&(x.mean(2)>.35)&(x.mean(2)<.85)
 y,z=np.nonzero(m);out.extend([z.mean()/w if len(y) else .33,y.mean()/h if len(y) else .27])
 return np.array(out)
if __name__=='__main__':
 import sys,json
 sys.path.insert(0,'/')
 from worker_api import API
 a=API();manifest=json.load(open('/demo_manifest.json'));features=[]
 for e in manifest['episodes']:
  d=a.read_demo(e['demo_id'],0,1,['obs/sensor_data/'+c+'/rgb' for c in ('front_camera','top_camera')]);features.append(locate({c:d['obs/sensor_data/'+c+'/rgb'][0] for c in ('front_camera','top_camera')}))
 np.save('/workspace/exploration/initial_locations.npy',features)
 print(np.round(features,3))