Hi there,
I am trying to build an annotation recipe with image.manual but with a mask-Rcnn model in the loop to identify bounding boxes and display to the annotator so that he/she can verify the model's prediction. This is my recipe code.
import prodigy
from prodigy.components.loaders import Images
from prodigy.util import split_string
from typing import List, Optional
from model_functions import load_models, getBBwLabels, b64_uri_to_array
# Recipe decorator with argument annotations: (description, argument type,
# shortcut, type / converter function called on value before it's passed to
# the function). Descriptions are also shown when typing --help.
@prodigy.recipe(
"image_mod.manual",
dataset=("The dataset to use", "positional", None, str),
source=("Path to a directory of images", "positional", None, str),
label=("One or more comma-separated labels", "option", "l", split_string),
exclude=("Names of datasets to exclude", "option", "e", split_string),
darken=("Darken image to make boxes stand out more", "flag", "D", bool),
)
def image_mod_manual(
dataset: str,
source: str,
label: Optional[List[str]] = None,
exclude: Optional[List[str]] = None,
darken: bool = False,
):
"""
Manually annotate images by drawing rectangular bounding boxes or polygon
shapes on the image.
"""
model_a, model_b, model_c = load_models()
# Load a stream of images from a directory and return a generator that
# yields a dictionary for each example in the data. All images are
# converted to base64-encoded data URIs.
def get_stream():
stream = Images(source)
for eg in stream:
image = b64_uri_to_array(eg["image"])
bbs, labels = getBBwLabels(image, model_a, model_b, model_c)
for i in range(len(bbs)):
eg['spans'][i]['label'] = labels[i]
eg["spans"][i]['points'] = bbs[i]
yield eg
return {
"view_id": "image_manual", # Annotation interface to use
"dataset": dataset, # Name of dataset to save annotations
"stream": get_stream(), # Incoming stream of examples
"exclude": exclude, # List of dataset names to exclude
"config": { # Additional config settings, mostly for app UI
"label": ", ".join(label) if label is not None else "all",
"labels": label, # Selectable label options,
"darken_image": 0.3 if darken else 0,
},
}
There are about seven bounding boxes with labels that the model outputs and this has to be tucked into the eg['spans'][i]['points'] and eg['spans'][i]['labels'] of the stream. However its giving me a
KeyError: 'spans'
Please let me know how to resolve this issue
Thank you
DU