Skip to content

Instance Segmentation

InstanceSegmentationModel

Bases: InferenceModel

Run inference on a instance segmentation model hosted on Roboflow or served through Roboflow Inference.

Source code in roboflow/models/instance_segmentation.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class InstanceSegmentationModel(InferenceModel):
    """
    Run inference on a instance segmentation model hosted on Roboflow or served through Roboflow Inference.
    """

    def __init__(
        self,
        api_key: str,
        version_id: str,
        colors: dict = None,
        preprocessing: dict = None,
        local: bool = None,
    ):
        """
        Create a InstanceSegmentationModel object through which you can run inference.

        Args:
            api_key (str): private roboflow api key
            version_id (str): the workspace/project id
            colors (dict): colors to use for the image
            preprocessing (dict): preprocessing to use for the image
            local (bool): whether the image is local or hosted
        """
        super(InstanceSegmentationModel, self).__init__(api_key, version_id)
        if local is None:
            self.api_url = (
                f"{INSTANCE_SEGMENTATION_URL}/{self.dataset_id}/{self.version}"
            )
        else:
            self.api_url = f"{local}/{self.dataset_id}/{self.version}"
        self.colors = {} if colors is None else colors
        self.preprocessing = {} if preprocessing is None else preprocessing

    def predict(self, image_path, confidence=40):
        """
        Infers detections based on image from a specified model and image path.

        Args:
            image_path (str): path to the image you'd like to perform prediction on
            confidence (int): confidence threshold for predictions, on a scale from 0-100

        Returns:
            PredictionGroup Object

        Example:
            >>> import roboflow

            >>> rf = roboflow.Roboflow(api_key="")

            >>> project = rf.workspace().project("PROJECT_ID")

            >>> model = project.version("1").model

            >>> prediction = model.predict("YOUR_IMAGE.jpg")
        """
        return super(InstanceSegmentationModel, self).predict(
            image_path,
            confidence=confidence,
            prediction_type=INSTANCE_SEGMENTATION_MODEL,
        )

    def __str__(self):
        return f"<{type(self).__name__} id={self.id}, api_url={self.api_url}>"

__init__(api_key, version_id, colors=None, preprocessing=None, local=None)

Create a InstanceSegmentationModel object through which you can run inference.

Parameters:

Name Type Description Default
api_key str

private roboflow api key

required
version_id str

the workspace/project id

required
colors dict

colors to use for the image

None
preprocessing dict

preprocessing to use for the image

None
local bool

whether the image is local or hosted

None
Source code in roboflow/models/instance_segmentation.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def __init__(
    self,
    api_key: str,
    version_id: str,
    colors: dict = None,
    preprocessing: dict = None,
    local: bool = None,
):
    """
    Create a InstanceSegmentationModel object through which you can run inference.

    Args:
        api_key (str): private roboflow api key
        version_id (str): the workspace/project id
        colors (dict): colors to use for the image
        preprocessing (dict): preprocessing to use for the image
        local (bool): whether the image is local or hosted
    """
    super(InstanceSegmentationModel, self).__init__(api_key, version_id)
    if local is None:
        self.api_url = (
            f"{INSTANCE_SEGMENTATION_URL}/{self.dataset_id}/{self.version}"
        )
    else:
        self.api_url = f"{local}/{self.dataset_id}/{self.version}"
    self.colors = {} if colors is None else colors
    self.preprocessing = {} if preprocessing is None else preprocessing

predict(image_path, confidence=40)

Infers detections based on image from a specified model and image path.

Parameters:

Name Type Description Default
image_path str

path to the image you'd like to perform prediction on

required
confidence int

confidence threshold for predictions, on a scale from 0-100

40

Returns:

Type Description

PredictionGroup Object

Example

import roboflow

rf = roboflow.Roboflow(api_key="")

project = rf.workspace().project("PROJECT_ID")

model = project.version("1").model

prediction = model.predict("YOUR_IMAGE.jpg")

Source code in roboflow/models/instance_segmentation.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def predict(self, image_path, confidence=40):
    """
    Infers detections based on image from a specified model and image path.

    Args:
        image_path (str): path to the image you'd like to perform prediction on
        confidence (int): confidence threshold for predictions, on a scale from 0-100

    Returns:
        PredictionGroup Object

    Example:
        >>> import roboflow

        >>> rf = roboflow.Roboflow(api_key="")

        >>> project = rf.workspace().project("PROJECT_ID")

        >>> model = project.version("1").model

        >>> prediction = model.predict("YOUR_IMAGE.jpg")
    """
    return super(InstanceSegmentationModel, self).predict(
        image_path,
        confidence=confidence,
        prediction_type=INSTANCE_SEGMENTATION_MODEL,
    )