Skip to content

Commit

Permalink
Fix conversion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kokseang committed Jan 23, 2025
1 parent 0ca57af commit 3b30746
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 149 deletions.
12 changes: 12 additions & 0 deletions config/topic_list_sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ mandatory_topic_list:
- /sensing/camera/camera3/image_rect_color/compressed
- /sensing/camera/camera4/image_rect_color/compressed
- /sensing/camera/camera5/image_rect_color/compressed
# - /sensing/camera/camera0/image_raw/compressed
# - /sensing/camera/camera1/image_raw/compressed
# - /sensing/camera/camera2/image_raw/compressed
# - /sensing/camera/camera3/image_raw/compressed
# - /sensing/camera/camera4/image_raw/compressed
# - /sensing/camera/camera5/image_raw/compressed
- /localization/kinematic_state
topic_list:
- /sensing/lidar/left/velodyne_packets
Expand All @@ -21,6 +27,12 @@ topic_list:
- /sensing/camera/camera3/image_rect_color/compressed
- /sensing/camera/camera4/image_rect_color/compressed
- /sensing/camera/camera5/image_rect_color/compressed
# - /sensing/camera/camera0/image_raw/compressed
# - /sensing/camera/camera1/image_raw/compressed
# - /sensing/camera/camera2/image_raw/compressed
# - /sensing/camera/camera3/image_raw/compressed
# - /sensing/camera/camera4/image_raw/compressed
# - /sensing/camera/camera5/image_raw/compressed
- /sensing/camera/camera0/camera_info
- /sensing/camera/camera1/camera_info
- /sensing/camera/camera2/camera_info
Expand Down
2 changes: 1 addition & 1 deletion perception_dataset/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class T4_FORMAT_DIRECTORY_NAME(Enum):
ANNOTATION = "annotation"
DATA = "data"
LIDARSEG_ANNO_FOLDER = "lidar_seg"
LIDARSEG_ANNO_FOLDER = "lidarseg"


class SENSOR_MODALITY_ENUM(Enum):
Expand Down
2 changes: 1 addition & 1 deletion perception_dataset/deepen/deepen_to_t4_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _format_annotations(self) -> Dict[str, Dict[str, Any]]:
)
return scenes_anno_dict

