From cb428ab0e00dd289ce903e237735ad401225419f Mon Sep 17 00:00:00 2001 From: jamjamjon <505024985@qq.com> Date: Sat, 4 May 2024 12:30:49 +0800 Subject: [PATCH] Add decimal_places for annotator --- src/core/annotator.rs | 31 +++++++++++++++++++++++++++---- src/ys/bbox.rs | 6 +++--- src/ys/keypoint.rs | 6 +++--- src/ys/mbr.rs | 6 +++--- src/ys/polygon.rs | 6 +++--- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/core/annotator.rs b/src/core/annotator.rs index e71a7b9..8f763c3 100644 --- a/src/core/annotator.rs +++ b/src/core/annotator.rs @@ -14,6 +14,7 @@ pub struct Annotator { _scale: f32, // Cope with ab_glyph & imageproc=0.24.0 scale_dy: f32, saveout: Option, + decimal_places: usize, // About mbrs without_mbrs: bool, @@ -65,6 +66,7 @@ impl Default for Annotator { scale_dy: 28., polygons_alpha: 179, saveout: None, + decimal_places: 4, without_bboxes: false, without_bboxes_conf: false, without_bboxes_name: false, @@ -98,6 +100,11 @@ impl Default for Annotator { } impl Annotator { + pub fn with_decimal_places(mut self, x: usize) -> Self { + self.decimal_places = x; + self + } + /// Plotting BBOXes or not pub fn without_bboxes(mut self, x: bool) -> Self { self.without_bboxes = x; @@ -364,7 +371,11 @@ impl Annotator { // label if !self.without_bboxes_name || !self.without_bboxes_conf { - let label = bbox.label(!self.without_bboxes_name, !self.without_bboxes_conf); + let label = bbox.label( + !self.without_bboxes_name, + !self.without_bboxes_conf, + self.decimal_places, + ); self.put_text( img, &label, @@ -395,7 +406,11 @@ impl Annotator { // label if !self.without_mbrs_name || !self.without_mbrs_conf { - let label = mbr.label(!self.without_mbrs_name, !self.without_mbrs_conf); + let label = mbr.label( + !self.without_mbrs_name, + !self.without_mbrs_conf, + self.decimal_places, + ); self.put_text( img, &label, @@ -451,7 +466,11 @@ impl Annotator { if self.with_polygons_name || self.with_polygons_conf { for polygon in polygons.iter() { if let Some((x, y)) = polygon.centroid() { - let label = polygon.label(self.with_polygons_name, self.with_polygons_conf); + let label = polygon.label( + self.with_polygons_name, + self.with_polygons_conf, + self.decimal_places, + ); self.put_text( img, &label, @@ -488,7 +507,11 @@ impl Annotator { // label if self.with_keypoints_name || self.with_keypoints_conf { - let label = kpt.label(self.with_keypoints_name, self.with_keypoints_conf); + let label = kpt.label( + self.with_keypoints_name, + self.with_keypoints_conf, + self.decimal_places, + ); self.put_text( img, &label, diff --git a/src/ys/bbox.rs b/src/ys/bbox.rs index 856cdc3..efc3d2d 100644 --- a/src/ys/bbox.rs +++ b/src/ys/bbox.rs @@ -161,7 +161,7 @@ impl Bbox { self.confidence } - pub fn label(&self, with_name: bool, with_conf: bool) -> String { + pub fn label(&self, with_name: bool, with_conf: bool, decimal_places: usize) -> String { let mut label = String::new(); if with_name { label.push_str( @@ -174,9 +174,9 @@ impl Bbox { } if with_conf { if with_name { - label.push_str(&format!(": {:.4}", self.confidence)); + label.push_str(&format!(": {:.decimal_places$}", self.confidence)); } else { - label.push_str(&format!("{:.4}", self.confidence)); + label.push_str(&format!("{:.decimal_places$}", self.confidence)); } } label diff --git a/src/ys/keypoint.rs b/src/ys/keypoint.rs index 9476882..52e4e38 100644 --- a/src/ys/keypoint.rs +++ b/src/ys/keypoint.rs @@ -215,7 +215,7 @@ impl Keypoint { self.name.as_ref() } - pub fn label(&self, with_name: bool, with_conf: bool) -> String { + pub fn label(&self, with_name: bool, with_conf: bool, decimal_places: usize) -> String { let mut label = String::new(); if with_name { label.push_str( @@ -228,9 +228,9 @@ impl Keypoint { } if with_conf { if with_name { - label.push_str(&format!(": {:.4}", self.confidence)); + label.push_str(&format!(": {:.decimal_places$}", self.confidence)); } else { - label.push_str(&format!("{:.4}", self.confidence)); + label.push_str(&format!("{:.decimal_places$}", self.confidence)); } } label diff --git a/src/ys/mbr.rs b/src/ys/mbr.rs index 29cee23..4fab7fb 100644 --- a/src/ys/mbr.rs +++ b/src/ys/mbr.rs @@ -104,7 +104,7 @@ impl Mbr { self.confidence } - pub fn label(&self, with_name: bool, with_conf: bool) -> String { + pub fn label(&self, with_name: bool, with_conf: bool, decimal_places: usize) -> String { let mut label = String::new(); if with_name { label.push_str( @@ -117,9 +117,9 @@ impl Mbr { } if with_conf { if with_name { - label.push_str(&format!(": {:.4}", self.confidence)); + label.push_str(&format!(": {:.decimal_places$}", self.confidence)); } else { - label.push_str(&format!("{:.4}", self.confidence)); + label.push_str(&format!("{:.decimal_places$}", self.confidence)); } } label diff --git a/src/ys/polygon.rs b/src/ys/polygon.rs index cfebd03..8580211 100644 --- a/src/ys/polygon.rs +++ b/src/ys/polygon.rs @@ -73,7 +73,7 @@ impl Polygon { self.confidence } - pub fn label(&self, with_name: bool, with_conf: bool) -> String { + pub fn label(&self, with_name: bool, with_conf: bool, decimal_places: usize) -> String { let mut label = String::new(); if with_name { label.push_str( @@ -86,9 +86,9 @@ impl Polygon { } if with_conf { if with_name { - label.push_str(&format!(": {:.4}", self.confidence)); + label.push_str(&format!(": {:.decimal_places$}", self.confidence)); } else { - label.push_str(&format!("{:.4}", self.confidence)); + label.push_str(&format!("{:.decimal_places$}", self.confidence)); } } label