コード例 #1
0
ファイル: game_interface.py プロジェクト: laurierloi/iapr2018
def main():

    #wvs.stop()
    #wvs.release()
    #TODO detect the count of forms

    # 1) Create a webcam video stream
    #   Note: src=0 links to the USB Webcam of the computers provided for the project
    webcam = True
    if webcam:
        #wvs =

        images = get_images()
        im_names = ["Analysis image"]
    else:
        ic = skimage.io.imread_collection("data/test1_1.png")
        images = skimage.io.concatenate_images(ic)
        xmin = 85
        xmax = 570
        ymin = 60
        ymax = 425
        images = np.copy(images[:, ymin:ymax, xmin:xmax, :])
        im_names = ["Test image"]

    # 2) first image analysis
    print("Doing first image analysis")
    global imageAnalysis
    imageAnalysis = ImageAnalysis(images,
                                  im_names,
                                  result_dir=RESULT_DIR,
                                  print_local=print_local,
                                  save_to_file=SAVE_TO_FILE,
                                  savefig=SAVE_FIG)

    imageAnalysis.image_analysis()
    figures_info = imageAnalysis.figures_info[
        0]  # (center, number, matching_pair, bbox)
    circle_info = imageAnalysis.circle_info[0]  # (index, bbox_center)
    arrow_info = imageAnalysis.arrow_info[
        0]  # (bbox_center, orientation, direction_x)

    # 3) get target list and store it
    print("Getting target list")
    target_list = get_target_list(figures_info, circle_info)

    if len(target_list) < NUMBER_OF_FIG:
        print("WARNING: not enough figures have been detected")

    print("Saving gameplan")
    plt.imshow(images[0])
    for index in range(len(target_list) - 1):
        x0 = target_list[index][1][0]
        x1 = target_list[index + 1][1][0]
        y0 = target_list[index][1][1]
        y1 = target_list[index + 1][1][1]

        plt.plot([y0, y1], [x0, x1], 'ro-')
    plt.savefig("gameplan.png")
    plt.close()

    print("Calibrating robot")
    # 4) calibrate robot
    global robotController
    robotController = RobotController()
    robot_info_cal = get_robot_info()
    robotController.calibration()
    robot_info = get_robot_info()
    robotController.calcFactorCalibr(
        robot_info_cal[0], robot_info_cal[1], robot_info_cal[2], robot_info[0],
        robot_info[1], robot_info[2])  # (x_i, y_i, theta_i, x_f, y_f, theta_f)

    # Target are stored as : (key, center, is_number)

    print("Executing task")
    # 5) Execute task
    for index, target in enumerate(target_list):
        target_point = target[1]
        print("Target:", target_point)
        x_t = target_point[1]
        y_t = target_point[0]
        is_number = target[2]
        if target_point[0] is "circle":
            finish = True
        else:
            finish = False
        print("New target: ", target_point)
        print("Finish")

        on_shape = False
        while not on_shape:
            robotController.GoTo(robot_info[0], robot_info[1], robot_info[2],
                                 x_t, y_t)
            robot_info = get_robot_info()
            if CORR_FACTORS:
                robotController.corrFactors(robot_info[0], robot_info[1],
                                            robot_info[2])
            on_shape = robotController.checkOnTheShape(robot_info[0],
                                                       robot_info[1],
                                                       is_number,
                                                       finish=finish,
                                                       dist_min=DIST_MIN)