Exemplo n.º 1
0
def main():
    if len(sys.argv) < 3:
        print(
            'Usage: python run_mcs_human_input.py <mcs_unity_build_file> <mcs_config_json_file> <debug_files> <enable_noise>'
        )
        sys.exit()

    config_data, status = MCS.load_config_json_file(sys.argv[2])

    if status is not None:
        print(status)
        exit()

    debug = True
    if len(sys.argv) >= 4:
        debug = sys.argv[3].lower() == 'true'

    enable_noise = False
    if len(sys.argv) >= 5:
        enable_noise = sys.argv[4].lower() == 'true'

    controller = MCS.create_controller(sys.argv[1],
                                       debug=debug,
                                       enable_noise=enable_noise)

    config_file_path = sys.argv[2]
    config_file_name = config_file_path[config_file_path.rfind('/') + 1:]

    if 'name' not in config_data.keys():
        config_data['name'] = config_file_name[0:config_file_name.find('.')]

    run_scene(controller, config_data)
Exemplo n.º 2
0
def main():
    args = parse_args()

    unity_exe_path = find_unity_executable()
    if unity_exe_path is None:
        print("Unity executable not found in /mcs", file=sys.stderr)
        exit(1)

    scene_config, status = MCS.load_config_json_file(args.scene)
    if status is not None:
        print(status, file=sys.stderr)
        exit(1)

    controller = MCS.create_controller(unity_exe_path, debug=False)
    run_playroom(controller, scene_config)
Exemplo n.º 3
0
def run_scene(controller, file_name):
    config_data, status = MCS.load_config_json_file(file_name)

    if status is not None:
        print(status)
        return

    config_file_path = file_name
    config_file_name = config_file_path[config_file_path.rfind('/')+1:]

    if 'name' not in config_data.keys():
        config_data['name'] = config_file_name[0:config_file_name.find('.')]

    last_step = DEFAULT_STEP_COUNT
    if 'goal' in config_data.keys():
        if 'last_step' in config_data['goal'].keys():
            last_step = config_data['goal']['last_step']

    step_time_list = []

    output = controller.start_scene(config_data)

    for i in range(output.step_number + 1, last_step + 1):
        start = time.perf_counter()
        output = controller.step('Pass')
        end = time.perf_counter()
        step_time_list.append(end - start)

    output = controller.end_scene("", "")

    return step_time_list
Exemplo n.º 4
0
def main(argv):
    parser = argparse.ArgumentParser(
        description='Update scene descriptions with target visibility info')
    parser.add_argument('--app',
                        metavar='UNITY_MCS_APP',
                        required=True,
                        help='Path to Unity MCS application')
    parser.add_argument('--debug',
                        action='store_true',
                        default=False,
                        help='Enable debugging output')
    parser.add_argument('--backup',
                        action='store_true',
                        default=False,
                        help='Keep backup files')
    parser.add_argument('--loglevel',
                        choices=LOG_LEVELS,
                        help='set logging level')
    parser.add_argument('scene_files',
                        metavar='SCENE_FILE',
                        nargs='+',
                        help='scene file to update')

    args = parser.parse_args(argv[1:])

    if args.loglevel:
        logging.getLogger().setLevel(args.loglevel)
    elif args.debug:
        logging.getLogger().setLevel(logging.DEBUG)

    controller = MCS.create_controller(args.app, args.debug)

    for f in args.scene_files:
        update_file(controller, f, args.backup)
Exemplo n.º 5
0
def main():
    if len(sys.argv) < 3:
        print('Usage: python run_mcs_human_input.py <mcs_unity_build_file> <mcs_config_json_file> <debug_files>')
        sys.exit()

    config_data, status = MCS.load_config_json_file(sys.argv[2])

    if status is not None:
        print(status)
        exit()

    debug = 'terminal' if len(sys.argv) < 4 else True

    controller = MCS.create_controller(sys.argv[1], debug=debug)

    config_file_path = sys.argv[2]
    config_file_name = config_file_path[config_file_path.rfind('/'):]

    if 'name' not in config_data.keys():
        config_data['name'] = config_file_name[0:config_file_name.find('.')]

    run_scene(controller, config_data)
