def adjust_non_voc_csv(csv_file, image_path, image_width, image_height): """ Read relative data and return adjusted frame accordingly. Args: csv_file: .csv file containing the following columns: [image, object_name, object_index, bx, by, bw, bh] image_path: Path prefix to be added. image_width: image width. image_height: image height Returns: pandas DataFrame with the following columns: ['image_path', 'object_name', 'img_width', 'img_height', 'x_min', 'y_min', 'x_max', 'y_max', 'relative_width', 'relative_height', 'object_id'] """ image_path = get_abs_path(image_path, verify=True) coordinates = [] old_frame = pd.read_csv(get_abs_path(csv_file, verify=True)) new_frame = pd.DataFrame() new_frame['image_path'] = old_frame['image'].apply( lambda item: get_abs_path(image_path, item)) new_frame['object_name'] = old_frame['object_name'] new_frame['img_width'] = image_width new_frame['img_height'] = image_height new_frame['relative_width'] = old_frame['bw'] new_frame['relative_height'] = old_frame['bh'] new_frame['object_id'] = old_frame['object_index'] + 1 for index, row in old_frame.iterrows(): image, object_name, object_index, bx, by, bw, bh = row co = ratios_to_coordinates(bx, by, bw, bh, image_width, image_height) coordinates.append(co) ( new_frame['x_min'], new_frame['y_min'], new_frame['x_max'], new_frame['y_max'], ) = np.array(coordinates).T new_frame[['x_min', 'y_min', 'x_max', 'y_max']] = new_frame[['x_min', 'y_min', 'x_max', 'y_max']].astype('int64') print(f'Parsed labels:\n{new_frame["object_name"].value_counts()}') classes = new_frame['object_name'].drop_duplicates() LOGGER.info( f'Adjustment from existing received {len(new_frame)} labels containing ' f'{len(classes)} classes') LOGGER.info(f'Added prefix to images: {image_path}') return new_frame[[ 'image_path', 'object_name', 'img_width', 'img_height', 'x_min', 'y_min', 'x_max', 'y_max', 'relative_width', 'relative_height', 'object_id', ]]
def relative_to_coordinates(self, out_file=None): """ Convert relative coordinates in self.mapping to coordinates. Args: out_file: path to new converted csv. Returns: pandas DataFrame with the new coordinates. """ items_to_save = [] for index, data in self.mapping.iterrows(): image_name, object_name, object_index, bx, by, bw, bh = data x1, y1, x2, y2 = ratios_to_coordinates(bx, by, bw, bh, self.image_width, self.image_height) items_to_save.append([ image_name, x1, y1, x2, y2, object_name, object_index, bx, by, bw, bh, ]) new_data = pd.DataFrame( items_to_save, columns=[ 'image', 'x1', 'y1', 'x2', 'y2', 'object_type', 'object_id', 'bx', 'by', 'bw', 'bh', ], ) new_data[['x1', 'y1', 'x2', 'y2']] = new_data[['x1', 'y1', 'x2', 'y2']].astype('int64') if out_file: new_data.to_csv(out_file, index=False) LOGGER.info(f'Converted labels in {self.labels_file} to coordinates') return new_data