Skip to content

Classification

ClassificationModel

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

Source code in roboflow/models/classification.py
 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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class ClassificationModel:
    """
    Run inference on a classification model hosted on Roboflow or served through Roboflow Inference.
    """

    def __init__(
        self,
        api_key: str,
        id: str,
        name: str = None,
        version: int = None,
        local: bool = False,
        colors: dict = None,
        preprocessing: dict = None,
    ):
        """
        Create a ClassificationModel object through which you can run inference.

        Args:
            api_key (str): private roboflow api key
            id (str): the workspace/project id
            name (str): is the name of the project
            version (int): version number
            local (bool): whether the image is local or hosted
            colors (dict): colors to use for the image
            preprocessing (dict): preprocessing to use for the image

        Returns:
            ClassificationModel Object
        """
        # Instantiate different API URL parameters
        self.__api_key = api_key
        self.id = id
        self.name = name
        self.version = version
        self.base_url = "https://classify.roboflow.com/"

        if self.name is not None and version is not None:
            self.__generate_url()

        self.colors = {} if colors is None else colors
        self.preprocessing = {} if preprocessing is None else preprocessing

    def predict(self, image_path, hosted=False):
        """
        Run inference on an image.

        Args:
            image_path (str): path to the image you'd like to perform prediction on
            hosted (bool): whether the image you're providing is hosted on Roboflow

        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")
        """
        self.__generate_url()
        self.__exception_check(image_path_check=image_path)
        # If image is local image
        if not hosted:
            # Open Image in RGB Format
            image = Image.open(image_path).convert("RGB")
            # Create buffer
            buffered = io.BytesIO()
            image.save(buffered, quality=90, format="JPEG")
            img_dims = image.size
            # Base64 encode image
            img_str = base64.b64encode(buffered.getvalue())
            img_str = img_str.decode("ascii")
            # Post to API and return response
            resp = requests.post(
                self.api_url,
                data=img_str,
                headers={"Content-Type": "application/x-www-form-urlencoded"},
            )
        else:
            # Create API URL for hosted image (slightly different)
            self.api_url += "&image=" + urllib.parse.quote_plus(image_path)
            # POST to the API
            resp = requests.post(self.api_url)
            img_dims = {"width": "0", "height": "0"}

        if resp.status_code != 200:
            raise Exception(resp.text)

        return PredictionGroup.create_prediction_group(
            resp.json(),
            image_dims=img_dims,
            image_path=image_path,
            prediction_type=CLASSIFICATION_MODEL,
            colors=self.colors,
        )

    def load_model(self, name, version):
        """
        Load a model.

        Args:
            name (str): is the name of the model you'd like to load
            version (int): version number
        """
        # Load model based on user defined characteristics
        self.name = name
        self.version = version
        self.__generate_url()

    def __generate_url(self):
        """
        Generate a Roboflow API URL on which to run inference.

        Returns:
            url (str): the url on which to run inference
        """

        # Generates URL based on all parameters
        splitted = self.id.rsplit("/")
        without_workspace = splitted[1]

        self.api_url = "".join(
            [
                self.base_url + without_workspace + "/" + str(self.version),
                "?api_key=" + self.__api_key,
                "&name=YOUR_IMAGE.jpg",
            ]
        )

    def __exception_check(self, image_path_check=None):
        """
        Check to see if an image exists.

        Args:
            image_path_check (str): path to the image to check

        Raises:
            Exception: if image does not exist
        """
        # Checks if image exists
        if image_path_check is not None:
            if not os.path.exists(image_path_check) and not check_image_url(
                image_path_check
            ):
                raise Exception("Image does not exist at " + image_path_check + "!")

    def __str__(self):
        """
        String representation of classification object
        """
        json_value = {
            "name": self.name,
            "version": self.version,
            "base_url": self.base_url,
        }

        return json.dumps(json_value, indent=2)

__exception_check(image_path_check=None)

Check to see if an image exists.

Parameters:

Name Type Description Default
image_path_check str

path to the image to check

None

Raises:

Type Description
Exception

if image does not exist

Source code in roboflow/models/classification.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def __exception_check(self, image_path_check=None):
    """
    Check to see if an image exists.

    Args:
        image_path_check (str): path to the image to check

    Raises:
        Exception: if image does not exist
    """
    # Checks if image exists
    if image_path_check is not None:
        if not os.path.exists(image_path_check) and not check_image_url(
            image_path_check
        ):
            raise Exception("Image does not exist at " + image_path_check + "!")

