示例#1
0
def _read_voc_annot(annot_path: str) -> Tuple[Sequence[int], 
                                              Sequence[tf.Tensor]]:
    # Reads a voc annotation and returns
    # a list of tuples containing the ground 
    # truth boxes and its respective label
    root = ET.parse(annot_path).getroot()
    image_size = (int(root.findtext('size/height')), # type: ignore[arg-type]
                  int(root.findtext('size/width')))  # type: ignore[arg-type]

    boxes = root.findall('object')
    bbs = []
    labels = []

    for b in boxes:
        bb = b.find('bndbox')
        bb = (int(bb.findtext('xmin')), # type: ignore
              int(bb.findtext('ymin')), # type: ignore
              int(bb.findtext('xmax')), # type: ignore
              int(bb.findtext('ymax'))) # type: ignore
        bbs.append(bb)
        labels.append(LABEL_2_IDX[b.findtext('name')]) # type: ignore

    bbs = tf.stack(bbs)
    bbs = bb_utils.normalize_bndboxes(bbs, image_size)

    return labels, bbs
示例#2
0
def _load_labelme_instance(
        images_base_path: Union[str, Path],
        annot_path: Union[str, Path],
        im_input_size: Tuple[int, int],
        class2idx: Mapping[str, int]) -> ObjectDetectionInstance:
    # Reads a labelme annotation and returns
    # a list of tuples containing the ground 
    # truth boxes and its respective label

    with Path(annot_path).open() as f:
        annot = json.load(f)

    bbs = []
    labels = []

    image_path = Path(images_base_path) / annot['imagePath']
    image = io_utils.load_image(str(image_path), 
                                im_input_size, 
                                normalize_image=True)
    
    w, h = annot['imageWidth'], annot['imageHeight']

    for shape in annot['shapes']:
        shape_type = shape['shape_type']
        if shape_type == 'rectangle':
            points = _load_bbox_from_rectangle(shape['points'])
        elif shape_type == 'polygon':
            points = _load_bbox_from_polygon(shape['points'])
        else:
            raise ValueError(
                f'Unexpected shape type: {shape_type} in file {annot_path}')
                
        label = shape['label']
        bbs.append(points)
        labels.append(class2idx[label])

    boxes = tf.stack(bbs)
    boxes = bb_utils.normalize_bndboxes(boxes, (h, w))

    return image, (tf.stack(labels), boxes)