Skip to content

Commit 95a8aff

Browse files
committed
Update structure to support subset
1 parent ebb32f7 commit 95a8aff

File tree

19 files changed

+158
-44
lines changed

19 files changed

+158
-44
lines changed

src/datumaro/plugins/data_formats/kitti_3d/base.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
import glob
66
import logging
7+
import os
78
import os.path as osp
89
from typing import List, Optional, Type, TypeVar
910

10-
from datumaro.components.annotation import AnnotationType, Bbox, LabelCategories
11+
from datumaro.components.annotation import AnnotationType, Bbox
1112
from datumaro.components.dataset_base import DatasetItem, SubsetBase
1213
from datumaro.components.errors import InvalidAnnotationError
1314
from datumaro.components.importer import ImportContext
14-
from datumaro.components.media import Image, PointCloud
15+
from datumaro.components.media import Image
1516
from datumaro.util.image import find_images
1617

17-
from .format import Kitti3dPath
18+
from .format import Kitti3DLabelMap, Kitti3dPath, make_kitti3d_categories
1819

1920
T = TypeVar("T")
2021

@@ -30,27 +31,38 @@ def __init__(
3031
ctx: Optional[ImportContext] = None,
3132
):
3233
assert osp.isdir(path), path
33-
super().__init__(subset=subset, media_type=PointCloud, ctx=ctx)
3434

3535
self._path = path
3636

37-
common_attrs = {"truncated", "occluded", "alpha", "dimensions", "location", "rotation_y"}
38-
self._categories = {AnnotationType.label: LabelCategories(attributes=common_attrs)}
37+
if not subset:
38+
folder_path = path.rsplit(Kitti3dPath.LABEL_DIR, 1)[0]
39+
img_dir = osp.join(folder_path, Kitti3dPath.IMAGE_DIR)
40+
if any(os.path.isdir(os.path.join(img_dir, item)) for item in os.listdir(img_dir)):
41+
subset = osp.split(path)[-1]
42+
self._path = folder_path
43+
super().__init__(subset=subset, ctx=ctx)
44+
45+
self._categories = make_kitti3d_categories(Kitti3DLabelMap)
3946
self._items = self._load_items()
4047

4148
def _load_items(self) -> List[DatasetItem]:
4249
items = []
50+
4351
image_dir = osp.join(self._path, Kitti3dPath.IMAGE_DIR)
4452
image_path_by_id = {
45-
osp.splitext(osp.relpath(p, image_dir))[0]: p
53+
osp.split(osp.splitext(osp.relpath(p, image_dir))[0])[-1]: p
4654
for p in find_images(image_dir, recursive=True)
4755
}
4856

57+
if self._subset == "default":
58+
ann_dir = osp.join(self._path, Kitti3dPath.LABEL_DIR)
59+
else:
60+
ann_dir = osp.join(self._path, Kitti3dPath.LABEL_DIR, self._subset)
61+
4962
label_categories = self._categories[AnnotationType.label]
50-
subset = osp.split(self._path)[-1]
5163

52-
for labels_path in sorted(glob.glob(osp.join(self._path, "*.txt"), recursive=True)):
53-
item_id = osp.splitext(osp.relpath(labels_path, self._path))[0]
64+
for labels_path in sorted(glob.glob(osp.join(ann_dir, "**", "*.txt"), recursive=True)):
65+
item_id = osp.splitext(osp.relpath(labels_path, ann_dir))[0]
5466
anns = []
5567

5668
try:
@@ -116,14 +128,18 @@ def _load_items(self) -> List[DatasetItem]:
116128
if image:
117129
image = Image.from_file(path=image)
118130

131+
if self._subset == "default":
132+
calib_path = osp.join(self._path, Kitti3dPath.CALIB_DIR, item_id + ".txt")
133+
else:
134+
calib_path = osp.join(
135+
self._path, Kitti3dPath.CALIB_DIR, self._subset, item_id + ".txt"
136+
)
119137
items.append(
120138
DatasetItem(
121139
id=item_id,
122-
subset=subset,
140+
subset=self._subset,
123141
media=image,
124-
attributes={
125-
"calib_path": osp.join(self._path, Kitti3dPath.CALIB_DIR, item_id + ".txt")
126-
},
142+
attributes={"calib_path": calib_path},
127143
annotations=anns,
128144
)
129145
)

