class NAOVisionTestCase(unittest.TestCase):
    def setUp(self):
        ref_mapping = DefaultConnect4Image().generate2DReference()
        self.img = cv2.imread("state_detection_test.png")
        self.strategy = NAOVision(ref_mapping, lambda: self.img)
        self.strategy.player_id = disc.GREEN

    def test_analyse(self):
        test_board = np.array(
            [
                [disc.EMPTY, disc.EMPTY, disc.RED, disc.EMPTY, disc.EMPTY, disc.EMPTY, disc.EMPTY],
                [disc.EMPTY, disc.EMPTY, disc.RED, disc.GREEN, disc.EMPTY, disc.EMPTY, disc.EMPTY],
                [disc.EMPTY, disc.EMPTY, disc.RED, disc.GREEN, disc.EMPTY, disc.EMPTY, disc.EMPTY],
                [disc.EMPTY, disc.EMPTY, disc.RED, disc.GREEN, disc.EMPTY, disc.EMPTY, disc.EMPTY],
                [disc.EMPTY, disc.EMPTY, disc.RED, disc.GREEN, disc.EMPTY, disc.EMPTY, disc.RED],
                [disc.GREEN, disc.RED, disc.RED, disc.GREEN, disc.RED, disc.RED, disc.GREEN],
            ]
        )
        test_state = C4State()
        test_state.board = test_board
        chosen_action = self.strategy.chooseNextAction(test_state)
        self.assertTrue(chosen_action == 3)

    def test_debug(self):
        test_state = C4State()
        board = self.strategy.analyseFullImage(test_state, img=self.img, debug=True)
        self.assertTrue(
            (
                board
                == np.array(
                    [
                        [-1, -1, 0, 1, -1, -1, -1],
                        [-1, -1, 0, 1, -1, -1, -1],
                        [-1, -1, 0, 1, -1, -1, -1],
                        [-1, -1, 0, 1, -1, -1, -1],
                        [-1, -1, 0, 1, -1, -1, 0],
                        [1, 0, 0, 1, -1, 0, 1],
                    ]
                )
            ).all()
        )
Beispiel #2
0
def state(args):
    global nao_motion
    print
    print "-1 = Empty, 0 = Red, 1 = Green"
    print
    data.IP = args['--ip']
    data.PORT = int(args['--port'])
    next_img_func = get_nao_image
    if args['--no-robot']:
        next_img_func = get_webcam_image
    myc4 = Connect4Handler(next_img_func, cam_no=int(args['--cam-no']))
    dist = float(args['--dist'])
    sloped = args['--sloped']
    tries = int(args['--min-detections'])

    ref_mapping = DefaultConnect4Image().generate2DReference()
    strategy = NAOVision(ref_mapping, lambda: perspective)

    strategy.player_id = disc.GREEN
    test_state = C4State()
    nao_motion = MotionController()
    nao_motion.lookAtGameBoard(dist)
    while True:
        try:
            myc4.detectFrontHoles(dist, sloped, tries=tries)
            perspective = myc4.front_hole_detector.getPerspective()
            if not args['--no-image']:
                cv2.imshow("Perspective", perspective)
            game_state = strategy.analyseFullImage(test_state, img=perspective, debug=True)
            print game_state
            print
        except BaseException:
            pass
        if cv2.waitKey(1) == 27:
            print "Esc pressed : exit"
            close_camera()
            break
        sleep(0.5)