class LegoPatternDetector(object): def __init__(self): self.homography = self.compute_homography() self.pattern = SharedPattern() def compute_homography(self): src_points = json.load(open('rect.json')) dst_points = [[0, 0], [GRID_SIZE, 0], [GRID_SIZE, GRID_SIZE], [0, GRID_SIZE]] return cv2.findHomography(numpy.asarray(src_points, float), numpy.asarray(dst_points, float))[0] def process_image(self, img): img = cv2.warpPerspective(img, self.homography, (GRID_SIZE, GRID_SIZE)) self.update_notes(img) self.mute_tracks(img) return img def update_notes(self, img): for track in range(self.pattern.num_tracks): for step in range(self.pattern.num_steps): color = average_cell_color_hsv(img, track, step) if is_clear_color_hsv(color): self.pattern.clear_step(track, step) elif is_note_color_hsv(color): self.pattern.set_step(track, step) def mute_tracks(self, img): for track in range(self.pattern.num_tracks): color = average_cell_color_hsv(img, track + 8, 0) if is_clear_color_hsv(color): self.pattern.unmute(track) else: self.pattern.mute(track)
class LegoPatternDetector(object): def __init__(self): self.homography = self.compute_homography() self.pattern = SharedPattern() def compute_homography(self): src_points = json.load(open('rect.json')) dst_points = [ [0, 0], [GRID_SIZE, 0], [GRID_SIZE, GRID_SIZE], [0, GRID_SIZE]] return cv2.findHomography( numpy.asarray(src_points, float), numpy.asarray(dst_points, float))[0] def process_image(self, img): img = cv2.warpPerspective(img, self.homography, (GRID_SIZE, GRID_SIZE)) self.update_notes(img) self.mute_tracks(img) return img def update_notes(self, img): for track in range(self.pattern.num_tracks): for step in range(self.pattern.num_steps): color = average_cell_color_hsv(img, track, step) if is_clear_color_hsv(color): self.pattern.clear_step(track, step) elif is_note_color_hsv(color): self.pattern.set_step(track, step) def mute_tracks(self, img): for track in range(self.pattern.num_tracks): color = average_cell_color_hsv(img, track + 8, 0) if is_clear_color_hsv(color): self.pattern.unmute(track) else: self.pattern.mute(track)
def __init__(self): self.homography = self.compute_homography() self.pattern = SharedPattern()