def classify_for_scene(scene, params): """Get correct classification for a given scene.""" left_moves = \ { featuresturn.make_key("left", initial_pos, current_pos): featuresturn.class_names[featuresturn.get_move_left_classification( initial_pos, current_pos, scene.fvalues, scene.maxima, params)] for initial_pos in range(0, scene.step_count) for current_pos in range(0, initial_pos + 1) } right_moves = \ { featuresturn.make_key("right", initial_pos, current_pos): featuresturn.class_names[featuresturn.get_move_right_classification( initial_pos, current_pos, scene.fvalues, scene.maxima, params)] for initial_pos in range(0, scene.step_count) for current_pos in range(initial_pos, scene.step_count) } return dict(left_moves.items() + right_moves.items())
def _sweep(self, direction): """Sweep the lens in one direction and return a tuple (success state, number of steps taken) along the way. """ initial_position = self.camera.last_position() sweep_fvalues = [ self.camera.last_fmeasure() ] while not self.camera.will_hit_edge(direction): # Move the lens forward. self.camera.move_coarse(direction) sweep_fvalues.append(self.camera.last_fmeasure()) # Take at least two steps before we allow turning back. if len(sweep_fvalues) < 3: continue if self.perfect_classification is None: # Obtain the ML classification at the new lens position. evaluator = featuresturn.action_feature_evaluator( sweep_fvalues, self.scene.step_count) classification = evaluatetree.evaluate_tree( self.params.action_tree, evaluator) else: key = featuresturn.make_key(str(direction), initial_position, self.camera.last_position()) classification = self.perfect_classification[key] if classification != "continue": assert (classification == "turn_peak" or classification == "backtrack") return classification, len(sweep_fvalues) - 1 # We've reached an edge, but the decision tree still does not want # to turn back, so what do we do now? # After thinking a lot about it, I think the best thing to do is to # introduce a condition manually. It's a bit ad-hoc, but we really need # to be able to handle this case robustly, as there are lot of cases # (i.e., landscape shots) where peaks will be at the edge. min_val = min(self.camera.get_fvalues(self.camera.visited_positions)) max_val = max(self.camera.get_fvalues(self.camera.visited_positions)) if float(min_val) / max_val > 0.8: return "backtrack", len(sweep_fvalues) - 1 else: return "turn_peak", len(sweep_fvalues) - 1