def __init__(self, scene=None, speaker=None): self.scene = scene if scene is None: self.scene = Scene(3) # not a very furnished scene, we only have one table table = Landmark('table', RectangleRepresentation(rect=BoundingBox([Vec2(5,5), Vec2(6,7)])), None, ObjectClass.TABLE) self.scene.add_landmark(table) self.table = table self.set_scene(scene,speaker)
class ModelScene(object): def __init__(self, scene=None, speaker=None): self.scene = scene if scene is None: self.scene = Scene(3) # not a very furnished scene, we only have one table table = Landmark('table', RectangleRepresentation(rect=BoundingBox([Vec2(5,5), Vec2(6,7)])), None, ObjectClass.TABLE) self.scene.add_landmark(table) self.table = table self.set_scene(scene,speaker) def set_scene(self,scene,speaker): self.scene = scene self.speaker = speaker self.table = self.scene.landmarks['table'] # there is a person standing at this location # he will be our reference if speaker is None: self.speaker = Speaker(Vec2(5.5, 4.5)) else: self.speaker = speaker # NOTE we need to keep around the list of landmarks so that we can # access them by id, which is the index of the landmark in this list # collect all possible landmarks self.landmarks = [] for scene_lmk in self.scene.landmarks.itervalues(): self.landmarks.append(scene_lmk) # a scene can be represented as a plane, line, etc # each representation of a scene has different landmarks rs = [scene_lmk.representation] rs.extend(scene_lmk.representation.get_alt_representations()) for r in rs: for lmk in r.get_landmarks(): self.landmarks.append(lmk) # FIXME we are using sentences with 1 or 2 LANDMARK-PHRASEs # so we need to restrict the landmarks to 0 or 1 ancestors self.landmarks = [l for l in self.landmarks if l.get_ancestor_count() < 2] def get_rand_loc(self): """returns a random location on the table""" bb = self.table.representation.get_geometry() xmin, ymin = bb.min_point xmax, ymax = bb.max_point return random.uniform(xmin, xmax), random.uniform(ymin, ymax) def get_landmark_id(self, lmk): return self.landmarks.index(lmk) def get_landmark_by_id(self, lmk_id): return self.landmarks[lmk_id] def sample_lmk_rel(self, loc, num_ancestors=None, usebest=False): """gets a location and returns a landmark and a relation that can be used to describe the given location""" landmarks = self.landmarks if num_ancestors is not None: landmarks = [l for l in landmarks if l.get_ancestor_count() == num_ancestors] loc = Landmark(None, PointRepresentation(loc), None, None) lmk, lmk_prob, lmk_entropy, head_on = self.speaker.sample_landmark( landmarks, loc, usebest=usebest) rel, rel_prob, rel_entropy = self.speaker.sample_relation(loc, self.table.representation.get_geometry(), head_on, lmk, step=0.5, usebest=usebest) rel = rel(head_on,lmk,loc) return (lmk, lmk_prob, lmk_entropy), (rel, rel_prob, rel_entropy)