Skip to content

Commit 38bda97

Browse files
zhen.chenAChenQ
authored andcommitted
feat: add "RLESubcatalog" & "LabeledRLE" & "RLE" class
PR Closed: #840
1 parent d3cfe08 commit 38bda97

File tree

10 files changed

+352
-47
lines changed

10 files changed

+352
-47
lines changed

tensorbay/geometry/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from .box import Box2D, Box3D
99
from .keypoint import Keypoint2D, Keypoints2D
10-
from .polygon import MultiPolygon, Polygon
10+
from .polygon import RLE, MultiPolygon, Polygon
1111
from .polyline import MultiPolyline2D, Polyline2D
1212
from .transform import Transform3D
1313
from .vector import Vector, Vector2D, Vector3D
@@ -21,6 +21,7 @@
2121
"Polyline2D",
2222
"MultiPolygon",
2323
"MultiPolyline2D",
24+
"RLE",
2425
"Transform3D",
2526
"Vector",
2627
"Vector2D",

tensorbay/geometry/polygon.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from typing import Dict, Iterable, List, Optional, Type, TypeVar
1414

15-
from ..utility import common_loads
15+
from ..utility import UserMutableSequence, common_loads
1616
from .point_list import MultiPointList2D, PointList2D
1717
from .vector import Vector2D
1818

