use astrs_data::{ArrayRef, AstrsMessage, DataError, DataType, Field, RecordBatch, Result};
use super::{build, read, single_row_column};
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd)]
pub struct BoundingBox {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl BoundingBox {
#[must_use]
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
x,
y,
width,
height,
}
}
#[must_use]
pub const fn to_array(self) -> [f32; 4] {
[self.x, self.y, self.width, self.height]
}
#[must_use]
pub const fn from_array(values: [f32; 4]) -> Self {
Self::new(values[0], values[1], values[2], values[3])
}
#[must_use]
pub fn area(self) -> f32 {
self.width.max(0.0) * self.height.max(0.0)
}
}
impl From<[f32; 4]> for BoundingBox {
fn from(values: [f32; 4]) -> Self {
Self::from_array(values)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd)]
pub struct Keypoint {
pub x: f32,
pub y: f32,
}
impl Keypoint {
#[must_use]
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
#[must_use]
pub const fn to_array(self) -> [f32; 2] {
[self.x, self.y]
}
}
impl From<[f32; 2]> for Keypoint {
fn from(values: [f32; 2]) -> Self {
Self::new(values[0], values[1])
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Detections {
pub boxes: Vec<BoundingBox>,
pub scores: Vec<f32>,
pub labels: Vec<u32>,
}
impl Detections {
pub fn new(boxes: Vec<BoundingBox>, scores: Vec<f32>, labels: Vec<u32>) -> Result<Self> {
let value = Self {
boxes,
scores,
labels,
};
value.check_alignment()?;
Ok(value)
}
#[must_use]
pub const fn empty() -> Self {
Self {
boxes: Vec::new(),
scores: Vec::new(),
labels: Vec::new(),
}
}
#[must_use]
pub fn len(&self) -> usize {
self.boxes.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.boxes.is_empty()
}
pub fn check_alignment(&self) -> Result<()> {
check_three(
("boxes", self.boxes.len()),
("scores", self.scores.len()),
("labels", self.labels.len()),
)
}
#[must_use]
pub fn layout() -> DataType {
DataType::strukt([
Field::required(
"boxes",
DataType::list(Field::required(
"xywh",
DataType::fixed_size_list(Field::required("v", DataType::Float32), 4),
)),
),
Field::required(
"scores",
DataType::list(Field::required("score", DataType::Float32)),
),
Field::required(
"labels",
DataType::list(Field::required("label", DataType::UInt32)),
),
])
}
}
impl AstrsMessage for Detections {
const URN: &'static str = "std/vision/v1/Detections";
fn data_type() -> DataType {
Self::layout()
}
fn to_record_batch(&self) -> Result<RecordBatch> {
self.check_alignment()?;
let flat: Vec<f32> = self
.boxes
.iter()
.flat_map(|value| value.to_array())
.collect();
let column = build::structure(vec![
(
"boxes",
build::fixed_tuple_lists::<f32>("xywh", "v", 4, &[&flat])?,
),
(
"scores",
build::primitive_lists::<f32>("score", &[&self.scores])?,
),
(
"labels",
build::primitive_lists::<u32>("label", &[&self.labels])?,
),
])?;
Ok(RecordBatch::from_payload(column))
}
fn from_record_batch(batch: &RecordBatch) -> Result<Self> {
let column = single_row_column(batch)?;
let (flat, width) = read::fixed_tuple_list_at::<f32>(read::child(column, "boxes")?, 0)?;
if width != 4 {
return Err(DataError::ChildLengthMismatch {
expected: 4,
actual: width,
});
}
let boxes = flat
.as_chunks::<4>()
.0
.iter()
.copied()
.map(BoundingBox::from)
.collect();
let scores = read::primitive_list_at::<f32>(read::child(column, "scores")?, 0)?;
let labels = read::primitive_list_at::<u32>(read::child(column, "labels")?, 0)?;
Self::new(boxes, scores, labels)
}
}
super::impl_from_payload!(Detections);
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Keypoints {
pub points: Vec<Keypoint>,
pub scores: Vec<f32>,
pub labels: Vec<u32>,
}
impl Keypoints {
pub fn new(points: Vec<Keypoint>, scores: Vec<f32>, labels: Vec<u32>) -> Result<Self> {
let value = Self {
points,
scores,
labels,
};
value.check_alignment()?;
Ok(value)
}
#[must_use]
pub fn len(&self) -> usize {
self.points.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.points.is_empty()
}
pub fn check_alignment(&self) -> Result<()> {
check_three(
("points", self.points.len()),
("scores", self.scores.len()),
("labels", self.labels.len()),
)
}
#[must_use]
pub fn layout() -> DataType {
DataType::strukt([
Field::required(
"points",
DataType::list(Field::required(
"xy",
DataType::fixed_size_list(Field::required("v", DataType::Float32), 2),
)),
),
Field::required(
"scores",
DataType::list(Field::required("score", DataType::Float32)),
),
Field::required(
"labels",
DataType::list(Field::required("label", DataType::UInt32)),
),
])
}
}
impl AstrsMessage for Keypoints {
const URN: &'static str = "std/vision/v1/Keypoints";
fn data_type() -> DataType {
Self::layout()
}
fn to_record_batch(&self) -> Result<RecordBatch> {
self.check_alignment()?;
let flat: Vec<f32> = self
.points
.iter()
.flat_map(|value| value.to_array())
.collect();
let column = build::structure(vec![
(
"points",
build::fixed_tuple_lists::<f32>("xy", "v", 2, &[&flat])?,
),
(
"scores",
build::primitive_lists::<f32>("score", &[&self.scores])?,
),
(
"labels",
build::primitive_lists::<u32>("label", &[&self.labels])?,
),
])?;
Ok(RecordBatch::from_payload(column))
}
fn from_record_batch(batch: &RecordBatch) -> Result<Self> {
let column = single_row_column(batch)?;
let (flat, width) = read::fixed_tuple_list_at::<f32>(read::child(column, "points")?, 0)?;
if width != 2 {
return Err(DataError::ChildLengthMismatch {
expected: 2,
actual: width,
});
}
let points = flat
.as_chunks::<2>()
.0
.iter()
.copied()
.map(Keypoint::from)
.collect();
let scores = read::primitive_list_at::<f32>(read::child(column, "scores")?, 0)?;
let labels = read::primitive_list_at::<u32>(read::child(column, "labels")?, 0)?;
Self::new(points, scores, labels)
}
}
super::impl_from_payload!(Keypoints);
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Mask {
pub width: u32,
pub height: u32,
pub data: Vec<u16>,
}
impl Mask {
pub fn new(width: u32, height: u32, data: Vec<u16>) -> Result<Self> {
let value = Self {
width,
height,
data,
};
value.check_dimensions()?;
Ok(value)
}
#[must_use]
pub fn label_at(&self, x: u32, y: u32) -> Option<u16> {
if x >= self.width || y >= self.height {
return None;
}
let index = usize::try_from(y)
.ok()?
.checked_mul(usize::try_from(self.width).ok()?)?
+ usize::try_from(x).ok()?;
self.data.get(index).copied()
}
pub fn check_dimensions(&self) -> Result<()> {
let expected = expected_cells(self.width, self.height);
if self.data.len() == expected {
Ok(())
} else {
Err(DataError::ChildLengthMismatch {
expected,
actual: self.data.len(),
})
}
}
#[must_use]
pub fn layout() -> DataType {
DataType::strukt([
Field::required("width", DataType::UInt32),
Field::required("height", DataType::UInt32),
Field::required(
"data",
DataType::list(Field::required("label", DataType::UInt16)),
),
])
}
}
impl AstrsMessage for Mask {
const URN: &'static str = "std/vision/v1/Mask";
fn data_type() -> DataType {
Self::layout()
}
fn to_record_batch(&self) -> Result<RecordBatch> {
self.check_dimensions()?;
let column = build::structure(vec![
("width", build::primitive::<u32>(&[self.width])),
("height", build::primitive::<u32>(&[self.height])),
(
"data",
build::primitive_lists::<u16>("label", &[&self.data])?,
),
])?;
Ok(RecordBatch::from_payload(column))
}
fn from_record_batch(batch: &RecordBatch) -> Result<Self> {
let column = single_row_column(batch)?;
let width = read::primitive_at::<u32>(read::child(column, "width")?, 0)?;
let height = read::primitive_at::<u32>(read::child(column, "height")?, 0)?;
let data = read::primitive_list_at::<u16>(read::child(column, "data")?, 0)?;
Self::new(width, height, data)
}
}
super::impl_from_payload!(Mask);
fn expected_cells(width: u32, height: u32) -> usize {
usize::try_from(u64::from(width) * u64::from(height)).unwrap_or(usize::MAX)
}
fn check_three(first: (&str, usize), second: (&str, usize), third: (&str, usize)) -> Result<()> {
if first.1 != second.1 {
return Err(DataError::ColumnLengthMismatch {
index: 1,
name: second.0.to_owned(),
expected: first.1,
actual: second.1,
});
}
if first.1 != third.1 {
return Err(DataError::ColumnLengthMismatch {
index: 2,
name: third.0.to_owned(),
expected: first.1,
actual: third.1,
});
}
Ok(())
}
pub fn detection_boxes(batch: &RecordBatch) -> Result<Vec<BoundingBox>> {
let column = single_row_column(batch)?;
let (flat, width) = read::fixed_tuple_list_at::<f32>(read::child(column, "boxes")?, 0)?;
if width != 4 {
return Err(DataError::ChildLengthMismatch {
expected: 4,
actual: width,
});
}
Ok(flat
.as_chunks::<4>()
.0
.iter()
.copied()
.map(BoundingBox::from)
.collect())
}
pub fn mask_labels(batch: &RecordBatch) -> Result<ArrayRef> {
let column = single_row_column(batch)?;
Ok(std::sync::Arc::clone(read::child(column, "data")?))
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::message::assert_registry_layout;
fn detections() -> Detections {
Detections::new(
vec![
BoundingBox::new(0.0, 0.0, 10.0, 20.0),
BoundingBox::new(5.0, 5.0, 1.0, 2.0),
],
vec![0.93, 0.41],
vec![7, 12],
)
.unwrap()
}
#[test]
fn every_vision_type_conforms_to_the_registry() {
assert_registry_layout::<Detections>().unwrap();
assert_registry_layout::<Keypoints>().unwrap();
assert_registry_layout::<Mask>().unwrap();
}
#[test]
fn detections_round_trip() {
let value = detections();
let batch = value.to_record_batch().unwrap();
assert_eq!(batch.num_rows(), 1);
let decoded = Detections::from_record_batch(&batch).unwrap();
assert_eq!(decoded, value);
assert_eq!(decoded.len(), 2);
assert!(!decoded.is_empty());
assert_eq!(decoded.boxes[0].area(), 200.0);
assert_eq!(detection_boxes(&batch).unwrap(), value.boxes);
}
#[test]
fn empty_detections_round_trip() {
let value = Detections::empty();
assert!(value.is_empty());
let batch = value.to_record_batch().unwrap();
assert_eq!(Detections::from_record_batch(&batch).unwrap(), value);
}
#[test]
fn misaligned_detection_lists_are_refused() {
let error =
Detections::new(vec![BoundingBox::default()], vec![0.5, 0.6], vec![1]).unwrap_err();
assert!(matches!(error, DataError::ColumnLengthMismatch { .. }));
assert!(
Detections::new(vec![BoundingBox::default()], vec![0.5], vec![1, 2]).is_err(),
"labels must align too"
);
}
#[test]
fn keypoints_round_trip() {
let value = Keypoints::new(
vec![Keypoint::new(1.0, 2.0), Keypoint::new(3.0, 4.0)],
vec![0.5, 0.25],
vec![0, 1],
)
.unwrap();
let batch = value.to_record_batch().unwrap();
assert_eq!(Keypoints::from_record_batch(&batch).unwrap(), value);
assert_eq!(value.len(), 2);
assert!(!value.is_empty());
assert_eq!(Keypoint::from([9.0, 8.0]).to_array(), [9.0, 8.0]);
assert!(Keypoints::new(vec![Keypoint::default()], vec![], vec![]).is_err());
}
#[test]
fn masks_round_trip_and_check_their_dimensions() {
let value = Mask::new(3, 2, vec![1, 2, 3, 4, 5, 6]).unwrap();
let batch = value.to_record_batch().unwrap();
assert_eq!(Mask::from_record_batch(&batch).unwrap(), value);
assert_eq!(value.label_at(0, 0), Some(1));
assert_eq!(value.label_at(2, 1), Some(6));
assert_eq!(value.label_at(3, 0), None);
assert_eq!(value.label_at(0, 2), None);
assert_eq!(mask_labels(&batch).unwrap().len(), 1);
let error = Mask::new(3, 2, vec![1, 2, 3]).unwrap_err();
assert!(matches!(error, DataError::ChildLengthMismatch { .. }));
assert!(Mask::new(0, 0, Vec::new()).is_ok());
}
#[test]
fn bounding_boxes_convert_both_ways() {
let value = BoundingBox::from([1.0, 2.0, 3.0, 4.0]);
assert_eq!(value.to_array(), [1.0, 2.0, 3.0, 4.0]);
assert_eq!(value.area(), 12.0);
assert_eq!(BoundingBox::new(0.0, 0.0, -1.0, 5.0).area(), 0.0);
assert_eq!(BoundingBox::default().area(), 0.0);
}
#[test]
fn a_wrong_layout_is_refused() {
let mask = Mask::new(1, 1, vec![0]).unwrap().to_record_batch().unwrap();
assert!(Detections::from_record_batch(&mask).is_err());
assert!(Keypoints::from_record_batch(&mask).is_err());
assert!(detection_boxes(&mask).is_err());
let detections = detections().to_record_batch().unwrap();
assert!(Mask::from_record_batch(&detections).is_err());
assert!(
Keypoints::from_record_batch(&detections).is_err(),
"four-wide boxes are not two-wide points"
);
}
}