src/datumaro/plugins/data_formats/kitti_3d/format.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,40 @@
44

55
import os.path as osp
66

7+
from datumaro.components.annotation import AnnotationType, LabelCategories
8+
79

810
class Kitti3dPath:
911
PCD_DIR = osp.join("velodyne")
1012
IMAGE_DIR = "image_2"
1113
LABEL_DIR = "label_2"
1214
CALIB_DIR = "calib"
15+
16+
17+
Kitti3DLabelMap = [
18+
"DontCare",
19+
"Car",
20+
"Pedestrian",
21+
"Van",
22+
"Truck",
23+
"Cyclist",
24+
"Sitter",
25+
"Train",
26+
"Motorcycle",
27+
"Bus",
28+
"Misc",
29+
]
30+
31+
32+
def make_kitti3d_categories(label_map=None):
33+
if label_map is None:
34+
label_map = Kitti3DLabelMap
35+
36+
categories = {}
37+
common_attrs = {"truncated", "occluded", "alpha", "dimensions", "location", "rotation_y"}
38+
label_categories = LabelCategories(attributes=common_attrs)
39+
for label in label_map:
40+
label_categories.add(label)
41+
categories[AnnotationType.label] = label_categories
42+
43+
return categories

src/datumaro/plugins/data_formats/kitti_3d/importer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Kitti3dImporter(Importer):
1717

1818
@classmethod
1919
def detect(cls, context: FormatDetectionContext) -> FormatDetectionConfidence:
20-
context.require_file(f"{Kitti3dPath.PCD_DIR}/*.bin")
2120
cls._check_ann_file(context.require_file(f"{Kitti3dPath.LABEL_DIR}/*.txt"), context)
2221
return FormatDetectionConfidence.MEDIUM
2322

@@ -43,6 +42,11 @@ def get_file_extensions(cls) -> List[str]:
4342

4443
@classmethod
4544
def find_sources(cls, path):
46-
return cls._find_sources_recursive(
45+
# return [{"url": path, "format": "kitti3d"}]
46+
sources = cls._find_sources_recursive(
4747
path, "", "kitti3d", dirname=Kitti3dPath.LABEL_DIR, file_filter=lambda p: osp.isdir(p)
4848
)
49+
if len(sources) == 0:
50+
return [{"url": path, "format": "kitti3d"}]
51+
else:
52+
return sources
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
P0: 7.215377000000e+02 0.000000000000e+00 6.095593000000e+02 0.000000000000e+00 0.000000000000e+00 7.215377000000e+02 1.728540000000e+02 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00 0.000000000000e+00
2+
P1: 7.215377000000e+02 0.000000000000e+00 6.095593000000e+02 -3.875744000000e+02 0.000000000000e+00 7.215377000000e+02 1.728540000000e+02 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00 0.000000000000e+00
3+
P2: 7.215377000000e+02 0.000000000000e+00 6.095593000000e+02 4.485728000000e+01 0.000000000000e+00 7.215377000000e+02 1.728540000000e+02 2.163791000000e-01 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00 2.745884000000e-03
4+
P3: 7.215377000000e+02 0.000000000000e+00 6.095593000000e+02 -3.395242000000e+02 0.000000000000e+00 7.215377000000e+02 1.728540000000e+02 2.199936000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00 2.729905000000e-03
5+
R0_rect: 9.999239000000e-01 9.837760000000e-03 -7.445048000000e-03 -9.869795000000e-03 9.999421000000e-01 -4.278459000000e-03 7.402527000000e-03 4.351614000000e-03 9.999631000000e-01
6+
Tr_velo_to_cam: 7.533745000000e-03 -9.999714000000e-01 -6.166020000000e-04 -4.069766000000e-03 1.480249000000e-02 7.280733000000e-04 -9.998902000000e-01 -7.631618000000e-02 9.998621000000e-01 7.523790000000e-03 1.480755000000e-02 -2.717806000000e-01
7+
Tr_imu_to_velo: 9.999976000000e-01 7.553071000000e-04 -2.035826000000e-03 -8.086759000000e-01 -7.854027000000e-04 9.998898000000e-01 -1.482298000000e-02 3.195559000000e-01 2.024406000000e-03 1.482454000000e-02 9.998881000000e-01 -7.997231000000e-01

0 commit comments

Comments
 (0)