def _format_point_3d_annotations(self) -> Dict[str, Dict[Any]]:
def _format_point_3d_annotations(self) -> Dict[str, Dict[str, Any]]:
"""Format annotations for point_3d from input_anno_file."""
if self._label_info.label_format == LabelFormat.PAINT_3D:
deepen_annotations = DeepenSegmentationPainting3DAnnotations.from_file(
Expand Down
109 changes: 65 additions & 44 deletions perception_dataset/deepen/download_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

CLIENT_ID = os.environ["DEEPEN_CLIENT_ID"]
ACCESS_TOKEN = os.environ["DEEPEN_ACCESS_TOKEN"]
DATASET_ID = "r82dS6VVkok98ZWr1CTkU4NE"
_HEADERS = {
"Authorization": f"Bearer {ACCESS_TOKEN}",
"Content-Type": "application/json",
Expand Down Expand Up @@ -46,69 +45,90 @@ def get_paint3d_labels(dataset_ids: List[str], dataset_dir: str, output_name: st
"""
Get 3d paint labels to lidar_seg/<dataset_id>_<frame_id>.bin and <output_name>.json.
Example of a response:
{
'server': 'gunicorn', 'date': 'Wed, 15 Jan 2025 08:45:01 GMT',
'content-type': 'text/html; charset=utf-8',
'paint-metadata': '{
"format": "pako_compressed", "paint_categories": ["car", "wall"],
"frame_sizes": [173430, 173097, 172595, 172957, 173011, 173204, 173813, 173913, 174393, 175304, 175176, 174038, 174473, 174373, 172598,
171886, 172377, 171724, 173322, 173566, 175911, 176165, 177180, 177774, 178544, 179606, 178347,
177048, 176775, 176997, 177271, 177864, 179341, 178986, 179670, 179973, 178885, 178424, 179233, 179766, 178644, 179057, 177871, 178286, 178570,
177410, 176688, 176961, 176871, 176862, 176740, 177872, 179313, 179414, 179496, 178974, 178653, 178992, 178861, 178823, 178690, 178815, 178966, 178960, 178907]
}',
'vary': 'Accept, Cookie, Accept-Encoding',
'allow': 'POST, GET, OPTIONS',
'x-frame-options': 'DENY',
'x-content-type-options': 'nosniff',
'referrer-policy': 'same-origin',
'content-encoding': 'gzip',
'via': '1.1 google',
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
'Transfer-Encoding': 'chunked'
}
{
'server': 'gunicorn', 'date': 'Wed, 15 Jan 2025 08:45:01 GMT',
'content-type': 'text/html; charset=utf-8',
'paint-metadata': '{
"format": "pako_compressed", "paint_categories": ["car", "wall"],
"frame_sizes": [173430, 173097, 172595, 172957, 173011, 173204, 173813, 173913, 174393, 175304, 175176, 174038, 174473, 174373, 172598,
171886, 172377, 171724, 173322, 173566, 175911, 176165, 177180, 177774, 178544, 179606, 178347,
177048, 176775, 176997, 177271, 177864, 179341, 178986, 179670, 179973, 178885, 178424, 179233, 179766, 178644, 179057, 177871, 178286, 178570,
177410, 176688, 176961, 176871, 176862, 176740, 177872, 179313, 179414, 179496, 178974, 178653, 178992, 178861, 178823, 178690, 178815, 178966, 178960, 178907]
}',
'vary': 'Accept, Cookie, Accept-Encoding',
'allow': 'POST, GET, OPTIONS',
'x-frame-options': 'DENY',
'x-content-type-options': 'nosniff',
'referrer-policy': 'same-origin',
'content-encoding': 'gzip',
'via': '1.1 google',
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
'Transfer-Encoding': 'chunked'
}
<output_name>.json is in the following format:
[
{
"dataset_id": "DOnC2vK05ojPr7qiqCsk2Ee7",
"file_id": "0.pcd",
"label_type": "3d_point",
"label_id": "none", # Keep it for consistency with downstream tasks
"label_category_id": "none", # Keep it for consistency with downstream tasks
"total_lidar_points": 173430,
"sensor_id": "lidar",
"stage_id": "QA",
"paint_categories": ["car", "wall", ...],
"lidarseg_anno_file": "lidar_seg/DOnC2vK05ojPr7qiqCsk2Ee7_0.bin"
},
...
]
[
{
"dataset_id": "DOnC2vK05ojPr7qiqCsk2Ee7",
"file_id": "0.pcd",
"label_type": "3d_point",
"label_id": "none", # Keep it for consistency with downstream tasks
"label_category_id": "none", # Keep it for consistency with downstream tasks
"total_lidar_points": 173430,
"sensor_id": "lidar",
"stage_id": "QA",
"paint_categories": ["car", "wall", ...],
"lidarseg_anno_file": "lidar_seg/DOnC2vK05ojPr7qiqCsk2Ee7_0.bin"
},
...
]
"""
dataset_path = Path(dataset_dir) / T4_FORMAT_DIRECTORY_NAME.LIDARSEG_ANNO_FOLDER.value()
dataset_path = Path(dataset_dir) / T4_FORMAT_DIRECTORY_NAME.LIDARSEG_ANNO_FOLDER.value
dataset_path.mkdir(parents=True, exist_ok=True)
lidarseg_annos_info = []
for dataset_id in dataset_ids:
datasets_url = f"https://tools.deepen.ai/api/v2/datasets/{DATASET_ID}/label_types/3d_point/paint_labels?stageId=QA&sensor_id=lidar&labelSetId=default"
datasets_url = f"https://tools.deepen.ai/api/v2/datasets/{dataset_id}/label_types/3d_point/paint_labels?stageId=QA&sensor_id=lidar&labelSetId=default"
try:
response = requests.get(datasets_url, headers=_HEADERS)
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise SystemExit(e)

# Get pcd files
label_url = f"https://tools.deepen.ai/api/v2/datasets/{dataset_id}/labels?labels_with_file_ids_order=true&final=true"
try:
label_response = requests.get(label_url, headers=_HEADERS)
label_response.raise_for_status()
except requests.exceptions.RequestException as e:
raise SystemExit(e)

label_response_data = label_response.json()
pcd_files = [
pcd_file_name
for pcd_file_name in label_response_data["labels"].keys()
if pcd_file_name.endswith(EXTENSION_ENUM.PCD.value)
]

paint_metadata = json.loads(response.headers["paint-metadata"])
assert len(pcd_files) == len(
paint_metadata["frame_sizes"]
), "Number of pcd files and frame sizes do not match!"

decompressed_data = zlib.decompress(response.content)
lidarseg_category_indices = np.frombuffer(decompressed_data, dtype=np.uint8)
# Split based on pointcloud size
previous_frame_size = 0
for file_id, frame_size in enumerate(paint_metadata["frame_sizes"]):

for file_id, frame_size in zip(pcd_files, paint_metadata["frame_sizes"]):

lidarseg_anno_filename = (
dataset_path / f"{dataset_id}_{str(file_id)}{EXTENSION_ENUM.BIN.value}"
)

annos_info = {
"dataset_id": dataset_id,
"file_id": f"{file_id}.pcd",
"file_id": file_id,
"label_id": "none", # Keep it for consistency with downstream tasks
"label_category_id": "none", # Keep it for consistency with downstream tasks
"total_lidar_points": frame_size,
Expand All @@ -127,11 +147,12 @@ def get_paint3d_labels(dataset_ids: List[str], dataset_dir: str, output_name: st
lidarseg_annos_info.append(annos_info)

# Save lidarseg anno information to json
output_file = os.path.join(dataset_dir, output_name)
with open(output_file, "w") as f:
output_name = Path(dataset_dir, output_name)
output_name.parents[0].mkdir(parents=True, exist_ok=True)
with open(output_name, "w") as f:
json.dump(lidarseg_annos_info, f, indent=4)

print(f"Annotation file is saved: {output_file}")
print(f"Annotation file is saved: {output_name}")


def get_datasets(dataset_ids: List[str], dataset_dir: str, output_name: str, label_type: str):
Expand Down
91 changes: 47 additions & 44 deletions perception_dataset/deepen/segmentation/painting3d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from dataclasses import dataclass
from dataclasses import dataclass, field
import json
import re
from typing import Any, Dict, List

from perception_dataset.deepen.deepen_annotation import DeepenAnnotation, LabelType
Expand All @@ -22,9 +23,9 @@ class DeepenSegmentationPainting3DScene(DeepenAnnotation):
:param total_lidar_points: Total number of lidar pointclouds.
"""

paint_categories: List[str]
lidarseg_anno_file: str
total_lidar_points: int
paint_categories: List[str] = field(default_factory=list)
lidarseg_anno_file: str = ""
total_lidar_points: int = 0

def __post_init__(self) -> None:
"""
Expand All @@ -33,6 +34,7 @@ def __post_init__(self) -> None:
"""
assert len(self.paint_categories) > 0, "Length of categories must be more than 0!"
assert self.total_lidar_points > 0, "Lidar pointclouds must be more than 0!"
assert self.lidarseg_anno_file != "", "Must provide a lidarseg annotation file!"


@dataclass
Expand All @@ -48,21 +50,22 @@ def add_record_to_dataset(
self, segmentation_painting_3d_scene: DeepenSegmentationPainting3DScene
) -> None:
"""Add a DeepenSegmentationPainting3DScene to the dataset records."""
scene_id = segmentation_painting_3d_scene.file_id
filename = segmentation_painting_3d_scene.file_id.split(".")[0]
scene_id = int(re.sub(r"\D", "", filename[-6:]))
self.deepen_segmentation_scenes[scene_id] = segmentation_painting_3d_scene

def format_scene_annotations(self) -> Dict[int, List[Dict[str, Any]]]:
"""
Format scene annotations to
{
id:
[
{
"paint_categories": ["car", "wall", ...],
"lidarseg_anno_file": "lidar_seg/DOnC2vK05ojPr7qiqCsk2Ee7_0.bin",
...
}
]
id:
[
{
"paint_categories": ["car", "wall", ...],
"lidarseg_anno_file": "lidar_seg/DOnC2vK05ojPr7qiqCsk2Ee7_0.bin",
...
}
]
}
"""
return {
Expand All @@ -87,30 +90,30 @@ def from_file(
"""Return DeepenSegmentationPainting3DDataset from files.
Args:
ann_file (str): Annotation files path in json. The format is:
[
{
"dataset_id": "DOnC2vK05ojPr7qiqCsk2Ee7",
"file_id": "0.pcd",
"label_type": "3d_point",
"label_id": "none", # Keep it for consistency with downstream tasks
"label_category_id": "none", # Keep it for consistency with downstream tasks
"total_lidar_points": 173430,
"sensor_id": "lidar",
"stage_id": "QA",
"paint_categories": ["car", "wall", ...],
"lidarseg_anno_file": "lidar_seg/DOnC2vK05ojPr7qiqCsk2Ee7_0.bin"
},
...
]
data_root (str): Root directory of the T4 dataset.
camera2index (Dict[str, int]): Name mapping from camera name to camera index.
dataset_corresponding (Dict[str, str]): Key-value mapping of T4 dataset name and Deepen ID.
as_dict (bool, optional): Whether to output objects as dict or its instance.
Defaults to True.
ann_file (str): Annotation files path in json. The format is:
[
{
"dataset_id": "DOnC2vK05ojPr7qiqCsk2Ee7",
"file_id": "0.pcd",
"label_type": "3d_point",
"label_id": "none", # Keep it for consistency with downstream tasks
"label_category_id": "none", # Keep it for consistency with downstream tasks
"total_lidar_points": 173430,
"sensor_id": "lidar",
"stage_id": "QA",
"paint_categories": ["car", "wall", ...],
"lidarseg_anno_file": "lidar_seg/DOnC2vK05ojPr7qiqCsk2Ee7_0.bin"
},
...
]
data_root (str): Root directory of the T4 dataset.
camera2index (Dict[str, int]): Name mapping from camera name to camera index.
dataset_corresponding (Dict[str, str]): Key-value mapping of T4 dataset name and Deepen ID.
as_dict (bool, optional): Whether to output objects as dict or its instance.
Defaults to True.
Returns:
List[DeepenSegmentationPainting2D]: List of converted `DeepenSegmentationPainting2D`s.
List[DeepenSegmentationPainting2D]: List of converted `DeepenSegmentationPainting2D`s.
"""
with open(ann_file, "r") as f:
lidarseg_ann_info = json.load(f)
Expand Down Expand Up @@ -146,15 +149,15 @@ def from_file(
def format_deepen_annotations(self) -> Dict[str, Dict[int, List[Dict[str, Any]]]]:
"""
Convert to {
dataset_id: {
scene_id/frame_index: [
{
"paint_categories": ["car", "wall", ...],
"lidarseg_anno_file": "lidar_seg/DOnC2vK05ojPr7qiqCsk2Ee7_0.bin",
...
}
]
}
dataset_id: {
scene_id/frame_index: [
{
"paint_categories": ["car", "wall", ...],
"lidarseg_anno_file": "lidar_seg/DOnC2vK05ojPr7qiqCsk2Ee7_0.bin",
...
}
]
}
}
"""
return {
Expand Down
Loading

0 comments on commit 3b30746

Please sign in to comment.