Skip to content

Commit

Permalink
Merge pull request #37 from kurusugawa-computer/add-local-scene
Browse files Browse the repository at this point in the history
拡張KITTI形式をプライベートストレージ用に登録するコマンドの新規追加
  • Loading branch information
yuji38kwmt authored Feb 25, 2021
2 parents e547d5e + e76ff83 commit 39d2ca0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
8 changes: 8 additions & 0 deletions anno3d/annofab/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class DataPath:


class Uploader:
"""
Args:
client:
project: プロジェクトID
force: 入力データと補助データを上書きしてアップロードするかどうか。
"""

_client: AnnofabApi
_project: str

Expand Down
53 changes: 44 additions & 9 deletions anno3d/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import tempfile
from enum import Enum
from pathlib import Path
from typing import Any, Optional, Tuple, Type, TypeVar
from typing import Any, List, Optional, Tuple, Type, TypeVar

import fire

from anno3d.annofab.client import ClientLoader
from anno3d.annofab.constant import segment_type_instance, segment_type_semantic
from anno3d.annofab.project import Label, ProjectApi
from anno3d.annofab.uploader import Uploader
from anno3d.file_paths_loader import FilePathsLoader
from anno3d.file_paths_loader import FilePathsLoader, load_scene_file_paths
from anno3d.kitti.scene_uploader import SceneUploader, SceneUploaderInput, UploadKind
from anno3d.model.annotation_area import RectAnnotationArea, SphereAnnotationArea, WholeAnnotationArea
from anno3d.model.file_paths import FrameKind
from anno3d.model.input_files import InputData
from anno3d.simple_data_uploader import create_frame_meta, create_kitti_files, create_meta_file, upload

E = TypeVar("E", bound=Enum)
Expand Down Expand Up @@ -407,6 +408,17 @@ def upload_scene(
class LocalCommand:
""" ローカルファイルシステムに対する処理を行います """

@staticmethod
def _write_all_files_json(inputs: List[InputData], output_dir_path: Path):
all_files_json = output_dir_path / "_all_data.jsonl"
with all_files_json.open(mode="w", encoding="UTF-8") as writer:
for input_data in inputs:
writer.write(input_data.to_json(ensure_ascii=False, sort_keys=True))
writer.write("\n")

logger.info("%d 件のinput dataを、%sに出力しました", len(inputs), output_dir_path)
logger.info("メタデータ: %s", all_files_json.absolute())

@staticmethod
def make_kitti_data(
kitti_dir: str,
Expand Down Expand Up @@ -444,14 +456,37 @@ def make_kitti_data(
for paths in pathss
]

all_files_json = output_dir_path / "_all_data.jsonl"
with all_files_json.open(mode="w", encoding="UTF-8") as writer:
for input_data in inputs:
writer.write(input_data.to_json(ensure_ascii=False, sort_keys=True))
writer.write("\n")
LocalCommand._write_all_files_json(inputs, output_dir_path)

logger.info("%d 件のinput dataを、%sに出力しました", len(inputs), output_dir_path)
logger.info("メタデータ: %s", all_files_json.absolute())
@staticmethod
def make_scene(
scene_path: str, output_dir: str, input_data_id_prefix: str = "", sensor_height: Optional[float] = None,
) -> None:
"""
拡張kitti形式のファイル群を3dpc-editorに登録可能なファイル群に変換します。
annofabのプライベートストレージを利用する場合にこのコマンドを利用します。
Args:
scene_path: scene.metaファイルのファイルパス or scene.metaファイルの存在するディレクトリパス or kitti形式ディレクトリ
output_dir: 出力先ディレクトリ。
input_data_id_prefix: input_data_idの先頭に付与する文字列
sensor_height: 点群のセンサ(velodyne)の設置高。単位は点群の単位系(=kittiであれば[m])
3dpc-editorは、この値を元に地面の高さを仮定する。 指定が無い場合はkittiのvelodyneの設置高を採用する
Returns:
"""
output_dir_path = Path(output_dir)
pathss = load_scene_file_paths(Path(scene_path))

inputs = [
create_kitti_files(
input_data_id_prefix, output_dir_path, paths, camera_horizontal_fov=None, sensor_height=sensor_height
)
for paths in pathss
]

LocalCommand._write_all_files_json(inputs, output_dir_path)


class Command:
Expand Down
40 changes: 40 additions & 0 deletions anno3d/file_paths_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from pathlib import Path
from typing import List, Optional

from anno3d.kitti.scene_uploader import Defaults, SceneUploader
from anno3d.model.file_paths import FilePaths, FrameKey, FrameKind, ImagePaths
from anno3d.model.scene import Scene


class FilePathsLoader:
Expand Down Expand Up @@ -35,3 +37,41 @@ def id_to_paths(pcd_file: str) -> FilePaths:
)

return [id_to_paths(pcd_file) for pcd_file in os.listdir(pcd_dir)]


def load_scene_file_paths(scene_path: Path) -> List[FilePaths]:
"""
拡張KITTI形式のファイルを読み込む.
Args:
scene_path: 読み込み対象パス。 以下の何れかとなる
* scene.metaファイルのパス
* scene.metaファイルの存在するディレクトリのパス
* scene.metaが存在しないアップロード対象ディレクトリのパス
* "velodyne/image_2/calib/label_2" のディレクトリがあるという前提で、読み込みを行う
"""
file = scene_path
if scene_path.is_dir():
file = scene_path / Defaults.scene_meta_file

scene_dir = file.parent
scene = Scene.decode_path(file) if file.is_file() else SceneUploader.default_scene(scene_path)

def scene_to_paths(frame_id: str) -> FilePaths:
images = [
ImagePaths(
scene_dir / f"{img.image_dir}/{frame_id}.png",
scene_dir / f"{img.calib_dir}/{frame_id}.txt",
img.camera_view_setting,
)
for img in scene.images
]

return FilePaths(
FrameKey(None, frame_id),
pcd=scene_dir / f"{scene.velodyne.velodyne_dir}/{frame_id}.bin",
images=images,
labels=[],
)

return [scene_to_paths(frame_id) for frame_id in scene.id_list]
4 changes: 2 additions & 2 deletions anno3d/kitti/scene_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(self, client: AnnofabApi):
self._project = ProjectApi(client)

@staticmethod
def _default_scene(path: Path) -> Scene:
def default_scene(path: Path) -> Scene:
velo_dir = path / Defaults.velo_dir
image_dir = path / Defaults.image_dir
calib_dir = path / Defaults.calib_dir
Expand Down Expand Up @@ -102,7 +102,7 @@ def upload_from_path(self, scene_path: Path, uploader_input: SceneUploaderInput,
if scene_path.is_dir():
file = scene_path / Defaults.scene_meta_file

scene = Scene.decode_path(file) if file.is_file() else self._default_scene(scene_path)
scene = Scene.decode_path(file) if file.is_file() else self.default_scene(scene_path)
return self.upload_scene(scene, uploader_input, force=force)

@staticmethod
Expand Down

0 comments on commit 39d2ca0

Please sign in to comment.