hotcoco 1.0.1

Perception evaluation in pure Rust — a pycocotools-compatible COCO/LVIS/Open Images engine with diagnostics and dataset tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Regression tests for the 1.0 preflight converter fixes.
//!
//! Each test pins one behavioral fix: CVAT open/close-pair shapes, YOLO
//! block-form `names:`, comma-safe YAML names, VOC float coordinates and the
//! 1-based inclusive convention, stem-collision detection, and the unified
//! missing-dimensions / malformed-input policies.

use std::collections::HashMap;

use hotcoco::convert::{
    ConvertError, coco_to_dota, coco_to_oid, coco_to_voc, coco_to_yolo, cvat_to_coco, dota_to_coco,
    voc_to_coco, yolo_to_coco,
};
use hotcoco::types::{Annotation, Category, Dataset, Image, Segmentation};

/// A one-image dataset with the given file name and simple bbox annotations.
fn one_image_dataset(file_name: &str, cats: &[&str], bboxes: &[(u64, [f64; 4])]) -> Dataset {
    Dataset {
        info: None,
        images: vec![Image {
            id: 1,
            file_name: file_name.into(),
            width: 640,
            height: 480,
            ..Default::default()
        }],
        annotations: bboxes
            .iter()
            .enumerate()
            .map(|(i, &(cat_id, bbox))| Annotation {
                id: (i + 1) as u64,
                image_id: 1,
                category_id: cat_id,
                bbox: Some(bbox),
                area: Some(bbox[2] * bbox[3]),
                ..Default::default()
            })
            .collect(),
        categories: cats
            .iter()
            .enumerate()
            .map(|(i, name)| Category {
                id: (i + 1) as u64,
                name: (*name).into(),
                ..Default::default()
            })
            .collect(),
        licenses: vec![],
    }
}

// ── CVAT ─────────────────────────────────────────────────────────────────────

/// CVAT writes a shape as an open/close pair whenever it has `<attribute>`
/// children. Those shapes must import, not vanish — a real CVAT export can
/// otherwise read as zero annotations with no error.
#[test]
fn cvat_start_event_shapes_with_attribute_children_import() {
    let dir = tempfile::tempdir().expect("tempdir");
    let xml_path = dir.path().join("annotations.xml");
    let xml = r#"<?xml version="1.0" encoding="utf-8"?>
<annotations>
  <version>1.1</version>
  <meta><task><labels>
    <label><name>person</name></label>
    <label><name>region</name></label>
  </labels></task></meta>
  <image id="0" name="test.jpg" width="640" height="480">
    <box label="person" xtl="100" ytl="50" xbr="300" ybr="400" occluded="0">
      <attribute name="pose">standing</attribute>
      <attribute name="verified">true</attribute>
    </box>
    <polygon label="region" points="10.0,20.0;50.0,20.0;50.0,80.0" occluded="0">
      <attribute name="kind">roi</attribute>
    </polygon>
  </image>
</annotations>"#;
    std::fs::write(&xml_path, xml).expect("write xml");

    let (dataset, stats) = cvat_to_coco(&xml_path).expect("cvat_to_coco");

    assert_eq!(dataset.annotations.len(), 2, "both pair-form shapes import");
    assert_eq!(stats.boxes, 1);
    assert_eq!(stats.polygons, 1);
    assert_eq!(stats.skipped_unsupported, 0, "<attribute> is not a shape");

    let box_ann = &dataset.annotations[0];
    assert_eq!(box_ann.bbox.expect("bbox"), [100.0, 50.0, 200.0, 350.0]);
    let poly_ann = &dataset.annotations[1];
    assert!(matches!(
        poly_ann.segmentation,
        Some(Segmentation::Polygon(_))
    ));
}

/// A `<box>` missing a coordinate attribute must be an error, never a silent
/// `0.0` default producing a plausible wrong box.
#[test]
fn cvat_missing_coordinate_attribute_is_an_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let xml_path = dir.path().join("annotations.xml");
    let xml = r#"<annotations>
  <image id="0" name="a.jpg" width="10" height="10">
    <box label="thing" xtl="1" ytl="2" xbr="3" occluded="0"/>
  </image>
</annotations>"#;
    std::fs::write(&xml_path, xml).expect("write xml");

    let err = cvat_to_coco(&xml_path).expect_err("missing ybr must fail");
    let msg = err.to_string();
    assert!(msg.contains("ybr"), "error names the attribute: {msg}");
    assert!(
        msg.contains("annotations.xml"),
        "error names the file: {msg}"
    );
}

