Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lichengunc/185391c5757858bfadda3b42283947f5 to your computer and use it in GitHub Desktop.
Save lichengunc/185391c5757858bfadda3b42283947f5 to your computer and use it in GitHub Desktop.
get best-view position for target object
def _get_best_yaw_obj_from_pos(self, obj_id, grid_pos, height=1.0, use_iou=True):
obj = self.objects[obj_id]
obj_fine_class = obj['fine_class']
cx, cy = self.env.house.to_coor(grid_pos[0], grid_pos[1])
self.env.cam.pos.x = cx
self.env.cam.pos.y = height
self.env.cam.pos.z = cy
best_yaw, best_coverage, best_mask = None, 0, None
for yaw in self.angles:
self.env.cam.yaw = yaw
self.env.cam.updateDirection()
seg = self.env.render(mode='semantic')
c = self.semantic_classes[obj_fine_class.lower()]
mask = np.all(seg == c, axis=2)
if use_iou:
coverage = self._compute_iou(mask)
else:
coverage = np.sum(mask) / (seg.shape[0] * seg.shape[1])
if best_yaw == None:
best_yaw = yaw
best_coverage = coverage
best_mask = mask
else:
if coverage > best_coverage:
best_yaw = yaw
best_coverage = coverage
best_mask = mask
return best_yaw, best_coverage, best_mask
def _compute_iou(self, cand_mask, ref_mask=None):
"""
Given (h, w) cand_mask, we wanna our ref_mask to be in the center of image,
with [0.25h:0.75h, 0.25w:0.75w] occupied.
"""
if ref_mask is None:
h, w = cand_mask.shape[0], cand_mask.shape[1]
ref_mask = np.zeros((h,w), np.int8)
ref_mask[int(0.25*h):int(0.85*h), int(0.25*w):int(0.75*w)] = 1
inter = (cand_mask > 0) & (ref_mask > 0)
union = (cand_mask > 0) | (ref_mask > 0)
iou = inter.sum() / (union.sum() + 1e-5)
return iou
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment