def draw_puck(self, bag): """ Draws the puck into the image. :param bag: :return: """ if 'position' in bag.puck: # draw puck itself color = ((255, 0, 0) if bag.puck.is_predicted_position else (0, 255, 0)) cv2.circle(bag.image, bag.puck.position_pixel, bag.puck.radius_unscaled, color, 2) cv2.circle(bag.image, bag.puck.position_pixel, 2, (0, 0, 255), 3) if 'path' in bag.puck: # draw puck movement #direction_velocity = vector.mul_by(bag.puck.direction, bag.puck.velocity) #destination = vector.add(bag.puck.position, direction_velocity) #destination_pixel = vector.coord_to_image_space(destination, bag.table_boundaries) #cv2.line(bag.image, bag.puck.position_pixel, destination_pixel, (255, 0, 0)) # draw path for i in xrange(0, len(bag.puck.path) - 1): position = vector.coord_to_image_space(bag.puck.path[i][0], bag.table_boundaries) destination = vector.coord_to_image_space(bag.puck.path[i + 1][0], bag.table_boundaries) cv2.line(bag.image, position, destination, (255, 0, 0)) cv2.circle(bag.image, position, 1, (0, 0, 255), 2) position_pixel = vector.coord_to_image_space(bag.puck.path[-1][0], bag.table_boundaries) direction_velocity = vector.mul_by(bag.puck.path[-1][1], bag.puck.path[-1][2]) destination = vector.add(bag.puck.path[-1][0], direction_velocity) destination_pixel = vector.coord_to_image_space(destination, bag.table_boundaries) cv2.line(bag.image, position_pixel, destination_pixel, (255, 0, 0))
def draw_stick(self, bag): """ Draws the stick and it's destination. :param bag: The parameter bag. :return: """ if 'stick' in bag and 'table_boundaries' in bag: # draw stick itself position_unscaled = vector.coord_to_image_space(bag.stick.position, bag.table_boundaries) cv2.circle(bag.image, position_unscaled, 10, (0, 255, 0), 2) cv2.circle(bag.image, position_unscaled, 2, (50, 50, 50), 3) if 'dest_position' in bag.stick: # draw desired stick movement dest_position_unscaled = vector.coord_to_image_space(bag.stick.dest_position, bag.table_boundaries) cv2.line(bag.image, position_unscaled, dest_position_unscaled, (0, 0, 0))
def _check_for_wild_shot(self, bag): """ Checks if the latest puck position is a wild shot. Requires bag.puck.predicted_position. :param bag: The parameter bag :return: """ #TODO ueberlegen ob nutzen oder loeschen predicted_position_pixel = vector.coord_to_image_space(bag.puck.predicted_position, bag.table_boundaries) diff = vector.length(vector.from_to(bag.puck.position_pixel, predicted_position_pixel)) # use time_diff and velocity to play with this value #accepted = self.ACCEPTABLE_PUCK_POSITION_DIFF * bag.time_diff * max((1000 * self.last_path[-1][2]) if len(self.last_path) > 0 else 1.0, 1.0) #return diff > accepted return False
def use_predicted_point(self, bag): #aufgerufen """ Uses a predicted point instead of taking a detected one. :param bag: The parameter bag. :return: """ # don't try anything is the buffer is empty if self.is_empty(): return print('Puck not detected. Using predicted position') # increase prediction count self.prediction_count += 1 if self.prediction_count > const.CONST.MAX_SUCCESSIVE_PREDICTION_AMOUNT: print("%i successive puck predictions; assuming the puck is out of the image" % self.prediction_count) self._reset() return current_time = time.time() if 'predicted_position' not in bag.puck: self._predict_position(bag, current_time) # use predicted position, but double check: can't always predict if 'predicted_position' in bag.puck: bag.puck.position = bag.puck.predicted_position bag.puck.position_pixel = vector.coord_to_image_space(bag.puck.position, bag.table_boundaries) bag.puck.is_predicted_position = True bag.puck.detection_time = current_time # save radius bag.next.puck.radius_unscaled = bag.puck.radius_unscaled print("Predicted position: (%f, %f)" % bag.puck.position) # add (if we couldn't predict the position, we add nothing) self._add_point(bag)