/// A missing `points` attribute is malformed input (error); a *present* points
/// list with fewer than 3 points is degenerate — skipped and counted, keeping
/// the rest of the file.
#[test]
fn cvat_degenerate_polygon_is_skipped_and_counted() {
    let dir = tempfile::tempdir().expect("tempdir");
    let xml_path = dir.path().join("annotations.xml");
    let xml = r#"<annotations>
  <image id="0" name="a.jpg" width="100" height="100">
    <polygon label="thing" points="10,10;50,50" occluded="0"/>
    <box label="thing" xtl="10" ytl="10" xbr="50" ybr="50" occluded="0"/>
  </image>
</annotations>"#;
    std::fs::write(&xml_path, xml).expect("write xml");

    let (dataset, stats) = cvat_to_coco(&xml_path).expect("file must survive");
    assert_eq!(dataset.annotations.len(), 1, "the box still imports");
    assert_eq!(
        stats.skipped_degenerate, 1,
        "the 2-point polygon is counted"
    );

    let no_points = r#"<annotations>
  <image id="0" name="a.jpg" width="100" height="100">
    <polygon label="thing" occluded="0"/>
  </image>
</annotations>"#;
    std::fs::write(&xml_path, no_points).expect("write xml");
    let err = cvat_to_coco(&xml_path).expect_err("missing points attribute");
    assert!(err.to_string().contains("points"), "error was: {err}");
}

/// A CVAT for *video* export nests shapes under `<track>`, not `<image>`.
/// Importing one used to yield an empty dataset with no error.
#[test]
fn cvat_video_export_is_a_clear_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    let xml_path = dir.path().join("annotations.xml");
    let xml = r#"<annotations>
  <version>1.1</version>
  <track id="0" label="person">
    <box frame="0" xtl="1" ytl="2" xbr="3" ybr="4" outside="0"/>
  </track>
</annotations>"#;
    std::fs::write(&xml_path, xml).expect("write xml");

    let err = cvat_to_coco(&xml_path).expect_err("video export must not import as empty");
    assert!(err.to_string().contains("video"), "error was: {err}");
}

/// Export: an annotation whose polygons are all degenerate falls back to its
/// bbox instead of vanishing; the degenerate polygons are counted.
#[test]
fn cvat_export_counts_degenerate_polygons_and_falls_back_to_bbox() {
    let mut dataset = one_image_dataset("a.jpg", &["thing"], &[(1, [10.0, 10.0, 20.0, 20.0])]);
    dataset.annotations[0].segmentation =
        Some(Segmentation::Polygon(vec![vec![1.0, 1.0, 2.0, 2.0]])); // 2 points

    let dir = tempfile::tempdir().expect("tempdir");
    let out = dir.path().join("out.xml");
    let stats = hotcoco::convert::coco_to_cvat(&dataset, &out).expect("coco_to_cvat");
    assert_eq!(stats.skipped_degenerate, 1);
    assert_eq!(stats.boxes, 1, "bbox fallback");
    assert_eq!(stats.polygons, 0);
    assert_eq!(stats.skipped_no_geometry, 0);

    let (recovered, _) = cvat_to_coco(&out).expect("re-import");
    assert_eq!(recovered.annotations.len(), 1);
    assert_eq!(
        recovered.annotations[0].bbox.expect("bbox"),
        [10.0, 10.0, 20.0, 20.0]
    );
}

// ── YOLO ─────────────────────────────────────────────────────────────────────

/// The block/mapping form of `names:` is what Ultralytics writes by default;
/// rejecting it means rejecting most real data.yaml files.
#[test]
fn yolo_block_mapping_names_parse() {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        dir.path().join("data.yaml"),
        "path: ../datasets/coco\ntrain: images/train\nnc: 2\nnames:\n  0: person\n  1: bicycle\nextra: 1\n",
    )
    .expect("write data.yaml");
    std::fs::write(dir.path().join("img.txt"), "1 0.5 0.5 0.2 0.2\n").expect("write label");

    let dims: HashMap<String, (u32, u32)> = [("img".to_string(), (100u32, 100u32))].into();
    let ds = yolo_to_coco(dir.path(), &dims).expect("yolo_to_coco");

    assert_eq!(ds.categories.len(), 2);
    assert_eq!(ds.categories[0].name, "person");
    assert_eq!(ds.categories[1].name, "bicycle");
    assert_eq!(ds.annotations[0].category_id, 2, "class 1 → bicycle");
}

