Example #1
0
    def get_table(self):
        size = self.original.shape
        table = Table(size[0], size[1])
        if self._is_ball_in_hand():
            table.set_ball_in_hand(True)
            table.set_hand(self._hand_pos)

        circles = self.get_circles()
        for c in circles[0,:]:
            ball = ball_mod.Ball(
                    x=int(np.round(c[0])), 
                    y=int(np.round(c[1])), 
                    r=BALL_RADIUS)
            if table.does_collide(ball):
                continue

            self._update_ball_detail(ball, table)
            if ball.type == ball_mod.TYPE_PHANTOM:
                continue

            # if ball-in-hand, then make sure cue ball is right under the hand
            # else it is a false positive match
            if ball.type == ball_mod.TYPE_CUE and table.is_ball_in_hand():
                x,y = table.get_hand()
                d = ((x-ball.x)**2 + (y-ball.y)**2)**0.5
                if d > BALL_RADIUS:
                    continue

            table.add_ball(ball)

        if self.debug:
            colored = self.original.copy()
            for b in table.get_balls():
                if b.type == ball_mod.TYPE_CUE or b.type == ball_mod.TYPE_BLACK:
                    color = (0,0,255) # red
                elif b.type == ball_mod.TYPE_STRIPE:
                    color = (0,255,0) # green
                elif b.type == ball_mod.TYPE_SOLID:
                    color = (0,255,255) # yellow

                cv2.circle(colored,(b.x, b.y), b.r,color,2)
                cv2.circle(colored,(b.x, b.y),2,(0,0,255),3)

            colored = cv2.cvtColor(colored, cv2.COLOR_BGR2RGB)
            plt.subplot(2,1,1)
            plt.imshow(colored)
            plt.draw()

            self.original = cv2.cvtColor(self.original, cv2.COLOR_BGR2RGB)
            plt.subplot(2,1,2)
            plt.imshow(self.original)
            plt.draw()
            plt.show(block=False)
            # plt.show(block=True)

        return table