Skip to content

Commit

Permalink
Add decimal_places for annotator
Browse files Browse the repository at this point in the history
  • Loading branch information
jamjamjon committed May 4, 2024
1 parent 371a080 commit cb428ab
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
31 changes: 27 additions & 4 deletions src/core/annotator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Annotator {
_scale: f32, // Cope with ab_glyph & imageproc=0.24.0
scale_dy: f32,
saveout: Option<String>,
decimal_places: usize,

// About mbrs
without_mbrs: bool,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/ys/bbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/ys/keypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/ys/mbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/ys/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit cb428ab

Please sign in to comment.