/// The block list form (`- person`) also appears in the wild.
#[test]
fn yolo_block_list_names_parse() {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        dir.path().join("data.yaml"),
        "nc: 2\nnames:\n  - person\n  - bicycle\n",
    )
    .expect("write data.yaml");
    std::fs::write(dir.path().join("img.txt"), "0 0.5 0.5 0.2 0.2\n").expect("write label");

    let dims: HashMap<String, (u32, u32)> = [("img".to_string(), (100u32, 100u32))].into();
    let ds = yolo_to_coco(dir.path(), &dims).expect("yolo_to_coco");
    assert_eq!(ds.categories.len(), 2);
    assert_eq!(ds.categories[0].name, "person");
}

/// Gaps or duplicates in the mapping indices would silently shift class ids.
#[test]
fn yolo_block_mapping_with_gap_is_an_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        dir.path().join("data.yaml"),
        "names:\n  0: person\n  2: bicycle\n",
    )
    .expect("write data.yaml");

    let err = yolo_to_coco(dir.path(), &HashMap::new()).expect_err("gap in indices");
    assert!(err.to_string().contains("gaps"), "error was: {err}");
}

/// A comma inside a category name must not split the flow list and shift every
/// later class index.
#[test]
fn yolo_names_containing_commas_round_trip() {
    let dataset = one_image_dataset(
        "img.jpg",
        &["Camera, still", "dog"],
        &[(1, [10.0, 10.0, 20.0, 20.0]), (2, [40.0, 40.0, 20.0, 20.0])],
    );
    let dir = tempfile::tempdir().expect("tempdir");
    coco_to_yolo(&dataset, dir.path()).expect("coco_to_yolo");

    let yaml = std::fs::read_to_string(dir.path().join("data.yaml")).expect("data.yaml");
    assert!(
        yaml.contains("'Camera, still'"),
        "comma name is quoted: {yaml}"
    );

    let dims: HashMap<String, (u32, u32)> = [("img".to_string(), (640u32, 480u32))].into();
    let recovered = yolo_to_coco(dir.path(), &dims).expect("yolo_to_coco");
    assert_eq!(
        recovered.categories.len(),
        2,
        "comma did not split the list"
    );
    assert_eq!(recovered.categories[0].name, "Camera, still");
    assert_eq!(recovered.categories[1].name, "dog");
}

/// Importing a YOLO label with no known image size used to multiply by 0×0 and
/// produce silent zero-size boxes; it is now an error.
#[test]
fn yolo_import_missing_dims_is_an_error() {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(dir.path().join("data.yaml"), "nc: 1\nnames: [thing]\n")
        .expect("write data.yaml");
    std::fs::write(dir.path().join("img.txt"), "0 0.5 0.5 0.2 0.2\n").expect("write label");

    let err = yolo_to_coco(dir.path(), &HashMap::new()).expect_err("no dims for img");
    assert!(
        matches!(err, ConvertError::MissingImageDimensions(_)),
        "got: {err}"
    );
}

/// Label parse errors carry the file and line.
#[test]
fn yolo_label_errors_name_file_and_line() {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(dir.path().join("data.yaml"), "nc: 1\nnames: [thing]\n")
        .expect("write data.yaml");
    std::fs::write(
        dir.path().join("img.txt"),
        "0 0.5 0.5 0.2 0.2\n0 0.5 oops 0.2 0.2\n",
    )
    .expect("write label");

    let dims: HashMap<String, (u32, u32)> = [("img".to_string(), (100u32, 100u32))].into();
    let err = yolo_to_coco(dir.path(), &dims).expect_err("bad cy");
    let msg = err.to_string();
    assert!(msg.contains("img.txt"), "names the file: {msg}");
    assert!(msg.contains("line 2"), "names the line: {msg}");
}

// ── VOC ──────────────────────────────────────────────────────────────────────