Exemplo n.º 6
0
def update_file(controller, filename, keep_backups):
    config_data, err_status = MCS.load_config_json_file(filename)
    if err_status is not None:
        logging.error(f'could not load scene file "{filename}": {err_status}')
        return False

    if 'name' not in config_data:
        base = os.path.basename(filename)
        config_data['name'] = base.rsplit('.', 1)[-1]

    output = controller.start_scene(config_data)

    metadata = config_data['goal']['metadata']
    target_ids = [
        metadata[key]['id'] for key in ('target', 'target_1', 'target_2')
        if key in metadata
    ]
    logging.debug(f'target_ids: {target_ids}')
    logging.debug(f'num objects = {len(output.object_list)}')

    targets_visible = False
    targets_not_visible = False
    for obj in output.object_list:
        if obj.uuid in target_ids:
            # If it's in the object list to begin with, it should be visible, but just to make sure...
            if obj.visible:
                targets_visible = True
                target_ids.remove(obj.uuid)
                logging.debug(f'target visible: {obj.uuid}')
            else:
                targets_not_visible = True
                logging.debug(f'target not visible: {obj.uuid}')
        if targets_visible and targets_not_visible:
            break
    if targets_visible:
        config_data['goal']['type_list'].append('target_starts_visible')
    if targets_not_visible or len(target_ids) > 0:
        config_data['goal']['type_list'].append('target_starts_invisible')

    backup_name = filename + '.backup'
    os.replace(filename, backup_name)
    try:
        write_file(filename, config_data)
    except Exception as e:
        logging.error(f'error writing file "{filename}": {e}')
        logging.error(f'backup file is at "{backup_name}"')
        return
    if not keep_backups:
        os.remove(backup_name)
Exemplo n.º 7
0
def main():
    if len(sys.argv) < 3:
        print('Usage: python mcs_run_scene_timer.py <mcs_unity_build_file> <scene_configuration_dir> <debug=False>')
        sys.exit()

    file_list = [os.path.join(sys.argv[2], file_name) for file_name in os.listdir(sys.argv[2]) if \
            os.path.isfile(os.path.join(sys.argv[2], file_name)) and os.path.splitext(file_name)[1] == '.json']
    file_list.sort()

    debug = sys.argv[3] if len(sys.argv) > 3 else False

    print(f'FOUND {len(file_list)} SCENE CONFIGURATION FILES... STARTING THE MCS UNITY APP...')
    controller = MCS.create_controller(sys.argv[1], debug=(True if debug == 'true' else debug))

    scene_time_list = []
    step_time_list_list = []
    step_time_avg_list = []
    step_time_len_list = []
    step_time_max_list = []
    step_time_min_list = []
    step_time_sum_list = []

    for i in range(0, len(file_list)):
        print('================================================================================')
        print(f'RUNNING FILE {(i + 1)}: {file_list[i]}')
        start = time.perf_counter()
        step_time_list = run_scene(controller, file_list[i])
        end = time.perf_counter()
        scene_time_list.append(end - start)
        step_time_list_list.append(step_time_list)
        step_time_avg_list.append(statistics.mean(step_time_list))
        step_time_len_list.append(len(step_time_list))
        step_time_max_list.append(max(step_time_list))
        step_time_min_list.append(min(step_time_list))
        step_time_sum_list.append(sum(step_time_list))

    print('================================================================================')
    print(f'RAN {len(file_list)} SCENES WITH {sum(step_time_len_list)} TOTAL STEPS IN {sum(scene_time_list):0.4f} SECONDS')
    print(f'Average single step took {statistics.mean(step_time_avg_list):0.4f} seconds')
    print(f'Longest single step took {max(step_time_max_list):0.4f} seconds')
    print(f'Shortest single step took {min(step_time_min_list):0.4f} seconds')
    print(f'Average single scene took {statistics.mean(scene_time_list):0.4f} seconds')
    print(f'Longest single scene took {max(scene_time_list):0.4f} seconds')
    print(f'Shortest single scene took {min(scene_time_list):0.4f} seconds')
