コード例 #1
0
plt.savefig(os.path.join(directory, 'masks.png'))
# print(annotations)

count = 1
for annotation in annotations:
    viewpoint_xyz = get_viewpoint_from_id(viewpoints_xyz,
                                          annotation['viewpoint_id'])
    r, theta, phi = cart2sphere(viewpoint_xyz[0], viewpoint_xyz[1],
                                viewpoint_xyz[2])
    theta, phi = sphere2euler(theta, phi)
    inplane_rotation_angle = get_inplane_rotation_from_id(
        inplane_rotations, annotation['inplane_rotation_id'])
    xyz_rotation_angles = [phi, theta, inplane_rotation_angle]
    class_name = categories[annotation['category_id']]['name']
    print("*****{}*****".format(class_name))
    print("Recovered rotation : {}".format(xyz_rotation_angles))
    quat = annotation['quaternion_xyzw']
    print("Actual rotation : {}".format(
        RT_transform.quat2euler(get_wxyz_quaternion(quat))))
    # print("Actual rotation : {}".format(RT_transform.quat2euler(get_wxyz_quaternion(quat), 'rxyz')))
    rgb, depth = render_pose(directory, count, class_name,
                             fixed_transforms_dict, camera_intrinsics,
                             annotation['camera_pose'], xyz_rotation_angles,
                             annotation['location'],
                             annotation['quaternion_xyzw'])
    plt.subplot(3, 3, count + 1)
    plt.imshow(rgb)
    plt.subplot(3, 3, count + 2)
    plt.imshow(depth)
    count += 2
