From 854c810c33a29bf764efc2a2fcce20426b320434 Mon Sep 17 00:00:00 2001 From: Stephane Chavin <stephane.chavin@lis-lab.fr> Date: Fri, 31 Jan 2025 10:13:49 +0100 Subject: [PATCH] add boxes correction if overflow --- get_train_annot.py | 9 +++++---- utils.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/get_train_annot.py b/get_train_annot.py index 3f556fa..731cc9d 100755 --- a/get_train_annot.py +++ b/get_train_annot.py @@ -81,10 +81,11 @@ def main(entry, arguments, species_list): y_pxl = 1 - (row.midl_y / (arguments.rf / 2)) height_pxl = (row.max_freq - row.min_freq) / \ (arguments.rf / 2) # take height value in pixels - if height_pxl > 1: - height_pxl = 1 - elif height_pxl > y_pxl * 2: - y_pxl = y_pxl + 0.5 * (height_pxl - y_pxl * 2) + + # Correction if the boxes are corrupted (> 1 or < 0) + x_pxl, width_pxl = correct_box(x_pxl, width_pxl) + y_pxl, height_pxl = correct_box(y_pxl, height_pxl) + # Store the annotation in a DataFrame new_table = pd.DataFrame([[str(species_list[species_list.species == specie].index[0]), x_pxl, y_pxl, width_pxl, height_pxl]], diff --git a/utils.py b/utils.py index 4120728..d2b50a9 100755 --- a/utils.py +++ b/utils.py @@ -569,3 +569,27 @@ def get_set_info(entry): state = 'balanced' proposition = '\u2705 this is good' return state, proposition + + +def correct_box(x,w): + """ + Apply correction if there is an overflow on the annotation box + :param x (float): Ratio of the center of the box + :return w (float): Ratio of the size of the box + :return x,w (float): Corrected values + """ + # Get the beggining and the end of the box + x0, x1 = (x - (w / 2)), (x + (w / 2)) + + # Check the overflow + if x1 > 1 and x0 > 0: + w = 1 - x0 + x = x0 + w/2 + elif x0 < 0 and x1 < 1 : + w = x1 + x = w/2 + elif x0 < 0 and x1 > 1: + w = 1 + x = 0.5 + + return x,w -- GitLab