Skip to content

Commit 0b03f6f

Browse files
authored
Merge pull request #1016 from Labelbox/kopreschko/AL-5309
Fix error message when end point coords are smaller than start point coords
2 parents 1fbeb54 + 53529d3 commit 0b03f6f

File tree

3 files changed

+111
-8
lines changed

3 files changed

+111
-8
lines changed

labelbox/data/serialization/ndjson/objects.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ def from_common(cls,
198198
extra: Dict[str, Any],
199199
data: Union[ImageData, TextData],
200200
confidence: Optional[float] = None) -> "NDRectangle":
201-
return cls(bbox=Bbox(top=rectangle.start.y,
202-
left=rectangle.start.x,
203-
height=rectangle.end.y - rectangle.start.y,
204-
width=rectangle.end.x - rectangle.start.x),
201+
return cls(bbox=Bbox(top=min(rectangle.start.y, rectangle.end.y),
202+
left=min(rectangle.start.x, rectangle.end.x),
203+
height=abs(rectangle.end.y - rectangle.start.y),
204+
width=abs(rectangle.end.x - rectangle.start.x)),
205205
data_row=DataRow(id=data.uid, global_key=data.global_key),
206206
name=name,
207207
schema_id=feature_schema_id,
@@ -230,10 +230,10 @@ def to_common(self, name: str, feature_schema_id: Cuid,
230230
@classmethod
231231
def from_common(cls, frame: int, rectangle: Rectangle):
232232
return cls(frame=frame,
233-
bbox=Bbox(top=rectangle.start.y,
234-
left=rectangle.start.x,
235-
height=rectangle.end.y - rectangle.start.y,
236-
width=rectangle.end.x - rectangle.start.x))
233+
bbox=Bbox(top=min(rectangle.start.y, rectangle.end.y),
234+
left=min(rectangle.start.x, rectangle.end.x),
235+
height=abs(rectangle.end.y - rectangle.start.y),
236+
width=abs(rectangle.end.x - rectangle.start.x)))
237237

238238

239239
class NDSegment(BaseModel):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"dataRow": {
4+
"id": "ckrb1sf1i1g7i0ybcdc6oc8ct"
5+
},
6+
"name": "bbox",
7+
"uuid": "c1be3a57-597e-48cb-8d8d-a852665f9e72",
8+
"classifications": [],
9+
"bbox": {
10+
"top": 28.0,
11+
"left": 38.0,
12+
"height": 41.0,
13+
"width": 43.0
14+
}
15+
}
16+
]
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)