def track_table(block): table_corners = get_rectangle_corners(block.table.ground.size) settings = TableTrackingSettings(False, None, None) controls = ControlGroupStatus(ControlGroup()) prev_tracker = None for f in xrange(block.frames): tracker = TableTracker(prev_tracker, settings, controls) prev_tracker = tracker corners = tracker.track_table(block.video.image[f]) block.camera.transform[f] = cv2.getPerspectiveTransform(table_corners, corners)
def __init__(self, frame, frame_num, timestamp, playback_time, frame_settings, prev_frame_analysis, controls): self.frame = frame self.frame_num = frame_num self.timestamp = timestamp self.playback_time = playback_time self.frame_settings = frame_settings self.controls = controls # Table tracker prev_table_tracker = prev_frame_analysis.table_tracker if prev_frame_analysis is not None else None self.table_tracker = TableTracker(prev_table_tracker, self.frame_settings.table_tracking_settings, controls.subpanel("table tracking"), ticker=self) # Table analysis self.prev_table_bg_estimation = prev_frame_analysis.table_bg_estimation if prev_frame_analysis is not None else None self.table_bg_estimation = None # Spot finder self.spots_finder = SpotsFinder(frame_settings.spots_finder_settings); # Computed data self.table_transform = None self.table_frame = None self.ball_density = None # Final data self.ball_pos = None # False if absent, (x, y) if present self.bars_pos = None # list of (lists of?) (shift, angle) # Timing data self.timings = {} # Record beginning of processing of this frame self.tic("lifetime")
def __init__(self, table, camera): self.table = table self.camera = camera self.frames_in_flight = 120 self.table_tracker = TableTracker(table, camera, TableReferenceFrame()) self.rectifier = Rectifier(table, margin=0.10, camera=camera, resolution=120) self.background_analyzer = BackgroundAnalyzer(table, self.rectifier) self.team_foosmen_analyzer = TeamFoosmenAnalyzer(table, self.rectifier) self.ball_analyzer = BallAnalyzer(table, self.rectifier) self.rod_analyzer = [RodAnalyzer(table, rod, self.rectifier) for rod in table.rods]
class GameAnalyzer: def __init__(self, table, camera): self.table = table self.camera = camera self.frames_in_flight = 120 self.table_tracker = TableTracker(table, camera, TableReferenceFrame()) self.rectifier = Rectifier(table, margin=0.10, camera=camera, resolution=120) self.background_analyzer = BackgroundAnalyzer(table, self.rectifier) self.team_foosmen_analyzer = TeamFoosmenAnalyzer(table, self.rectifier) self.ball_analyzer = BallAnalyzer(table, self.rectifier) self.rod_analyzer = [RodAnalyzer(table, rod, self.rectifier) for rod in table.rods] def analyze(self, data): self.table_tracker.locate_table(data) self.table_tracker.settle_table(data) self.table_tracker.compute_camera_transform(data) self.rectifier.rectify(data.frames) self.background_analyzer.estimate_color(data) self.background_analyzer.analyze_color(data) for i, team in enumerate(self.table.teams): self.team_foosmen_analyzer.initialize_model(team, data) self.team_foosmen_analyzer.analyze_color(team, data) self.team_foosmen_analyzer.compute_visible_llr(team, data) self.team_foosmen_analyzer.compute_location_llr(team, data) for i, rod in enumerate(self.table.rods): analyzer = self.rod_analyzer[i] for j, foosman in enumerate(rod.foosmen): foosman_analyzer = analyzer.foosman_analyzer[j] foosman_analyzer.compute_shift_llr(data) analyzer.compute_shift_llr(data) self.ball_analyzer.analyze_color(data) self.ball_analyzer.compute_visible_llr(data) self.ball_analyzer.compute_location_llr(data)
class FrameAnalysis: def __init__(self, frame, frame_num, timestamp, playback_time, frame_settings, prev_frame_analysis, controls): self.frame = frame self.frame_num = frame_num self.timestamp = timestamp self.playback_time = playback_time self.frame_settings = frame_settings self.controls = controls # Table tracker prev_table_tracker = prev_frame_analysis.table_tracker if prev_frame_analysis is not None else None self.table_tracker = TableTracker(prev_table_tracker, self.frame_settings.table_tracking_settings, controls.subpanel("table tracking"), ticker=self) # Table analysis self.prev_table_bg_estimation = prev_frame_analysis.table_bg_estimation if prev_frame_analysis is not None else None self.table_bg_estimation = None # Spot finder self.spots_finder = SpotsFinder(frame_settings.spots_finder_settings); # Computed data self.table_transform = None self.table_frame = None self.ball_density = None # Final data self.ball_pos = None # False if absent, (x, y) if present self.bars_pos = None # list of (lists of?) (shift, angle) # Timing data self.timings = {} # Record beginning of processing of this frame self.tic("lifetime") def do_undistort_frame(self): if self.frame_settings.undistort_camera: self.tic("undistort frame") self.frame = cv2.undistort(self.frame, self.frame_settings.camera_matrix, self.frame_settings.dist_coeffs) self.controls.subpanel("table tracking").show("undistorted", self.frame / 255.0) self.toc("undistort frame") def do_table_tracking(self): self.tic("table tracking") self.table_corners = self.table_tracker.track_table(self.frame) self.toc("table tracking") self.tic("table warping") warping_proj = numpy.dot(rectangle_to_region(self.table_corners), pixels_to_rectangle(*self.frame_settings.table_frame_size)) # logger.info("\n%r\n%r", self.table_corners, warping_proj) self.table_frame = cv2.warpPerspective(self.frame, warping_proj, self.frame_settings.table_frame_size, flags=cv2.INTER_LINEAR | cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_REPLICATE) / 255.0 self.toc("table warping") self.controls.subpanel("table tracking").show("table frame", self.table_frame) def do_compute_ball_density(self): self.tic("estimate background") self.table_bg_estimation = estimate_table_background(self.prev_table_bg_estimation, self.table_frame, self.frame_settings.table_bg_settings, self.controls.subpanel("tablebg")) self.toc("estimate background") self.tic("compute ball density") self.table_bg_analysis = analyze_table_background(self.table_bg_estimation, self.table_frame, self.controls.subpanel("tablebg")) # FIXME: the ball analysis is done in the analyze_table_background self.ball_density = self.table_bg_analysis.ballness self.toc("compute ball density") def find_spots(self): self.spots = self.spots_finder.find_spots(self.ball_density) def get_csv_line(self): line = "%.5f" % (self.playback_time) if self.ball_pos is not None: line += ",%.5f,%.5f" % (self.ball_pos[0], self.ball_pos[1]) else: line += ",," # FIXME: decide how bars are to be enumerated # for side in [0, 1]: # for bar in xrange(BARS): # line += ",%.5f,%.5f" % (self.bars_pos[side][bar][0], self.bars_pos[side][bar][1]) line += '\n' # Record finishing of processing of this frame self.toc("lifetime") return line def tic(self, name): assert name not in self.timings self.timings[name] = [monotonic_time(), None] def toc(self, name): assert name in self.timings assert self.timings[name][1] is None self.timings[name][1] = monotonic_time() timings_logger.debug("Frame %d, %s took %f msecs", self.frame_num, name, 1000.0 * (self.timings[name][1] - self.timings[name][0]))