|
| 1 | +import uuid |
| 2 | +import pytest |
| 3 | + |
| 4 | +from labelbox.schema.labeling_frontend import LabelingFrontend |
| 5 | + |
| 6 | +IMG_URL = "https://picsum.photos/200/300" |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def ontology(): |
| 11 | + bbox_tool = { |
| 12 | + 'required': |
| 13 | + False, |
| 14 | + 'name': |
| 15 | + 'bbox', |
| 16 | + 'tool': |
| 17 | + 'rectangle', |
| 18 | + 'color': |
| 19 | + '#a23030', |
| 20 | + 'classifications': [{ |
| 21 | + 'required': False, |
| 22 | + 'instructions': 'nested', |
| 23 | + 'name': 'nested', |
| 24 | + 'type': 'radio', |
| 25 | + 'options': [{ |
| 26 | + 'label': 'radio_option_1', |
| 27 | + 'value': 'radio_value_1' |
| 28 | + }] |
| 29 | + }] |
| 30 | + } |
| 31 | + polygon_tool = { |
| 32 | + 'required': False, |
| 33 | + 'name': 'polygon', |
| 34 | + 'tool': 'polygon', |
| 35 | + 'color': '#FF34FF', |
| 36 | + 'classifications': [] |
| 37 | + } |
| 38 | + polyline_tool = { |
| 39 | + 'required': False, |
| 40 | + 'name': 'polyline', |
| 41 | + 'tool': 'line', |
| 42 | + 'color': '#FF4A46', |
| 43 | + 'classifications': [] |
| 44 | + } |
| 45 | + point_tool = { |
| 46 | + 'required': False, |
| 47 | + 'name': 'point--', |
| 48 | + 'tool': 'point', |
| 49 | + 'color': '#008941', |
| 50 | + 'classifications': [] |
| 51 | + } |
| 52 | + entity_tool = { |
| 53 | + 'required': False, |
| 54 | + 'name': 'entity--', |
| 55 | + 'tool': 'named-entity', |
| 56 | + 'color': '#006FA6', |
| 57 | + 'classifications': [] |
| 58 | + } |
| 59 | + segmentation_tool = { |
| 60 | + 'required': False, |
| 61 | + 'name': 'segmentation--', |
| 62 | + 'tool': 'superpixel', |
| 63 | + 'color': '#A30059', |
| 64 | + 'classifications': [] |
| 65 | + } |
| 66 | + checklist = { |
| 67 | + 'required': |
| 68 | + False, |
| 69 | + 'instructions': |
| 70 | + 'checklist', |
| 71 | + 'name': |
| 72 | + 'checklist', |
| 73 | + 'type': |
| 74 | + 'checklist', |
| 75 | + 'options': [{ |
| 76 | + 'label': 'option1', |
| 77 | + 'value': 'option1' |
| 78 | + }, { |
| 79 | + 'label': 'option2', |
| 80 | + 'value': 'option2' |
| 81 | + }, { |
| 82 | + 'label': 'optionN', |
| 83 | + 'value': 'optionn' |
| 84 | + }] |
| 85 | + } |
| 86 | + free_form_text = { |
| 87 | + 'required': False, |
| 88 | + 'instructions': 'text', |
| 89 | + 'name': 'text', |
| 90 | + 'type': 'text', |
| 91 | + 'options': [] |
| 92 | + } |
| 93 | + |
| 94 | + tools = [ |
| 95 | + bbox_tool, polygon_tool, polyline_tool, point_tool, entity_tool, |
| 96 | + segmentation_tool |
| 97 | + ] |
| 98 | + classifications = [checklist, free_form_text] |
| 99 | + return {"tools": tools, "classifications": classifications} |
| 100 | + |
| 101 | + |
| 102 | +@pytest.fixture |
| 103 | +def configured_project(client, project, ontology, dataset): |
| 104 | + editor = list( |
| 105 | + client.get_labeling_frontends( |
| 106 | + where=LabelingFrontend.name == "editor"))[0] |
| 107 | + project.setup(editor, ontology) |
| 108 | + for _ in range(len(ontology['tools']) + len(ontology['classifications'])): |
| 109 | + dataset.create_data_row(row_data=IMG_URL) |
| 110 | + project.datasets.connect(dataset) |
| 111 | + yield project |
| 112 | + |
| 113 | + |
| 114 | +@pytest.fixture |
| 115 | +def prediction_id_mapping(configured_project): |
| 116 | + #Maps tool types to feature schema ids |
| 117 | + ontology = configured_project.ontology().normalized |
| 118 | + inferences = [] |
| 119 | + datarows = [d for d in list(configured_project.datasets())[0].data_rows()] |
| 120 | + result = {} |
| 121 | + |
| 122 | + for idx, tool in enumerate(ontology['tools'] + ontology['classifications']): |
| 123 | + if 'tool' in tool: |
| 124 | + tool_type = tool['tool'] |
| 125 | + else: |
| 126 | + tool_type = tool['type'] |
| 127 | + result[tool_type] = { |
| 128 | + "uuid": str(uuid.uuid4()), |
| 129 | + "schemaId": tool['featureSchemaId'], |
| 130 | + "dataRow": { |
| 131 | + "id": datarows[idx].uid, |
| 132 | + }, |
| 133 | + 'tool': tool |
| 134 | + } |
| 135 | + return result |
| 136 | + |
| 137 | + |
| 138 | +@pytest.fixture |
| 139 | +def polygon_inference(prediction_id_mapping): |
| 140 | + polygon = prediction_id_mapping['polygon'].copy() |
| 141 | + polygon.update({ |
| 142 | + "polygon": [{ |
| 143 | + "x": 147.692, |
| 144 | + "y": 118.154 |
| 145 | + }, { |
| 146 | + "x": 142.769, |
| 147 | + "y": 404.923 |
| 148 | + }, { |
| 149 | + "x": 57.846, |
| 150 | + "y": 318.769 |
| 151 | + }, { |
| 152 | + "x": 28.308, |
| 153 | + "y": 169.846 |
| 154 | + }] |
| 155 | + }) |
| 156 | + del polygon['tool'] |
| 157 | + return polygon |
| 158 | + |
| 159 | + |
| 160 | +@pytest.fixture |
| 161 | +def rectangle_inference(prediction_id_mapping): |
| 162 | + rectangle = prediction_id_mapping['rectangle'].copy() |
| 163 | + rectangle.update({ |
| 164 | + "bbox": { |
| 165 | + "top": 48, |
| 166 | + "left": 58, |
| 167 | + "height": 865, |
| 168 | + "width": 1512 |
| 169 | + }, |
| 170 | + 'classifications': [{ |
| 171 | + "schemaId": |
| 172 | + rectangle['tool']['classifications'][0]['featureSchemaId'], |
| 173 | + "answer": { |
| 174 | + "schemaId": |
| 175 | + rectangle['tool']['classifications'][0]['options'][0] |
| 176 | + ['featureSchemaId'] |
| 177 | + } |
| 178 | + }] |
| 179 | + }) |
| 180 | + del rectangle['tool'] |
| 181 | + return rectangle |
| 182 | + |
| 183 | + |
| 184 | +@pytest.fixture |
| 185 | +def line_inference(prediction_id_mapping): |
| 186 | + line = prediction_id_mapping['line'].copy() |
| 187 | + line.update( |
| 188 | + {"line": [{ |
| 189 | + "x": 147.692, |
| 190 | + "y": 118.154 |
| 191 | + }, { |
| 192 | + "x": 150.692, |
| 193 | + "y": 160.154 |
| 194 | + }]}) |
| 195 | + del line['tool'] |
| 196 | + return line |
| 197 | + |
| 198 | + |
| 199 | +@pytest.fixture |
| 200 | +def point_inference(prediction_id_mapping): |
| 201 | + point = prediction_id_mapping['point'].copy() |
| 202 | + point.update({"point": {"x": 147.692, "y": 118.154}}) |
| 203 | + del point['tool'] |
| 204 | + return point |
| 205 | + |
| 206 | + |
| 207 | +@pytest.fixture |
| 208 | +def entity_inference(prediction_id_mapping): |
| 209 | + entity = prediction_id_mapping['named-entity'].copy() |
| 210 | + entity.update({"location": {"start": 67, "end": 128}}) |
| 211 | + del entity['tool'] |
| 212 | + return entity |
| 213 | + |
| 214 | + |
| 215 | +@pytest.fixture |
| 216 | +def segmentation_inference(prediction_id_mapping): |
| 217 | + segmentation = prediction_id_mapping['superpixel'].copy() |
| 218 | + segmentation.update( |
| 219 | + {'mask': { |
| 220 | + 'instanceURI': "sampleuri", |
| 221 | + 'colorRGB': [0, 0, 0] |
| 222 | + }}) |
| 223 | + del segmentation['tool'] |
| 224 | + return segmentation |
| 225 | + |
| 226 | + |
| 227 | +@pytest.fixture |
| 228 | +def checklist_inference(prediction_id_mapping): |
| 229 | + checklist = prediction_id_mapping['checklist'].copy() |
| 230 | + checklist.update({ |
| 231 | + 'answers': [{ |
| 232 | + 'schemaId': checklist['tool']['options'][0]['featureSchemaId'] |
| 233 | + }] |
| 234 | + }) |
| 235 | + del checklist['tool'] |
| 236 | + return checklist |
| 237 | + |
| 238 | + |
| 239 | +@pytest.fixture |
| 240 | +def text_inference(prediction_id_mapping): |
| 241 | + text = prediction_id_mapping['text'].copy() |
| 242 | + text.update({'answer': "free form text..."}) |
| 243 | + del text['tool'] |
| 244 | + return text |
| 245 | + |
| 246 | + |
| 247 | +@pytest.fixture |
| 248 | +def video_checklist_inference(prediction_id_mapping): |
| 249 | + checklist = prediction_id_mapping['checklist'].copy() |
| 250 | + checklist.update({ |
| 251 | + 'answers': [{ |
| 252 | + 'schemaId': checklist['tool']['options'][0]['featureSchemaId'] |
| 253 | + }] |
| 254 | + }) |
| 255 | + |
| 256 | + checklist.update( |
| 257 | + {"frames": [{ |
| 258 | + "start": 7, |
| 259 | + "end": 13, |
| 260 | + }, { |
| 261 | + "start": 18, |
| 262 | + "end": 19, |
| 263 | + }]}) |
| 264 | + del checklist['tool'] |
| 265 | + return checklist |
| 266 | + |
| 267 | + |
| 268 | +@pytest.fixture |
| 269 | +def predictions(polygon_inference, rectangle_inference, line_inference, |
| 270 | + entity_inference, segmentation_inference, checklist_inference, |
| 271 | + text_inference): |
| 272 | + return [ |
| 273 | + polygon_inference, rectangle_inference, line_inference, |
| 274 | + entity_inference, segmentation_inference, checklist_inference, |
| 275 | + text_inference |
| 276 | + ] |
0 commit comments