Skip to main content

Rust Library Overview

Panlabel can be used as a Rust library in addition to the CLI. This allows you to integrate annotation format conversion, validation, and analysis directly into your applications.

Installation

Add Panlabel to your Cargo.toml:
[dependencies]
panlabel = "0.1"
Or use cargo add:
cargo add panlabel

Library vs CLI

The library provides programmatic access to all CLI functionality:
  • CLI: Best for one-off conversions, validation, and analysis
  • Library: Best for integrating into Rust applications, batch processing, custom workflows
The CLI is actually built on top of the library, so all CLI features are available programmatically.

Quick Start

Here’s a simple example of reading a COCO dataset, validating it, and converting to YOLO:
use panlabel::ir::{io_coco_json, io_yolo};
use panlabel::validation::{validate_dataset, ValidateOptions};
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Read COCO format
    let dataset = io_coco_json::read_coco_json(Path::new("annotations.json"))?;
    
    // Validate the dataset
    let opts = ValidateOptions { strict: false };
    let report = validate_dataset(&dataset, &opts);
    
    if report.error_count() > 0 {
        eprintln!("Validation errors found:\n{}", report);
        return Ok(());
    }
    
    // Convert to YOLO format
    io_yolo::write_yolo_dir(Path::new("output/"), &dataset)?;
    
    println!("Converted {} images successfully!", dataset.images.len());
    Ok(())
}

Core Modules

Panlabel’s library is organized into several key modules:

Intermediate Representation

Core data types for representing annotation datasets

Format I/O

Read and write annotation formats

Validation

Dataset validation and error reporting

Conversion

Conversion reporting and lossiness analysis

Diff

Compare two datasets semantically

Stats

Analyze dataset statistics and quality metrics

Sample

Create dataset subsets with sampling

Documentation

Full API documentation is available on docs.rs:
# Generate and open local documentation
cargo doc --open

Error Handling

All I/O and conversion operations return Result<T, PanlabelError>. The error type provides detailed context:
use panlabel::PanlabelError;

match io_coco_json::read_coco_json(path) {
    Ok(dataset) => { /* process dataset */ },
    Err(PanlabelError::Io(err)) => {
        eprintln!("I/O error: {}", err);
    },
    Err(PanlabelError::CocoJsonParse { path, source }) => {
        eprintln!("Failed to parse COCO JSON at {:?}: {}", path, source);
    },
    Err(err) => {
        eprintln!("Error: {}", err);
    }
}

Next Steps

Build docs developers (and LLMs) love