mod coco;
mod dataset;
mod decode;
#[cfg(test)]
mod tests;
use crate::InMemDataset;
use crate::transform::MapperDataset;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use thiserror::Error;
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PixelDepth {
U8(u8),
U16(u16),
F32(f32),
}
impl TryFrom<PixelDepth> for u8 {
type Error = &'static str;
fn try_from(value: PixelDepth) -> Result<Self, Self::Error> {
if let PixelDepth::U8(v) = value {
Ok(v)
} else {
Err("Value is not u8")
}
}
}
impl TryFrom<PixelDepth> for u16 {
type Error = &'static str;
fn try_from(value: PixelDepth) -> Result<Self, Self::Error> {
if let PixelDepth::U16(v) = value {
Ok(v)
} else {
Err("Value is not u16")
}
}
}
impl TryFrom<PixelDepth> for f32 {
type Error = &'static str;
fn try_from(value: PixelDepth) -> Result<Self, Self::Error> {
if let PixelDepth::F32(v) = value {
Ok(v)
} else {
Err("Value is not f32")
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Annotation {
Label(usize),
MultiLabel(Vec<usize>),
BoundingBoxes(Vec<BoundingBox>),
SegmentationMask(SegmentationMask),
}
#[derive(Debug, Clone, PartialEq)]
pub struct SegmentationMask {
pub mask: Vec<usize>,
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct BoundingBox {
pub coords: [f32; 4],
pub label: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImageDatasetItem {
pub image: Vec<PixelDepth>,
pub image_width: usize,
pub image_height: usize,
pub annotation: Annotation,
pub image_path: String,
}
#[derive(Deserialize, Serialize, Debug, Clone)]
enum AnnotationRaw {
Label(String),
MultiLabel(Vec<String>),
BoundingBoxes(Vec<BoundingBox>),
SegmentationMask(PathBuf),
}
#[derive(Deserialize, Serialize, Debug, Clone)]
struct ImageDatasetItemRaw {
image_path: PathBuf,
annotation: AnnotationRaw,
}
impl ImageDatasetItemRaw {
fn new<P: AsRef<Path>>(image_path: P, annotation: AnnotationRaw) -> ImageDatasetItemRaw {
ImageDatasetItemRaw {
image_path: image_path.as_ref().to_path_buf(),
annotation,
}
}
}
struct PathToImageDatasetItem {
classes: HashMap<String, usize>,
}
#[derive(Error, Debug)]
pub enum ImageLoaderError {
#[error("unknown: `{0}`")]
Unknown(String),
#[error("I/O error: `{0}`")]
IOError(String),
#[error("Invalid file extension: `{0}`")]
InvalidFileExtensionError(String),
#[error("Parsing error: `{0}`")]
ParsingError(String),
}
type ImageDatasetMapper =
MapperDataset<InMemDataset<ImageDatasetItemRaw>, PathToImageDatasetItem, ImageDatasetItemRaw>;
pub struct ImageFolderDataset {
dataset: ImageDatasetMapper,
}