edgefirst_client/coco/mod.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright © 2025 Au-Zone Technologies. All Rights Reserved.
3
4//! # COCO Dataset Format Support
5//!
6//! This module provides comprehensive support for the COCO (Common Objects in
7//! Context) dataset format, enabling bidirectional conversion between COCO and
8//! EdgeFirst formats.
9//!
10//! ## Supported Workflows
11//!
12//! 1. **COCO → EdgeFirst dataset**: Convert COCO JSON/ZIP or a standard
13//! multi-split directory to Arrow IPC or Parquet
14//! 2. **EdgeFirst dataset → COCO**: Convert Arrow IPC or Parquet back to COCO
15//! JSON
16//! 3. **COCO → Studio**: Import COCO directly into EdgeFirst Studio via API
17//! 4. **Studio → COCO**: Export Studio dataset to COCO format
18//!
19//! ## Scope
20//!
21//! Phase 1 supports:
22//! - Bounding boxes (box2d)
23//! - Polygon segmentation (`polygon`)
24//! - RLE segmentation (PNG-encoded raster `mask`)
25//!
26//! Not yet supported: keypoints, captions, panoptic segmentation.
27//!
28//! ## Example
29//!
30//! ```rust,no_run
31//! use edgefirst_client::coco::{CocoReader, CocoToArrowOptions, coco_to_arrow};
32//!
33//! # async fn example() -> Result<(), edgefirst_client::Error> {
34//! // Read COCO annotations
35//! let reader = CocoReader::new();
36//! let dataset = reader.read_json("annotations/instances_val2017.json")?;
37//! println!(
38//! "Found {} images and {} annotations",
39//! dataset.images.len(),
40//! dataset.annotations.len()
41//! );
42//!
43//! // Convert to EdgeFirst Arrow format
44//! let options = CocoToArrowOptions::default();
45//! let count = coco_to_arrow(
46//! "annotations/instances_val2017.json",
47//! "dataset.arrow",
48//! &options,
49//! None,
50//! )
51//! .await?;
52//! println!("Converted {} samples", count);
53//! # Ok(())
54//! # }
55//! ```
56
57mod convert;
58mod reader;
59mod types;
60pub mod verify;
61mod writer;
62
63#[cfg(feature = "polars")]
64mod arrow;
65
66#[cfg(feature = "polars")]
67pub mod studio;
68
69// Re-export types
70pub use types::{
71 CocoAnnotation, CocoCategory, CocoCompressedRle, CocoDataset, CocoImage, CocoIndex, CocoInfo,
72 CocoLicense, CocoRle, CocoSegmentation,
73};
74
75// Re-export readers/writers
76pub use reader::{
77 CocoReadOptions, CocoReader, infer_group_from_filename, infer_group_from_folder,
78 read_coco_directory,
79};
80pub use writer::{CocoDatasetBuilder, CocoWriteOptions, CocoWriter};
81
82// Re-export conversion functions
83pub use convert::{
84 box2d_to_coco_bbox, calculate_coco_area, coco_bbox_to_box2d, coco_polygon_to_polygon,
85 coco_rle_to_polygon, coco_segmentation_to_mask_data, coco_segmentation_to_polygon,
86 decode_compressed_rle, decode_rle, encode_rle, mask_to_contours, polygon_to_coco_polygon,
87 rle_to_mask_data, validate_coco_bbox,
88};
89
90// Re-export Arrow conversions (feature-gated)
91#[cfg(feature = "polars")]
92pub use arrow::{
93 ArrowToCocoOptions, CocoToArrowOptions, SCHEMA_VERSION, arrow_to_coco, coco_to_arrow,
94 write_dataset,
95};
96
97// Re-export Studio integration (feature-gated)
98#[cfg(feature = "polars")]
99pub use studio::{
100 CocoExportOptions, CocoImportOptions, CocoImportResult, CocoUpdateOptions, CocoUpdateResult,
101 CocoVerifyOptions, export_studio_to_coco, import_coco_to_studio, update_coco_annotations,
102 verify_coco_import,
103};
104
105// Re-export verification types
106pub use verify::{
107 BboxValidationResult, CategoryValidationResult, MaskValidationResult, VerificationResult,
108};
109
110#[cfg(test)]
111mod tests;