def build_output_manifest_line()

in broadcast-monitoring/scripts/generate-logo-images.py [0:0]


def build_output_manifest_line(s3_bucket, s3_key, img, bounding_boxes, label_map):
    '''Build a groundtruth manifest line from an image and its bounding boxes

    :param s3_bucket: s3 bucket where source media lives
    :param s3_key: key to image object
    :param img: image
    :param bounding_boxes: bounding boxes for labels on the image
    :param label_map: map of index values for each label

    :return a dict representation of an output manifest line
    '''
    img_h, img_w, *_ = img.shape
    # create a reverse map of the class map to convert map[int]str -> map[str]int
    # to allow lookups of annotation value from label name
    r_label_map = {v: k for k, v in label_map.items()}

    def get_bb_info(bb):
        return {
            "pos": (bb.x1_int, bb.y1_int),
            "size": (bb.x2_int - bb.x1_int, bb.y2_int - bb.y1_int),
            "label": r_label_map.get(bb.label)
        }

    manifest_line = {
        'source-ref': f's3://{os.path.join(s3_bucket, s3_key)}',
        'bounding-box': {
            'annotations': [build_annotation(**get_bb_info(bb)) for bb in bounding_boxes],
            'image_size': [{
                "width": img_w,
                "height": img_h,
                "depth": 3
            }]
        },
        'bounding-box-metadata': {
            'class-map': {str(r_label_map[bb.label]): bb.label
                          for bb in bounding_boxes},
            'human-annotated': "yes",
            'objects': [{
                'confidence': 1
            }] * len(bounding_boxes),
            'creation-date': f'{datetime.utcnow().isoformat()[:-3]}Z',
            'type': 'groundtruth/object-detection'
        }
    }
    return manifest_line