@@ -151,3 +151,70 @@ def dumps(self) -> List[List[Dict[str, float]]]:
151151
152152
"""
153153
return self._dumps()
154+
155+
156+
class RLE(UserMutableSequence[int]):
157+
"""This class defines the concept of RLE.
158+
159+
:class:`RLE` contains an rle format mask.
160+
161+
Arguments:
162+
rle: A rle format mask.
163+
164+
Examples:
165+
>>> RLE([272, 2, 4, 4, 2, 9])
166+
RLE [
167+
272,
168+
2,
169+
...
170+
]
171+
172+
"""
173+
174+
_data: List[int]
175+
176+
def __init__(self, rle: Optional[Iterable[int]]):
177+
self._data = list(rle) if rle else []
178+
179+
def _dumps(self) -> List[int]:
180+
return self._data
181+
182+
def _loads(self, contents: List[int]) -> None:
183+
self._data = contents
184+
185+
@classmethod
186+
def loads(cls: Type["RLE"], contents: List[int]) -> "RLE":
187+
"""Loads a :class:RLE` from the given contents.
188+
189+
Arguments:
190+
contents: One rle mask.
191+
192+
Returns:
193+
The loaded :class:`RLE` object.
194+
195+
Examples:
196+
>>> contents = [272, 2, 4, 4, 2, 9]
197+
>>> rle = RLE.loads(contents)
198+
>>> rle
199+
RLE [
200+
272,
201+
2,
202+
...
203+
]
204+
205+
"""
206+
return common_loads(cls, contents)
207+
208+
def dumps(self) -> List[int]:
209+
"""Dumps a :class:`RLE` into one rle mask.
210+
211+
Returns:
212+
All the information of the :class:`RLE`.
213+
214+
Examples:
215+
>>> rle = RLE([272, 2, 4, 4, 2, 9])
216+
>>> rle.dumps()
217+
[272, 2, 4, 4, 2, 9]
218+
219+
"""
220+
return self._dumps()

tensorbay/geometry/tests/test_polygon.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
# Copyright 2021 Graviti. Licensed under MIT License.
44
#
55

6-
from .. import Box2D, MultiPolygon, Polygon, Vector2D
6+
from ..box import Box2D
7+
from ..polygon import RLE, MultiPolygon, Polygon
8+
from ..vector import Vector2D
79

810
_DATA_POLYGON = [{"x": 1.0, "y": 1.0}, {"x": 2.0, "y": 2.0}, {"x": 2.0, "y": 3.0}]
911
_DATA_MULTIPOLYGON = [
1012
[{"x": 1.0, "y": 4.0}, {"x": 2.0, "y": 3.7}, {"x": 7.0, "y": 4.0}],
1113
[{"x": 5.0, "y": 7.0}, {"x": 6.0, "y": 7.0}, {"x": 9.0, "y": 8.0}],
1214
]
15+
_DATA_RLE = [272, 2, 4, 4, 2, 9]
1316

1417

1518
class TestPolygon:
@@ -63,3 +66,16 @@ def test_dumps(self):
6366
[[[1.0, 4.0], [2.0, 3.7], [7.0, 4.0]], [[5.0, 7.0], [6.0, 7.0], [9.0, 8.0]]]
6467
)
6568
assert multipolygon.dumps() == _DATA_MULTIPOLYGON
69+
70+
71+
class TestRLE:
72+
def test_init(self):
73+
assert RLE([272, 2, 4, 4, 2, 9])._data == [272, 2, 4, 4, 2, 9]
74+
75+
def test_loads(self):
76+
rle = RLE.loads(_DATA_RLE)
77+
assert rle._data == _DATA_RLE
78+
79+
def test_dumps(self):
80+
rle = RLE(_DATA_RLE)
81+
assert rle.dumps() == _DATA_RLE

tensorbay/label/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
from .label_polygon import (
1616
LabeledMultiPolygon,
1717
LabeledPolygon,
18+
LabeledRLE,
1819
MultiPolygonSubcatalog,
1920
PolygonSubcatalog,
21+
RLESubcatalog,
2022
)
2123
from .label_polyline import (
2224
LabeledMultiPolyline2D,
@@ -35,23 +37,25 @@
3537
"CategoryInfo",
3638
"Classification",
3739
"ClassificationSubcatalog",
40+
"Items",
3841
"Keypoints2DSubcatalog",
3942
"KeypointsInfo",
4043
"Label",
4144
"LabelType",
4245
"LabeledBox2D",
4346
"LabeledBox3D",
4447
"LabeledKeypoints2D",
48+
"LabeledMultiPolygon",
4549
"LabeledMultiPolyline2D",
4650
"LabeledPolygon",
4751
"LabeledPolyline2D",
52+
"LabeledRLE",
4853
"LabeledSentence",
49-
"MultiPolyline2DSubcatalog",
50-
"LabeledMultiPolygon",
51-
"Items",
5254
"MultiPolygonSubcatalog",
55+
"MultiPolyline2DSubcatalog",
5356
"PolygonSubcatalog",
5457
"Polyline2DSubcatalog",
58+
"RLESubcatalog",
5559
"SentenceSubcatalog",
5660
"Subcatalogs",
5761
"Word",

tensorbay/label/basic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class LabelType(TypeEnum):
4343
POLYGON = "polygon"
4444
POLYLINE2D = "polyline2d"
4545
MULTI_POLYLINE2D = "multi_polyline2d"
46+
RLE = "rle"
4647
KEYPOINTS2D = "keypoints2d"
4748
MULTI_POLYGON = "multi_polygon"
4849
SENTENCE = "sentence"

tensorbay/label/catalog.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
:class:`.PolygonSubcatalog` subcatalog for polygon type of label
2626
:class:`.Polyline2DSubcatalog` subcatalog for 2D polyline type of label
2727
:class:`.MultiPolygonSubcatalog` subcatalog for multiple polygon type of label
28+
:class:`.RLESubcatalog` subcatalog for rle mask type of label
2829
:class:`.MultiPolyline2DSubcatalog` subcatalog for 2D multiple polyline type of label
2930
:class:`.SentenceSubcatalog` subcatalog for transcripted sentence type of label
3031
=================================== ==================================================
@@ -39,7 +40,7 @@
3940
from .label_box import Box2DSubcatalog, Box3DSubcatalog
4041
from .label_classification import ClassificationSubcatalog
4142
from .label_keypoints import Keypoints2DSubcatalog
42-
from .label_polygon import MultiPolygonSubcatalog, PolygonSubcatalog
43+
from .label_polygon import MultiPolygonSubcatalog, PolygonSubcatalog, RLESubcatalog
4344
from .label_polyline import MultiPolyline2DSubcatalog, Polyline2DSubcatalog
4445
from .label_sentence import SentenceSubcatalog
4546

@@ -52,6 +53,7 @@
5253
MultiPolyline2DSubcatalog,
5354
Keypoints2DSubcatalog,
5455
MultiPolygonSubcatalog,
56+
RLESubcatalog,
5557
SentenceSubcatalog,
5658
]
5759

@@ -104,6 +106,7 @@ class Catalog(ReprMixin, AttrsMixin):
104106
multi_polyline2d: MultiPolyline2DSubcatalog = _attr()
105107
keypoints2d: Keypoints2DSubcatalog = _attr()
106108
multi_polygon: MultiPolygonSubcatalog = _attr()
109+
rle: RLESubcatalog = _attr()
107110
sentence: SentenceSubcatalog = _attr()
108111

109112
def __bool__(self) -> bool:

tensorbay/label/label.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
.. table:: label classes
1414
:widths: auto
1515
16-
============================================================= ===================================
17-
label classes explaination
18-
============================================================= ===================================
19-
:class:`~tensorbay.label.label_classification.Classification` classification type of label
20-
:class:`~tensorbay.label.label_box.LabeledBox2D` 2D bounding box type of label
21-
:class:`~tensorbay.label.label_box.LabeledBox3D` 3D bounding box type of label
22-
:class:`~tensorbay.label.label_polygon.LabeledPolygon` polygon type of label
23-
:class:`~tensorbay.label.label_polyline.LabeledPolyline2D` 2D polyline type of label
24-
:class:`~tensorbay.label.label_keypoints.LabeledKeypoints2D` 2D keypoints type of label
25-
:class:`~tensorbay.label.label_sentence.LabeledSentence` transcripted sentence type of label
26-
============================================================= ===================================
16+
================================ ===================================
17+
label classes explaination
18+
================================ ===================================
19+
:class:`.Classification` classification type of label
20+
:class:`.LabeledBox2D` 2D bounding box type of label
21+
:class:`.LabeledBox3D` 3D bounding box type of label
22+
:class:`.LabeledPolygon` polygon type of label
23+
:class:`.LabeledMultiPolygon` polygon lists type of label
24+
:class:`.LabeledRLE` rle mask type of label
25+
:class:`.LabeledPolyline2D` 2D polyline type of label
26+
:class:`.LabeledMultiPolyline2D` 2D polyline lists type of label
27+
:class:`.LabeledKeypoints2D` 2D keypoints type of label
28+
:class:`.LabeledSentence` transcripted sentence type of label
29+
================================ ===================================
2730
2831
"""
2932

@@ -35,7 +38,7 @@
3538
from .label_box import LabeledBox2D, LabeledBox3D
3639
from .label_classification import Classification
3740
from .label_keypoints import LabeledKeypoints2D
38-
from .label_polygon import LabeledMultiPolygon, LabeledPolygon
41+
from .label_polygon import LabeledMultiPolygon, LabeledPolygon, LabeledRLE
3942
from .label_polyline import LabeledMultiPolyline2D, LabeledPolyline2D
4043
from .label_sentence import LabeledSentence
4144

@@ -74,6 +77,7 @@ class Label(ReprMixin, AttrsMixin):
7477
polygon: List[LabeledPolygon] = _attr()
7578
polyline2d: List[LabeledPolyline2D] = _attr()
7679
multi_polyline2d: List[LabeledMultiPolyline2D] = _attr()
80+
rle: List[LabeledRLE] = _attr()
7781
keypoints2d: List[LabeledKeypoints2D] = _attr()
7882
multi_polygon: List[LabeledMultiPolygon] = _attr()
7983
sentence: List[LabeledSentence] = _attr()

0 commit comments

Comments
 (0)