/// Real-world VOC files carry float coordinates like `156.00`; parsing them as
/// integers failed the file and aborted the whole directory import.
#[test]
fn voc_float_coordinates_parse() {
    let dir = tempfile::tempdir().expect("tempdir");
    let ann_dir = dir.path().join("Annotations");
    std::fs::create_dir_all(&ann_dir).expect("mkdir");
    let xml = r"<annotation>
  <filename>a.jpg</filename>
  <size><width>640</width><height>480</height></size>
  <object>
    <name>person</name>
    <bndbox><xmin>156.00</xmin><ymin>50.5</ymin><xmax>300.00</xmax><ymax>400.0</ymax></bndbox>
  </object>
</annotation>";
    std::fs::write(ann_dir.join("a.xml"), xml).expect("write xml");

    let ds = voc_to_coco(dir.path()).expect("float coords must parse");
    assert_eq!(ds.annotations.len(), 1);
    let bbox = ds.annotations[0].bbox.expect("bbox");
    // 1-based inclusive: x = 156 - 1, w = 300 - 156 + 1.
    assert!((bbox[0] - 155.0).abs() < 1e-9, "x: {}", bbox[0]);
    assert!((bbox[2] - 145.0).abs() < 1e-9, "w: {}", bbox[2]);
}

/// iscrowd → `<difficult>` → iscrowd survives a full round trip, and integer
/// boxes round-trip exactly under the 1-based inclusive convention.
#[test]
fn voc_round_trip_is_exact_and_keeps_iscrowd() {
    let mut dataset = one_image_dataset(
        "img.jpg",
        &["thing"],
        &[(1, [10.0, 20.0, 30.0, 40.0]), (1, [0.0, 0.0, 200.0, 150.0])],
    );
    dataset.annotations[0].iscrowd = true;

    let dir = tempfile::tempdir().expect("tempdir");
    coco_to_voc(&dataset, dir.path()).expect("coco_to_voc");
    let recovered = voc_to_coco(dir.path()).expect("voc_to_coco");

    assert_eq!(recovered.annotations.len(), 2);
    // Sorted output preserves annotation order within the single image.
    assert_eq!(
        recovered.annotations[0].bbox.expect("bbox"),
        [10.0, 20.0, 30.0, 40.0],
        "integer boxes round-trip exactly"
    );
    assert_eq!(
        recovered.annotations[1].bbox.expect("bbox"),
        [0.0, 0.0, 200.0, 150.0]
    );
    assert!(recovered.annotations[0].iscrowd, "iscrowd survives");
    assert!(!recovered.annotations[1].iscrowd);
}

/// VOC parse errors name the failing file.
#[test]
fn voc_errors_name_the_file() {
    let dir = tempfile::tempdir().expect("tempdir");
    let ann_dir = dir.path().join("Annotations");
    std::fs::create_dir_all(&ann_dir).expect("mkdir");
    let xml = r"<annotation>
  <filename>a.jpg</filename>
  <size><width>640</width><height>480</height></size>
  <object>
    <name>person</name>
    <bndbox><xmin>oops</xmin><ymin>1</ymin><xmax>2</xmax><ymax>2</ymax></bndbox>
  </object>
</annotation>";
    std::fs::write(ann_dir.join("bad.xml"), xml).expect("write xml");

    let err = voc_to_coco(dir.path()).expect_err("bad coordinate");
    assert!(err.to_string().contains("bad.xml"), "error was: {err}");
}

// ── Stem collisions ──────────────────────────────────────────────────────────

/// `train/img.jpg` and `val/img.jpg` share the stem `img`; every exporter that
/// keys output by stem must refuse rather than silently overwrite.
#[test]
fn exporters_reject_colliding_file_stems() {
    let mut dataset = one_image_dataset("train/img.jpg", &["thing"], &[(1, [1.0, 1.0, 2.0, 2.0])]);
    dataset.images.push(Image {
        id: 2,
        file_name: "val/img.jpg".into(),
        width: 640,
        height: 480,
        ..Default::default()
    });

    let dir = tempfile::tempdir().expect("tempdir");
    let is_collision = |e: &ConvertError| matches!(e, ConvertError::StemCollision { .. });

    let err = coco_to_yolo(&dataset, dir.path()).expect_err("yolo");
    assert!(is_collision(&err), "yolo: {err}");
    let err = coco_to_voc(&dataset, dir.path()).expect_err("voc");
    assert!(is_collision(&err), "voc: {err}");
    let err = coco_to_dota(&dataset, dir.path()).expect_err("dota");
    assert!(is_collision(&err), "dota: {err}");
    let err = coco_to_oid(&dataset, &dir.path().join("out.csv")).expect_err("oid");
    assert!(is_collision(&err), "oid: {err}");
}

// ── DOTA ─────────────────────────────────────────────────────────────────────

