-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathtest_instance_segmentation.py
More file actions
164 lines (127 loc) · 5.32 KB
/
test_instance_segmentation.py
File metadata and controls
164 lines (127 loc) · 5.32 KB
1
2
3
4
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
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
import unittest
import responses
from requests.exceptions import HTTPError
from roboflow.config import INSTANCE_SEGMENTATION_URL
from roboflow.models.instance_segmentation import InstanceSegmentationModel
from roboflow.util.prediction import PredictionGroup
MOCK_RESPONSE = {
"predictions": [
{
"x": 812.0,
"y": 362.9,
"width": 277,
"height": 206,
"class": "J",
"confidence": 0.598,
"points": [
{"x": 831.0, "y": 527.0},
{"x": 931.0, "y": 389.0},
{"x": 831.0, "y": 527.0},
],
},
{
"x": 363.8,
"y": 665.5,
"width": 707,
"height": 669,
"class": "K",
"confidence": 0.52,
"points": [
{"x": 131.0, "y": 999.0},
{"x": 269.0, "y": 666.0},
{"x": 131.0, "y": 999.0},
],
},
],
"image": {"width": 1333, "height": 1000},
}
class TestInstanceSegmentation(unittest.TestCase):
api_key = "my-api-key"
workspace = "roboflow"
dataset_id = "test-123"
version = "23"
api_url = f"https://serverless.roboflow.com/{dataset_id}/{version}"
_default_params = {
"api_key": api_key,
"confidence": "40",
}
def setUp(self):
super().setUp()
self.version_id = f"{self.workspace}/{self.dataset_id}/{self.version}"
def test_init_sets_attributes(self):
instance = InstanceSegmentationModel(self.api_key, self.version_id)
self.assertEqual(instance.id, self.version_id)
self.assertEqual(
instance.api_url,
f"{INSTANCE_SEGMENTATION_URL}/{self.dataset_id}/{self.version}",
)
@responses.activate
def test_predict_returns_prediction_group(self):
image_path = "tests/images/rabbit.JPG"
instance = InstanceSegmentationModel(self.api_key, self.version_id)
responses.add(responses.POST, self.api_url, json=MOCK_RESPONSE)
group = instance.predict(image_path)
self.assertIsInstance(group, PredictionGroup)
@responses.activate
def test_predict_with_local_image_request(self):
image_path = "tests/images/rabbit.JPG"
instance = InstanceSegmentationModel(self.api_key, self.version_id)
responses.add(responses.POST, self.api_url, json=MOCK_RESPONSE)
instance.predict(image_path)
request = responses.calls[0].request
self.assertEqual(request.method, "POST")
self.assertRegex(request.url, rf"^{self.api_url}")
self.assertDictEqual(request.params, self._default_params)
self.assertIsNotNone(request.body)
@responses.activate
def test_predict_with_hosted_image_request(self):
image_path = "https://example.com/raccoon.JPG"
expected_params = {
**self._default_params,
"image": image_path,
}
instance = InstanceSegmentationModel(self.api_key, self.version_id)
# Mock the library validating that the URL is valid before sending to the API
responses.add(responses.HEAD, image_path)
responses.add(responses.POST, self.api_url, json=MOCK_RESPONSE)
instance.predict(image_path)
request = responses.calls[1].request
self.assertEqual(request.method, "POST")
self.assertRegex(request.url, rf"^{self.api_url}")
self.assertDictEqual(request.params, expected_params)
self.assertIsNone(request.body)
@responses.activate
def test_predict_with_confidence_request(self):
confidence = "100"
image_path = "tests/images/rabbit.JPG"
expected_params = {**self._default_params, "confidence": confidence}
instance = InstanceSegmentationModel(self.api_key, self.version_id)
responses.add(responses.POST, self.api_url, json=MOCK_RESPONSE)
instance.predict(image_path, confidence=confidence)
request = responses.calls[0].request
self.assertEqual(request.method, "POST")
self.assertRegex(request.url, rf"^{self.api_url}")
self.assertDictEqual(request.params, expected_params)
self.assertIsNotNone(request.body)
@responses.activate
def test_predict_with_non_200_response_raises_http_error(self):
image_path = "tests/images/rabbit.JPG"
responses.add(responses.POST, self.api_url, status=403)
instance = InstanceSegmentationModel(self.api_key, self.version_id)
with self.assertRaises(HTTPError):
instance.predict(image_path)
@responses.activate
def test_predict_with_numpy_array(self):
# Create a simple numpy array image
import numpy as np
image_array = np.zeros((100, 100, 3), dtype=np.uint8) # Create a black image
image_array[30:70, 30:70] = 255 # Add a white square
instance = InstanceSegmentationModel(self.api_key, self.version_id)
responses.add(responses.POST, self.api_url, json=MOCK_RESPONSE)
group = instance.predict(image_array)
self.assertIsInstance(group, PredictionGroup)
request = responses.calls[0].request
self.assertEqual(request.method, "POST")
self.assertRegex(request.url, rf"^{self.api_url}")
self.assertDictEqual(request.params, self._default_params)
self.assertIsNotNone(request.body)