plt.show()
コード例 #2
0
def main():

    # object_settings_file = Path(os.path.join(IMAGE_DIR_LIST[0], "_object_settings.json"))
    # camera_settings_file = Path(os.path.join(IMAGE_DIR_LIST[0], "_camera_settings.json"))

    if object_settings_file.is_file():
        with open(object_settings_file) as file:
            object_settings_data = json.load(file)
            if SELECTED_OBJECTS is None:
                CLASSES = object_settings_data['exported_object_classes']
            else:
                CLASSES = SELECTED_OBJECTS
            CATEGORIES = [{
                'id': i,
                'name': CLASSES[i].replace('_16k', '').replace('_16K', ''),
                'supercategory': 'shape',
            } for i in range(0,len(CLASSES))]

            FIXED_TRANSFORMS = {}
            for i in range(0,len(object_settings_data['exported_object_classes'])):
                class_name = object_settings_data['exported_objects'][i]['class']
                transform = object_settings_data['exported_objects'][i]['fixed_model_transform']
                if class_name in CLASSES:
                    class_name = class_name.replace('_16k', '').replace('_16K', '')
                    FIXED_TRANSFORMS[class_name] = transform
                    
            # print(FIXED_TRANSFORMS)
            # SEGMENTATION_DATA =  object_settings_data['exported_objects']    
    else:
        raise Exception("Object settings file not found")

    if camera_settings_file.is_file():
        with open(camera_settings_file) as file:
            camera_settings_data = json.load(file)       
            CAMERA_INTRINSICS = camera_settings_data['camera_settings'][0]['intrinsic_settings']
    else:
        raise Exception("Camera settings file not found")

    VIEWPOINTS = [viewpoints_xyz[i].tolist() for i in range(0, len(viewpoints_xyz))]

    INPLANE_ROTATIONS = [inplane_rot_angles[i] for i in range(0, len(inplane_rot_angles))]

    coco_output = {
        "info": INFO,
        "licenses": LICENSES,
        "categories": CATEGORIES,
        "viewpoints" : VIEWPOINTS,
        "inplane_rotations" : INPLANE_ROTATIONS,
        "camera_intrinsic_settings": CAMERA_INTRINSICS,
        "fixed_transforms": FIXED_TRANSFORMS,
        "images": [],
        "annotations": []
    }

    image_global_id = 1
    segmentation_global_id = 1

    # filter for jpeg images
    for IMAGE_DIR_T in IMAGE_DIR_LIST:
        # for root, _, files in os.walk(IMAGE_DIR):
        for SCENE in SCENES:
            if IMAGE_DIR_T == "":
                IMAGE_DIR = os.path.join(ROOT_DIR, SCENE)
            else:
                IMAGE_DIR = os.path.join(ROOT_DIR, IMAGE_DIR_T, SCENE)

            all_dir_files = os.listdir(IMAGE_DIR)
            image_files = filter_for_jpeg(IMAGE_DIR, all_dir_files)
            # dir_name = os.path.basename(IMAGE_DIR)
            SEGMENTATION_DATA = get_segmentation_data_for_scene(IMAGE_DIR)
            print(SEGMENTATION_DATA)
            # go through each image
            for ii in trange(len(image_files)):
                image_filename = image_files[ii]
                if IMAGE_DIR_T == "":
                    image_out_filename =  os.path.join(SCENE, os.path.basename(image_filename))
                else:
                    image_out_filename =  os.path.join(IMAGE_DIR_T, SCENE, os.path.basename(image_filename))

                img_size = (960,540)
                image_info = pycococreatortools.create_image_info(
                    image_global_id, image_out_filename, img_size
                )
                # plt.figure()
                # skimage.io.imshow(skimage.io.imread(image_filename))
                # plt.show()
                # filter for associated png annotations
                # for root, _, files in os.walk(IMAGE_DIR):
                segmentation_image_files = filter_for_annotations(IMAGE_DIR, all_dir_files, image_filename)
                label_files = filter_for_labels(IMAGE_DIR, all_dir_files, image_filename)
                boxes = []
                labels = []
                segmentation_ids = []
                label_filename = label_files[0]
                # go through each associated json file containing objects data
                # for label_filename in label_files:
                # print("File %d - %s"% (image_global_id, label_filename))
                my_file = Path(label_filename)
                segmentation_image = skimage.io.imread(segmentation_image_files[0])
                # print("File %d - %s"% (image_global_id, segmentation_image_files[0]))
                
                if my_file.is_file():
                    with open(label_filename) as file:
                        label_data = json.load(file)
                        # all_objects_yaw_only = True
                        for i in range(0, len(label_data['objects'])):
                            class_name = label_data['objects'][i]['class']

                            if class_name not in SELECTED_OBJECTS:
                                continue
                            # print(class_name)
                            class_bounding_box = label_data['objects'][i]['bounding_box']
                            quat = label_data['objects'][i]['quaternion_xyzw']
                            
                            angles = RT_transform.quat2euler(get_wxyz_quaternion(quat))
                            # angles = RT_transform.quat2euler(get_wxyz_quaternion(quat), 'syxz')
                            # angles = apply_angle_symmetry(angles, SYMMETRY_INFO[class_name])
                            # This function gives angles with this convention of euler - https://en.wikipedia.org/wiki/Euler_angles#Signs_and_ranges (geometric definition)

                            # if np.isclose(angles[1], 0):
                            #     print("Test")
                            theta, phi = euler2sphere(angles[1], angles[0])
                            actual_angles = np.array([1, theta, phi])
                            xyz_coord = sphere2cart(1, theta, phi)
                            
                            viewpoint_id = find_viewpoint_id(viewpoints_xyz, xyz_coord)
                            r_xyz = get_viewpoint_from_id(viewpoints_xyz, viewpoint_id)
                            recovered_angles = np.array(cart2sphere(r_xyz[0], r_xyz[1], r_xyz[2]))

                            inplane_rotation_id = find_inplane_rotation_id(inplane_rot_angles, angles[2])
                            # inplate_rotation_angle = get_inplane_rotation_from_id(INPLANE_ROTATIONS, inplane_rotation_id)


                            if np.all(np.isclose(actual_angles, recovered_angles, atol=0.4)) == False:
                                print("Mismatch in : {}".format(label_filename))
                                print("sphere2cart angles : {}".format(actual_angles))
                                print("cart2sphere angles : {}".format(recovered_angles))
                            # elif np.all(np.isclose(actual_angles, recovered_angles, atol=0.4)) == True:
                            #     print("Match")

                            # print(inplate_rotation_angle)
                            class_label = [x['id'] for x in CATEGORIES if x['name'] in class_name][0]
                            segmentation_id = [x['segmentation_class_id'] for x in SEGMENTATION_DATA if x['class'] in class_name][0]

                            boxes.append(class_bounding_box['top_left'] + class_bounding_box['bottom_right'])
                            labels.append(class_label)     
                            segmentation_ids.append([x['segmentation_class_id'] for x in SEGMENTATION_DATA if x['class'] in class_name][0])

                            # Create binary masks from segmentation image for every object
                            # for segmentation_image_file in segmentation_image_files:
                            # segmentation_image = skimage.io.imread(segmentation_image_file)
                            binary_mask = np.copy(segmentation_image)
                            binary_mask[binary_mask != segmentation_id] = 0
                            binary_mask[binary_mask == segmentation_id] = 1
                            # skimage.io.imshow(binary_mask, cmap=plt.cm.gray)
                            # plt.show()
                            # TODO : check if its actually a crowd in case of multiple instances of one object type
                            # class_label = [x['class'] for x in SEGMENTATION_DATA if x['segmentation_class_id'] in segmentation_id][0]
                            category_info = {'id': class_label, 'is_crowd': 0}

                            
                            annotation_info = pycococreatortools.create_annotation_info(
                                segmentation_global_id, image_global_id, category_info, binary_mask,
                                img_size, tolerance=2)
                            
                            # print(annotation_info)

                            if annotation_info is not None:
                                annotation_info['viewpoint_id'] = int(viewpoint_id)
                                annotation_info['inplane_rotation_id'] = int(inplane_rotation_id)
                                annotation_info['camera_pose'] = label_data['camera_data']
                                annotation_info['location'] = label_data['objects'][i]['location']
                                annotation_info['quaternion_xyzw'] = quat
                                coco_output["annotations"].append(annotation_info)
                                coco_output["images"].append(image_info)
                            else:
                                tqdm.write("File %s doesn't have boxes or labels in json file" % image_filename)
                            segmentation_global_id = segmentation_global_id + 1
                else:
                    tqdm.write("File %s doesn't have a label file" % image_filename)
                        

                image_global_id = image_global_id + 1

            with open('{}/{}.json'.format(ROOT_DIR, OUTFILE_NAME), 'w') as output_json_file:
                json.dump(coco_output, output_json_file)