1+ import os
2+ import numpy as np
3+ import json
4+ from cv2 import imread
5+
6+ from avstack .config import DATASETS
7+ from avstack .geometry import GlobalOrigin3D , ReferenceFrame , q_stan_to_cam , Box2D
8+ from avstack .calibration import CameraCalibration
9+ from .._dataset import BaseSceneDataset , BaseSceneManager
10+
11+
12+ _nominal_whitelist_types = ["car" ]
13+ _nominal_ignore_types = ["car" ]
14+
15+
16+ @DATASETS .register_module ()
17+ class RcCarsScenesManager (BaseSceneManager ):
18+ name = "rccars"
19+ nominal_whitelist_types = _nominal_whitelist_types
20+ nominal_ignore_types = _nominal_ignore_types
21+
22+ def __init__ (
23+ self ,
24+ data_dir : str ,
25+ seed : int = 1 ,
26+ verbose : bool = False ,
27+ ):
28+ if not os .path .exists (data_dir ):
29+ raise RuntimeError (f"Cannot find data dir at { data_dir } " )
30+ self .data_dir = data_dir
31+ self .verbose = verbose
32+ self .scenes = sorted (next (os .walk (data_dir ))[1 ])
33+
34+ def get_scene_dataset_by_index (self , scene_idx , split ):
35+ return RcCarsSceneDataset (self .data_dir , self .scenes [scene_idx ], split )
36+
37+ def get_scene_dataset_by_name (self , scene_name , split ):
38+ if not scene_name in self .scenes :
39+ raise IndexError (f"Cannot find scene { scene_name } in { self .scenes } " )
40+ return RcCarsSceneDataset (self .data_dir , scene_name , split )
41+
42+
43+
44+ class RcCarsSceneDataset (BaseSceneDataset ):
45+ name = "rccars"
46+ nominal_whitelist_types = _nominal_whitelist_types
47+ nominal_ignore_types = _nominal_ignore_types
48+
49+ def __init__ (
50+ self ,
51+ data_dir : str ,
52+ scene : str ,
53+ split : str ,
54+ whitelist_types : list = _nominal_whitelist_types ,
55+ ignore_types : list = _nominal_ignore_types ,
56+ ):
57+ # avstack fields
58+ self .data_dir = data_dir
59+ self .scene = scene
60+ self .split = split
61+ self .sequence_id = scene
62+ self .sensors = {"main_camera" : "camera" }
63+ self .sensor_IDs = {"camera" : "images" }
64+ self .scene_path = os .path .join (data_dir , scene , split )
65+ super ().__init__ (whitelist_types , ignore_types )
66+
67+ # load in the json
68+ with open (os .path .join (self .scene_path , f"{ split } .json" )) as f :
69+ self ._result = json .load (f )
70+
71+ # create variables needed for processing
72+ self ._img_id_to_file = {
73+ img ["id" ]: img ["file_name" ]
74+ for img in self ._result ["images" ]
75+ }
76+ self ._img_id_to_anns = {
77+ img ["id" ]: []
78+ for img in self ._result ["images" ]
79+ }
80+ for ann in self ._result ["annotations" ]:
81+ self ._img_id_to_anns [ann ["image_id" ]].append (ann )
82+ self ._obj_id_to_str = {
83+ cat ["id" ]: cat ["name" ]
84+ for cat in self ._result ["categories" ]
85+ }
86+
87+ @property
88+ def frames (self ):
89+ return sorted (list (self ._img_id_to_file .keys ()))
90+
91+ def __str__ (self ):
92+ return f"RCCar Dataset of folder: { self .scene_path } "
93+
94+ def _load_timestamp (self , * args , ** kwargs ):
95+ return 0.0 # TODO
96+
97+ def _load_calibration (self , frame : int , sensor : str , * args , ** kwargs ):
98+ reference = ReferenceFrame (x = np .zeros ((3 ,)), q = q_stan_to_cam , reference = GlobalOrigin3D )
99+ calib = CameraCalibration (
100+ reference = reference ,
101+ P = np .zeros ((3 , 4 )), # TODO
102+ img_shape = [100 , 100 ], # TODO
103+ )
104+ return calib
105+
106+ def _load_image (self , frame : int , sensor : str , ** kwargs ):
107+ file_name = self ._img_id_to_file [frame ]
108+ # img_folder = "images" if sensor is None else self.sensor_IDs[sensor]
109+ full_path = os .path .join (self .scene_path , file_name )
110+
111+ if not os .path .exists (full_path ):
112+ raise FileNotFoundError (full_path )
113+
114+ return imread (full_path )
115+
116+ def get_boxes (self , frame : int , sensor : str = None , ** kwargs ):
117+ calib = self .get_calibration (frame = frame , sensor = sensor )
118+ boxes = [
119+ Box2D (
120+ box2d = [
121+ ann ["bbox" ][0 ],
122+ ann ["bbox" ][1 ],
123+ ann ["bbox" ][0 ] + ann ["bbox" ][2 ],
124+ ann ["bbox" ][1 ] + ann ["bbox" ][3 ],
125+ ],
126+ calibration = calib ,
127+ obj_type = self ._obj_id_to_str [ann ["category_id" ]]
128+ )
129+ for ann in self ._img_id_to_anns [frame ]
130+ ]
131+ return boxes
132+
133+ def get_ego_reference (self , * args , ** kwargs ):
134+ return GlobalOrigin3D
135+
0 commit comments