Exemplo n.º 8
0
def main(args):
    if len(args) < 2:
        print(
            'Usage: python image_generator.py <unity_app_file_path> <output_folder=../images/>'
        )
        exit()

    controller = MCS.create_controller(args[1])

    scene_configuration_list = generate_scene_configuration_list(
        simplified_objects.OBJECT_LIST)

    output_folder = (args[2] if len(args) > 2 else '../images') + '/'

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    for scene_configuration in scene_configuration_list:
        object_config = scene_configuration['objects'][0]
        object_type = object_config['type']
        material_list = object_config[
            'materials'] if 'materials' in object_config else []

        print('type = ' + object_type + ', materials = ' +
              json.dumps(material_list))

        step_output = controller.start_scene(scene_configuration)
        object_screenshot = step_output.image_list[0]

        # Shrink and crop the object's screenshot to reduce its size.
        object_screenshot = object_screenshot.resize((300, 200))
        object_screenshot = object_screenshot.crop(
            ImageOps.invert(object_screenshot).getbbox())

        output_file_name = output_folder + generate_output_file_name(
            object_type, material_list)
        object_screenshot.save(fp=output_file_name + '.png')

        pixels = retrieve_image_pixel_list(object_screenshot)

        with open(output_file_name + '.txt', 'w') as output_text_file:
            output_text_file.write(
                json.dumps(PrettyJsonNoIndent(pixels), cls=PrettyJsonEncoder))
Exemplo n.º 9
0
def run_scene(file_name, action_list):
    config_data, status = MCS.load_config_json_file(file_name)

    if status is not None:
        print(status)
        return

    config_file_path = file_name
    config_file_name = config_file_path[config_file_path.rfind('/') + 1:]

    if 'name' not in config_data.keys():
        config_data['name'] = config_file_name[0:config_file_name.find('.')]

    output = controller.start_scene(config_data)

    output = controller.step('Pass')

    for action in action_list:
        output = controller.step(action)
Exemplo n.º 10
0
def run_scene(file_name):
    config_data, status = MCS.load_config_json_file(file_name)

    if status is not None:
        print(status)
        return

    config_file_path = file_name
    config_file_name = config_file_path[config_file_path.rfind('/')+1:]

    if 'name' not in config_data.keys():
        config_data['name'] = config_file_name[0:config_file_name.find('.')]

    last_step = 30
    if 'goal' in config_data.keys():
        if 'last_step' in config_data['goal'].keys():
            last_step = config_data['goal']['last_step']

    output = controller.start_scene(config_data)

    for i in range(1, last_step + 1):
        action = output.action_list[len(output.action_list) - 1]
        output = controller.step(action, **params)
Exemplo n.º 11
0
 def test_load_config_file_json(self):
     actual, status = MCS.load_config_json_file("test/test_scene.json")
     expected = {
         "ceilingMaterial": "Walls/WallDrywallWhite",
         "floorMaterial": "Fabrics/RUG4",
         "wallMaterial": "Walls/YellowDrywall",
         "objects": [{
             "id": "testBall",
             "type": "sphere",
             "materialFile": "Plastics/BlueRubber",
             "physics": True,
             "shows": [{
                 "stepBegin": 0,
                 "position": {
                     "x": 0,
                     "y": 0.5,
                     "z": 3
                 },
                 "scale": {
                     "x": 0.25,
                     "y": 0.25,
                     "z": 0.25
                 }   
             }],     
             "forces": [{
                 "stepBegin": 1,
                 "stepEnd": 1,
                 "vector": {
                     "x": 50,
                     "y": 0,
                     "z": 0
                 }
             }]  
         }]      
     }
     self.assertEqual(actual, expected)
     self.assertEqual(status, None)
