|
| 1 | +import json |
| 2 | +import labelbox.types as lb_types |
| 3 | +from labelbox.data.serialization.ndjson.converter import NDJsonConverter |
| 4 | + |
| 5 | +DATAROW_ID = "ckrb1sf1i1g7i0ybcdc6oc8ct" |
| 6 | + |
| 7 | + |
| 8 | +def test_rectangle(): |
| 9 | + with open('tests/data/assets/ndjson/rectangle_import.json', 'r') as file: |
| 10 | + data = json.load(file) |
| 11 | + res = list(NDJsonConverter.deserialize(data)) |
| 12 | + res = list(NDJsonConverter.serialize(res)) |
| 13 | + assert res == data |
| 14 | + |
| 15 | + |
| 16 | +def test_rectangle_inverted_start_end_points(): |
| 17 | + with open('tests/data/assets/ndjson/rectangle_import.json', 'r') as file: |
| 18 | + data = json.load(file) |
| 19 | + |
| 20 | + bbox = lb_types.ObjectAnnotation( |
| 21 | + name="bbox", |
| 22 | + value=lb_types.Rectangle( |
| 23 | + start=lb_types.Point(x=81, y=69), |
| 24 | + end=lb_types.Point(x=38, y=28), |
| 25 | + ), |
| 26 | + extra={"uuid": "c1be3a57-597e-48cb-8d8d-a852665f9e72"}) |
| 27 | + |
| 28 | + label = lb_types.Label(data=lb_types.ImageData(uid=DATAROW_ID), |
| 29 | + annotations=[bbox]) |
| 30 | + |
| 31 | + res = list(NDJsonConverter.serialize([label])) |
| 32 | + assert res == data |
| 33 | + |
| 34 | + expected_bbox = lb_types.ObjectAnnotation( |
| 35 | + name="bbox", |
| 36 | + value=lb_types.Rectangle( |
| 37 | + start=lb_types.Point(x=38, y=28), |
| 38 | + end=lb_types.Point(x=81, y=69), |
| 39 | + ), |
| 40 | + extra={ |
| 41 | + "uuid": "c1be3a57-597e-48cb-8d8d-a852665f9e72", |
| 42 | + "page": None, |
| 43 | + "unit": None |
| 44 | + }) |
| 45 | + |
| 46 | + label = lb_types.Label(data=lb_types.ImageData(uid=DATAROW_ID), |
| 47 | + annotations=[expected_bbox]) |
| 48 | + |
| 49 | + res = list(NDJsonConverter.deserialize(res)) |
| 50 | + assert res == [label] |
| 51 | + |
| 52 | + |
| 53 | +def test_rectangle_mixed_start_end_points(): |
| 54 | + with open('tests/data/assets/ndjson/rectangle_import.json', 'r') as file: |
| 55 | + data = json.load(file) |
| 56 | + |
| 57 | + bbox = lb_types.ObjectAnnotation( |
| 58 | + name="bbox", |
| 59 | + value=lb_types.Rectangle( |
| 60 | + start=lb_types.Point(x=81, y=28), |
| 61 | + end=lb_types.Point(x=38, y=69), |
| 62 | + ), |
| 63 | + extra={"uuid": "c1be3a57-597e-48cb-8d8d-a852665f9e72"}) |
| 64 | + |
| 65 | + label = lb_types.Label(data=lb_types.ImageData(uid=DATAROW_ID), |
| 66 | + annotations=[bbox]) |
| 67 | + |
| 68 | + res = list(NDJsonConverter.serialize([label])) |
| 69 | + assert res == data |
| 70 | + |
| 71 | + bbox = lb_types.ObjectAnnotation( |
| 72 | + name="bbox", |
| 73 | + value=lb_types.Rectangle( |
| 74 | + start=lb_types.Point(x=38, y=28), |
| 75 | + end=lb_types.Point(x=81, y=69), |
| 76 | + ), |
| 77 | + extra={ |
| 78 | + "uuid": "c1be3a57-597e-48cb-8d8d-a852665f9e72", |
| 79 | + "page": None, |
| 80 | + "unit": None |
| 81 | + }) |
| 82 | + |
| 83 | + label = lb_types.Label(data=lb_types.ImageData(uid=DATAROW_ID), |
| 84 | + annotations=[bbox]) |
| 85 | + |
| 86 | + res = list(NDJsonConverter.deserialize(res)) |
| 87 | + assert res == [label] |
0 commit comments