Add decimal_places for annotator (#13)

This commit is contained in:
Jamjamjon 2024-05-04 12:48:55 +08:00 committed by GitHub
parent 371a08011f
commit 246e6091cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 16 deletions

View File

@ -14,6 +14,7 @@ pub struct Annotator {
_scale: f32, // Cope with ab_glyph & imageproc=0.24.0 _scale: f32, // Cope with ab_glyph & imageproc=0.24.0
scale_dy: f32, scale_dy: f32,
saveout: Option<String>, saveout: Option<String>,
decimal_places: usize,
// About mbrs // About mbrs
without_mbrs: bool, without_mbrs: bool,
@ -65,6 +66,7 @@ impl Default for Annotator {
scale_dy: 28., scale_dy: 28.,
polygons_alpha: 179, polygons_alpha: 179,
saveout: None, saveout: None,
decimal_places: 4,
without_bboxes: false, without_bboxes: false,
without_bboxes_conf: false, without_bboxes_conf: false,
without_bboxes_name: false, without_bboxes_name: false,
@ -98,6 +100,11 @@ impl Default for Annotator {
} }
impl Annotator { impl Annotator {
pub fn with_decimal_places(mut self, x: usize) -> Self {
self.decimal_places = x;
self
}
/// Plotting BBOXes or not /// Plotting BBOXes or not
pub fn without_bboxes(mut self, x: bool) -> Self { pub fn without_bboxes(mut self, x: bool) -> Self {
self.without_bboxes = x; self.without_bboxes = x;
@ -364,7 +371,11 @@ impl Annotator {
// label // label
if !self.without_bboxes_name || !self.without_bboxes_conf { 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( self.put_text(
img, img,
&label, &label,
@ -395,7 +406,11 @@ impl Annotator {
// label // label
if !self.without_mbrs_name || !self.without_mbrs_conf { 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( self.put_text(
img, img,
&label, &label,
@ -451,7 +466,11 @@ impl Annotator {
if self.with_polygons_name || self.with_polygons_conf { if self.with_polygons_name || self.with_polygons_conf {
for polygon in polygons.iter() { for polygon in polygons.iter() {
if let Some((x, y)) = polygon.centroid() { 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( self.put_text(
img, img,
&label, &label,
@ -488,7 +507,11 @@ impl Annotator {
// label // label
if self.with_keypoints_name || self.with_keypoints_conf { 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( self.put_text(
img, img,
&label, &label,

View File

@ -161,7 +161,7 @@ impl Bbox {
self.confidence 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(); let mut label = String::new();
if with_name { if with_name {
label.push_str( label.push_str(
@ -174,9 +174,9 @@ impl Bbox {
} }
if with_conf { if with_conf {
if with_name { if with_name {
label.push_str(&format!(": {:.4}", self.confidence)); label.push_str(&format!(": {:.decimal_places$}", self.confidence));
} else { } else {
label.push_str(&format!("{:.4}", self.confidence)); label.push_str(&format!("{:.decimal_places$}", self.confidence));
} }
} }
label label

View File

@ -215,7 +215,7 @@ impl Keypoint {
self.name.as_ref() 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(); let mut label = String::new();
if with_name { if with_name {
label.push_str( label.push_str(
@ -228,9 +228,9 @@ impl Keypoint {
} }
if with_conf { if with_conf {
if with_name { if with_name {
label.push_str(&format!(": {:.4}", self.confidence)); label.push_str(&format!(": {:.decimal_places$}", self.confidence));
} else { } else {
label.push_str(&format!("{:.4}", self.confidence)); label.push_str(&format!("{:.decimal_places$}", self.confidence));
} }
} }
label label

View File

@ -104,7 +104,7 @@ impl Mbr {
self.confidence 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(); let mut label = String::new();
if with_name { if with_name {
label.push_str( label.push_str(
@ -117,9 +117,9 @@ impl Mbr {
} }
if with_conf { if with_conf {
if with_name { if with_name {
label.push_str(&format!(": {:.4}", self.confidence)); label.push_str(&format!(": {:.decimal_places$}", self.confidence));
} else { } else {
label.push_str(&format!("{:.4}", self.confidence)); label.push_str(&format!("{:.decimal_places$}", self.confidence));
} }
} }
label label

View File

@ -73,7 +73,7 @@ impl Polygon {
self.confidence 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(); let mut label = String::new();
if with_name { if with_name {
label.push_str( label.push_str(
@ -86,9 +86,9 @@ impl Polygon {
} }
if with_conf { if with_conf {
if with_name { if with_name {
label.push_str(&format!(": {:.4}", self.confidence)); label.push_str(&format!(": {:.decimal_places$}", self.confidence));
} else { } else {
label.push_str(&format!("{:.4}", self.confidence)); label.push_str(&format!("{:.decimal_places$}", self.confidence));
} }
} }
label label