__generate_url()

Generate a Roboflow API URL on which to run inference.

Returns:

Name Type Description
url str

the url on which to run inference

Source code in roboflow/models/classification.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def __generate_url(self):
    """
    Generate a Roboflow API URL on which to run inference.

    Returns:
        url (str): the url on which to run inference
    """

    # Generates URL based on all parameters
    splitted = self.id.rsplit("/")
    without_workspace = splitted[1]

    self.api_url = "".join(
        [
            self.base_url + without_workspace + "/" + str(self.version),
            "?api_key=" + self.__api_key,
            "&name=YOUR_IMAGE.jpg",
        ]
    )

__init__(api_key, id, name=None, version=None, local=False, colors=None, preprocessing=None)

Create a ClassificationModel object through which you can run inference.

Parameters:

Name Type Description Default
api_key str

private roboflow api key

required
id str

the workspace/project id

required
name str

is the name of the project

None
version int

version number

None
local bool

whether the image is local or hosted

False
colors dict

colors to use for the image

None
preprocessing dict

preprocessing to use for the image

None

Returns:

Type Description

ClassificationModel Object

Source code in roboflow/models/classification.py
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
def __init__(
    self,
    api_key: str,
    id: str,
    name: str = None,
    version: int = None,
    local: bool = False,
    colors: dict = None,
    preprocessing: dict = None,
):
    """
    Create a ClassificationModel object through which you can run inference.

    Args:
        api_key (str): private roboflow api key
        id (str): the workspace/project id
        name (str): is the name of the project
        version (int): version number
        local (bool): whether the image is local or hosted
        colors (dict): colors to use for the image
        preprocessing (dict): preprocessing to use for the image

    Returns:
        ClassificationModel Object
    """
    # Instantiate different API URL parameters
    self.__api_key = api_key
    self.id = id
    self.name = name
    self.version = version
    self.base_url = "https://classify.roboflow.com/"

    if self.name is not None and version is not None:
        self.__generate_url()

    self.colors = {} if colors is None else colors
    self.preprocessing = {} if preprocessing is None else preprocessing

__str__()

String representation of classification object

Source code in roboflow/models/classification.py
167
168
169
170
171
172
173
174
175
176
177
def __str__(self):
    """
    String representation of classification object
    """
    json_value = {
        "name": self.name,
        "version": self.version,
        "base_url": self.base_url,
    }

    return json.dumps(json_value, indent=2)

load_model(name, version)

Load a model.

Parameters:

Name Type Description Default
name str

is the name of the model you'd like to load

required
version int

version number

required
Source code in roboflow/models/classification.py
117
118
119
120
121
122
123
124
125
126
127
128
def load_model(self, name, version):
    """
    Load a model.

    Args:
        name (str): is the name of the model you'd like to load
        version (int): version number
    """
    # Load model based on user defined characteristics
    self.name = name
    self.version = version
    self.__generate_url()

predict(image_path, hosted=False)

Run inference on an image.

Parameters:

Name Type Description Default
image_path str

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

required
hosted bool

whether the image you're providing is hosted on Roboflow

False

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/classification.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def predict(self, image_path, hosted=False):
    """
    Run inference on an image.

    Args:
        image_path (str): path to the image you'd like to perform prediction on
        hosted (bool): whether the image you're providing is hosted on Roboflow

    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")
    """
    self.__generate_url()
    self.__exception_check(image_path_check=image_path)
    # If image is local image
    if not hosted:
        # Open Image in RGB Format
        image = Image.open(image_path).convert("RGB")
        # Create buffer
        buffered = io.BytesIO()
        image.save(buffered, quality=90, format="JPEG")
        img_dims = image.size
        # Base64 encode image
        img_str = base64.b64encode(buffered.getvalue())
        img_str = img_str.decode("ascii")
        # Post to API and return response
        resp = requests.post(
            self.api_url,
            data=img_str,
            headers={"Content-Type": "application/x-www-form-urlencoded"},
        )
    else:
        # Create API URL for hosted image (slightly different)
        self.api_url += "&image=" + urllib.parse.quote_plus(image_path)
        # POST to the API
        resp = requests.post(self.api_url)
        img_dims = {"width": "0", "height": "0"}

    if resp.status_code != 200:
        raise Exception(resp.text)

    return PredictionGroup.create_prediction_group(
        resp.json(),
        image_dims=img_dims,
        image_path=image_path,
        prediction_type=CLASSIFICATION_MODEL,
        colors=self.colors,
    )