How to get labels from object detection CoreML

Hi there,

I train an object detection model in CreateML and want to deploy in Python.

Then I can get coordination from the following codes but how to get the labels of prediction?

import coremltools as ct from PIL import Image path = '/sample.jpeg' example_image = Image.open(path).resize((960, 128)) model_path = '/sample.mlmodel' model = ct.models.MLModel(model_path) out_dict = model.predict({'imagePath': example_image,}) out_dict

Replies

model.predict as you describe will only give the numerical values of the bounding box coordinates and confidence values. There's no direct Python API in coremltools to get the labels, but you can load it from the model spec from the NMS layer like so:

spec = coremltools.utils.load_spec('/sample.mlmodel')
spec.pipeline.models[1].nonMaximumSuppression.stringClassLabels

This will return a StringVector with each of the labels in your model in the same order as you'll see in model.predict.