Exemplo n.º 12
0
    output = controller.step('Pass')
    output = controller.step('RotateLook', rotation=0, horizon=-30)
    output = controller.step('ThrowObject',
                             objectId="apple_a",
                             force=1,
                             objectDirectionX=1,
                             objectDirectionY=0,
                             objectDirectionZ=2)
    output = controller.step('RotateLook', rotation=20, horizon=0)
    output = controller.step('Pass')
    output = controller.step('Pass')
    output = controller.step('Pass')


if __name__ == "__main__":
    config_data, status = MCS.load_config_json_file(sys.argv[2])

    if status is not None:
        print(status)
        exit()

    debug = 'terminal' if sys.argv[3] is None else True
    enable_noise = 'terminal' if sys.argv[4] is None else False

    controller = MCS.create_controller(sys.argv[1],
                                       debug=debug,
                                       enable_noise=enable_noise)

    config_file_path = sys.argv[2]
    config_file_name = config_file_path[config_file_path.rfind('/') + 1:]
Exemplo n.º 13
0
    # Move towards apple to pick it up
    output = controller.step('MoveLeft')
    output = controller.step('MoveAhead')

    # Should return OUT_OF_REACH
    output = controller.step('PullObject', objectId="apple_a", force=1)

    output = controller.step('RotateLook', rotation=0, horizon=45)

    # Should return SUCCESSFUL
    output = controller.step('PullObject', objectId="apple_a", force=1)
    output = controller.step('PushObject', objectId="apple_a", force=1)


if __name__ == "__main__":
    config_data, status = MCS.load_config_json_file(sys.argv[2])

    if status is not None:
        print(status)
        exit()

    controller = MCS.create_controller(sys.argv[1], debug=True)

    config_file_path = sys.argv[2]
    config_file_name = config_file_path[config_file_path.rfind('/') + 1:]

    # TODO: Read name directly from JSON in config file
    config_data['name'] = config_file_name[0:config_file_name.find('.')]

    run_scene(controller, config_data)
Exemplo n.º 14
0
    config_file_path = file_name
    config_file_name = config_file_path[config_file_path.rfind('/')+1:]

    if 'name' not in config_data.keys():
        config_data['name'] = config_file_name[0:config_file_name.find('.')]

    last_step = 30
    if 'goal' in config_data.keys():
        if 'last_step' in config_data['goal'].keys():
            last_step = config_data['goal']['last_step']

    output = controller.start_scene(config_data)

    for i in range(1, last_step + 1):
        action = output.action_list[len(output.action_list) - 1]
        output = controller.step(action, **params)

if __name__ == "__main__":
    controller = MCS.create_controller(sys.argv[1], debug=True)

    run_scene('./scenes/intphys_gravity_plausible_sample_1.json')
    run_scene('./scenes/intphys_gravity_implausible_sample_1.json')
    run_scene('./scenes/intphys_object_permanence_plausible_sample_1.json')
    run_scene('./scenes/intphys_object_permanence_implausible_sample_1.json')
    run_scene('./scenes/intphys_shape_constancy_plausible_sample_1.json')
    run_scene('./scenes/intphys_shape_constancy_implausible_sample_1.json')
    run_scene('./scenes/intphys_spatial_temporal_continuity_plausible_sample_1.json')
    run_scene('./scenes/intphys_spatial_temporal_continuity_implausible_sample_1.json')

Exemplo n.º 15
0
 def test_load_config_file_json_is_missing(self):
     actual, status = MCS.load_config_json_file("test/test_scene_missing.json")
     self.assertEqual(actual, {})
     self.assertEqual(status, "The given file 'test/test_scene_missing.json' cannot be found.")
Exemplo n.º 16
0
 def test_load_config_file_json_is_invalid(self):
     actual, status = MCS.load_config_json_file("test/test_scene_invalid.json")
     self.assertEqual(actual, {})
     self.assertEqual(status, "The given file 'test/test_scene_invalid.json' does not contain valid JSON.")