/// Malformed DOTA lines used to be skipped silently and uncounted; they are now
/// errors naming the file and line.
#[test]
fn dota_malformed_line_is_an_error_with_context() {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        dir.path().join("P0001.txt"),
        "0.0 0.0 4.0 0.0 4.0 3.0 0.0 3.0 plane 0\n1.0 2.0 nonsense\n",
    )
    .expect("write label");

    let err = dota_to_coco(dir.path(), None, &HashMap::new()).expect_err("short line");
    let msg = err.to_string();
    assert!(msg.contains("P0001.txt"), "names the file: {msg}");
    assert!(msg.contains("line 2"), "names the line: {msg}");

    std::fs::write(
        dir.path().join("P0001.txt"),
        "0.0 0.0 4.0 0.0 4.0 3.0 0.0 3.0 plane notanumber\n",
    )
    .expect("write label");
    let err = dota_to_coco(dir.path(), None, &HashMap::new()).expect_err("bad difficulty");
    assert!(err.to_string().contains("difficulty"), "error was: {err}");
}

/// Real DOTA labels start with `imagesource:`/`gsd:` metadata lines; the
/// stricter malformed-line policy must not reject them.
#[test]
fn dota_metadata_header_lines_are_skipped() {
    let dir = tempfile::tempdir().expect("tempdir");
    std::fs::write(
        dir.path().join("P0001.txt"),
        "imagesource:GoogleEarth\ngsd:0.146343\n0.0 0.0 4.0 0.0 4.0 3.0 0.0 3.0 plane 0\n",
    )
    .expect("write label");

    let ds = dota_to_coco(dir.path(), None, &HashMap::new()).expect("metadata lines skipped");
    assert_eq!(ds.annotations.len(), 1);
    assert_eq!(ds.categories[0].name, "plane");
    // Dims are metadata for DOTA: unknown stays 0×0, and file_name is the stem.
    assert_eq!(ds.images[0].file_name, "P0001");
    assert_eq!((ds.images[0].width, ds.images[0].height), (0, 0));
}

// ── Open Images ──────────────────────────────────────────────────────────────

/// OID CSV errors name the file as well as the line.
#[test]
fn oid_errors_name_the_file() {
    let dir = tempfile::tempdir().expect("tempdir");
    let csv = dir.path().join("boxes.csv");
    std::fs::write(
        &csv,
        "ImageID,LabelName,XMin,XMax,YMin,YMax\nimg1,/m/01,oops,0.5,0.3,0.4\n",
    )
    .expect("write csv");

    let err = hotcoco::convert::oid_to_coco(&csv, None, &HashMap::new()).expect_err("bad XMin");
    let msg = err.to_string();
    assert!(msg.contains("boxes.csv"), "names the file: {msg}");
    assert!(msg.contains("line 2"), "names the line: {msg}");
}

/// Exporting an annotated image with unknown dimensions used to skip its rows
/// with only a counter; normalization needs the true size, so it is an error.
#[test]
fn coco_to_oid_missing_dims_is_an_error() {
    let mut dataset = one_image_dataset("img.jpg", &["thing"], &[(1, [1.0, 1.0, 2.0, 2.0])]);
    dataset.images[0].width = 0;

    let dir = tempfile::tempdir().expect("tempdir");
    let err = coco_to_oid(&dataset, &dir.path().join("out.csv")).expect_err("zero width");
    assert!(
        matches!(err, ConvertError::MissingImageDimensions(_)),
        "got: {err}"
    );
}

// ── Cross-format policies ────────────────────────────────────────────────────

/// A dangling `category_id` is a data-integrity error on export, not a silent
/// skip (or a fabricated "unknown" label).
#[test]
fn exporters_reject_unknown_category_ids() {
    let mut dataset = one_image_dataset("img.jpg", &["thing"], &[(1, [1.0, 1.0, 2.0, 2.0])]);
    dataset.annotations[0].category_id = 99;

    let dir = tempfile::tempdir().expect("tempdir");
    let err = coco_to_yolo(&dataset, dir.path()).expect_err("dangling category");
    assert!(
        matches!(
            err,
            ConvertError::UnknownCategory {
                ann_id: 1,
                category_id: 99
            }
        ),
        "got: {err}"
    );
    let err = coco_to_voc(&dataset, dir.path()).expect_err("dangling category");
    assert!(
        matches!(err, ConvertError::UnknownCategory { .. }),
        "voc: {err}"
    );
    let err = coco_to_oid(&dataset, &dir.path().join("out.csv")).expect_err("dangling category");
    assert!(
        matches!(err, ConvertError::UnknownCategory { .. }),
        "oid: {err}"
    );
}