1mod coco;
2mod helpers;
3mod jsonl;
4mod kitti;
5mod mot;
6mod openimages;
7mod types;
8mod voc;
9mod widerface;
10mod yolo;
11
12use std::fs;
13use std::path::Path;
14
15use crate::{DetectionDatasetFrame, EvalError, TrackingDatasetFrame};
16
17use self::coco::build_detection_dataset_from_coco;
18use self::jsonl::parse_dataset_jsonl;
19use self::mot::parse_and_build_mot;
20use self::openimages::parse_and_build_openimages;
21use self::types::{
22 CocoGroundTruthWire, CocoPredictionWire, DetectionDatasetFrameWire, TrackingDatasetFrameWire,
23};
24use self::widerface::parse_and_build_widerface;
25
26pub fn load_detection_dataset_coco_files(
27 ground_truth_path: &Path,
28 predictions_path: &Path,
29) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
30 let ground_truth_text =
31 fs::read_to_string(ground_truth_path).map_err(|err| EvalError::DatasetIo {
32 path: ground_truth_path.display().to_string(),
33 message: err.to_string(),
34 })?;
35 let predictions_text =
36 fs::read_to_string(predictions_path).map_err(|err| EvalError::DatasetIo {
37 path: predictions_path.display().to_string(),
38 message: err.to_string(),
39 })?;
40 parse_detection_dataset_coco(&ground_truth_text, &predictions_text)
41}
42
43pub fn parse_detection_dataset_coco(
44 ground_truth_json: &str,
45 predictions_json: &str,
46) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
47 let ground_truth: CocoGroundTruthWire =
48 serde_json::from_str(ground_truth_json).map_err(|err| EvalError::InvalidDatasetFormat {
49 format: "coco-ground-truth",
50 message: err.to_string(),
51 })?;
52 let predictions: Vec<CocoPredictionWire> =
53 serde_json::from_str(predictions_json).map_err(|err| EvalError::InvalidDatasetFormat {
54 format: "coco-predictions",
55 message: err.to_string(),
56 })?;
57 build_detection_dataset_from_coco(ground_truth, predictions)
58}
59
60pub fn load_detection_dataset_openimages_csv_files(
61 ground_truth_path: &Path,
62 predictions_path: &Path,
63) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
64 let ground_truth_text =
65 fs::read_to_string(ground_truth_path).map_err(|err| EvalError::DatasetIo {
66 path: ground_truth_path.display().to_string(),
67 message: err.to_string(),
68 })?;
69 let predictions_text =
70 fs::read_to_string(predictions_path).map_err(|err| EvalError::DatasetIo {
71 path: predictions_path.display().to_string(),
72 message: err.to_string(),
73 })?;
74 parse_detection_dataset_openimages_csv(&ground_truth_text, &predictions_text)
75}
76
77pub fn parse_detection_dataset_openimages_csv(
78 ground_truth_csv: &str,
79 predictions_csv: &str,
80) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
81 parse_and_build_openimages(ground_truth_csv, predictions_csv)
82}
83
84pub fn load_detection_dataset_yolo_label_dirs(
85 manifest_path: &Path,
86 ground_truth_labels_dir: &Path,
87 prediction_labels_dir: &Path,
88) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
89 let manifest_text = fs::read_to_string(manifest_path).map_err(|err| EvalError::DatasetIo {
90 path: manifest_path.display().to_string(),
91 message: err.to_string(),
92 })?;
93 yolo::load_yolo_label_dirs(
94 &manifest_text,
95 ground_truth_labels_dir,
96 prediction_labels_dir,
97 )
98}
99
100pub fn load_detection_dataset_voc_xml_dirs(
101 manifest_path: &Path,
102 ground_truth_xml_dir: &Path,
103 prediction_xml_dir: &Path,
104) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
105 let manifest_text = fs::read_to_string(manifest_path).map_err(|err| EvalError::DatasetIo {
106 path: manifest_path.display().to_string(),
107 message: err.to_string(),
108 })?;
109 voc::load_voc_xml_dirs(&manifest_text, ground_truth_xml_dir, prediction_xml_dir)
110}
111
112pub fn load_detection_dataset_kitti_label_dirs(
113 manifest_path: &Path,
114 ground_truth_labels_dir: &Path,
115 prediction_labels_dir: &Path,
116) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
117 let manifest_text = fs::read_to_string(manifest_path).map_err(|err| EvalError::DatasetIo {
118 path: manifest_path.display().to_string(),
119 message: err.to_string(),
120 })?;
121 kitti::load_kitti_label_dirs(
122 &manifest_text,
123 ground_truth_labels_dir,
124 prediction_labels_dir,
125 )
126}
127
128pub fn load_detection_dataset_widerface_files(
129 ground_truth_path: &Path,
130 predictions_path: &Path,
131) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
132 let ground_truth_text =
133 fs::read_to_string(ground_truth_path).map_err(|err| EvalError::DatasetIo {
134 path: ground_truth_path.display().to_string(),
135 message: err.to_string(),
136 })?;
137 let predictions_text =
138 fs::read_to_string(predictions_path).map_err(|err| EvalError::DatasetIo {
139 path: predictions_path.display().to_string(),
140 message: err.to_string(),
141 })?;
142 parse_detection_dataset_widerface(&ground_truth_text, &predictions_text)
143}
144
145pub fn parse_detection_dataset_widerface(
146 ground_truth_text: &str,
147 predictions_text: &str,
148) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
149 parse_and_build_widerface(ground_truth_text, predictions_text)
150}
151
152pub fn load_detection_dataset_jsonl_file(
153 path: &Path,
154) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
155 let text = fs::read_to_string(path).map_err(|err| EvalError::DatasetIo {
156 path: path.display().to_string(),
157 message: err.to_string(),
158 })?;
159 parse_detection_dataset_jsonl(&text)
160}
161
162pub fn parse_detection_dataset_jsonl(text: &str) -> Result<Vec<DetectionDatasetFrame>, EvalError> {
163 parse_dataset_jsonl(text, |line, line_no| {
164 let wire = serde_json::from_str::<DetectionDatasetFrameWire>(line).map_err(|err| {
165 EvalError::InvalidDatasetEntry {
166 line: line_no,
167 message: err.to_string(),
168 }
169 })?;
170 Ok(wire.into_runtime())
171 })
172}
173
174pub fn load_tracking_dataset_jsonl_file(
175 path: &Path,
176) -> Result<Vec<TrackingDatasetFrame>, EvalError> {
177 let text = fs::read_to_string(path).map_err(|err| EvalError::DatasetIo {
178 path: path.display().to_string(),
179 message: err.to_string(),
180 })?;
181 parse_tracking_dataset_jsonl(&text)
182}
183
184pub fn parse_tracking_dataset_jsonl(text: &str) -> Result<Vec<TrackingDatasetFrame>, EvalError> {
185 parse_dataset_jsonl(text, |line, line_no| {
186 let wire = serde_json::from_str::<TrackingDatasetFrameWire>(line).map_err(|err| {
187 EvalError::InvalidDatasetEntry {
188 line: line_no,
189 message: err.to_string(),
190 }
191 })?;
192 Ok(wire.into_runtime())
193 })
194}
195
196pub fn load_tracking_dataset_mot_txt_files(
197 ground_truth_path: &Path,
198 predictions_path: &Path,
199) -> Result<Vec<TrackingDatasetFrame>, EvalError> {
200 let ground_truth_text =
201 fs::read_to_string(ground_truth_path).map_err(|err| EvalError::DatasetIo {
202 path: ground_truth_path.display().to_string(),
203 message: err.to_string(),
204 })?;
205 let predictions_text =
206 fs::read_to_string(predictions_path).map_err(|err| EvalError::DatasetIo {
207 path: predictions_path.display().to_string(),
208 message: err.to_string(),
209 })?;
210 parse_tracking_dataset_mot(&ground_truth_text, &predictions_text)
211}
212
213pub fn parse_tracking_dataset_mot(
214 ground_truth_text: &str,
215 predictions_text: &str,
216) -> Result<Vec<TrackingDatasetFrame>, EvalError> {
217 parse_and_build_mot(ground_truth_text, predictions_text)
218}