use super::transform::resample;
use super::{
BBox, CoordFrame, Keypoint, Keypoints, Point2, Polygon, RawRegion, Region, RegionOrigin, Shape,
TransformChain,
};
use crate::backend::BackendId;
use crate::digest::Digest;
use crate::error::{Result, VisionError};
#[derive(Clone, Debug)]
pub struct RegionNormalizer {
image: Digest,
chain: TransformChain,
frame: CoordFrame,
backend: BackendId,
manifest: Option<String>,
}
impl RegionNormalizer {
pub fn new(
image: Digest,
chain: TransformChain,
frame: CoordFrame,
backend: BackendId,
) -> Self {
Self { image, chain, frame, backend, manifest: None }
}
pub fn with_manifest(mut self, manifest: Option<String>) -> Self {
self.manifest = manifest;
self
}
pub fn chain(&self) -> &TransformChain {
&self.chain
}
pub fn origin(&self) -> RegionOrigin {
RegionOrigin {
backend: self.backend.clone(),
manifest: self.manifest.clone(),
frame: self.frame,
chain: self.chain.clone(),
escalated_from: None,
}
}
pub fn shape(&self, raw: &Shape) -> Result<Shape> {
let sent = self.chain.output_size();
let parse =
|message: String| VisionError::Parse { backend: self.backend.to_string(), message };
let to_sent = |x: f32, y: f32| -> Result<[f32; 2]> {
if !x.is_finite() || !y.is_finite() {
return Err(parse(format!("coordinate ({x}, {y}) is not finite")));
}
let p = self.frame.point_to_sent([f64::from(x), f64::from(y)], sent);
Ok([p[0] as f32, p[1] as f32])
};
let in_sent = match raw {
Shape::Point(p) => {
let [x, y] = to_sent(p.x, p.y)?;
Shape::Point(Point2 { x, y })
}
Shape::Box(b) => {
let a = to_sent(b.x0, b.y0)?;
let c = to_sent(b.x1, b.y1)?;
Shape::Box(BBox::new(a[0], a[1], c[0], c[1]))
}
Shape::Polygon(poly) => Shape::Polygon(Polygon {
points: poly.points.iter().map(|p| to_sent(p[0], p[1])).collect::<Result<_>>()?,
}),
Shape::Keypoints(k) => Shape::Keypoints(Keypoints {
skeleton: k.skeleton.clone(),
points: k
.points
.iter()
.map(|kp| {
let [x, y] = to_sent(kp.x, kp.y)?;
Ok(Keypoint { x, y, ..*kp })
})
.collect::<Result<_>>()?,
}),
Shape::Mask(mask) => {
let Some(raster) = self.frame.mask_raster(sent) else {
return Err(parse(format!(
"a mask cannot be given in the {:?} frame; convert it to sent pixels first",
self.frame
)));
};
if (mask.width, mask.height) != raster {
return Err(parse(format!(
"a {}x{} mask does not match the {}x{} raster of its frame",
mask.width, mask.height, raster.0, raster.1
)));
}
match self.frame {
CoordFrame::SentPixels => Shape::Mask(mask.clone()),
frame => Shape::Mask(resample(
mask,
sent,
|p| frame.point_from_sent(p, sent),
|p| frame.point_to_sent(p, sent),
)),
}
}
};
let source = self.chain.shape_to_source(&in_sent)?;
Ok(clamp(source, self.chain.source_width, self.chain.source_height))
}
pub fn region(&self, raw: RawRegion) -> Result<Option<Region>> {
let shape = self.shape(&raw.shape)?;
let empty = match &shape {
Shape::Box(b) => b.is_empty(),
Shape::Mask(m) => m.area() == 0,
_ => false,
};
if empty {
return Ok(None);
}
Ok(Some(Region {
image: self.image,
shape,
label: raw.label,
score: raw.score,
track: raw.track,
origin: self.origin(),
}))
}
pub fn regions(&self, raw: Vec<RawRegion>) -> Result<Vec<Region>> {
let mut regions = Vec::with_capacity(raw.len());
for r in raw {
if let Some(region) = self.region(r)? {
regions.push(region);
}
}
Ok(regions)
}
}
fn clamp(shape: Shape, width: u32, height: u32) -> Shape {
let (w, h) = (width as f32, height as f32);
let pt = |x: f32, y: f32| [x.clamp(0.0, w), y.clamp(0.0, h)];
match shape {
Shape::Point(p) => {
let [x, y] = pt(p.x, p.y);
Shape::Point(Point2 { x, y })
}
Shape::Box(b) => Shape::Box(b.clamped(width, height)),
Shape::Polygon(poly) => {
Shape::Polygon(Polygon { points: poly.points.iter().map(|p| pt(p[0], p[1])).collect() })
}
Shape::Keypoints(k) => Shape::Keypoints(Keypoints {
skeleton: k.skeleton,
points: k
.points
.into_iter()
.map(|kp| {
let [x, y] = pt(kp.x, kp.y);
Keypoint { x, y, ..kp }
})
.collect(),
}),
mask @ Shape::Mask(_) => mask,
}
}