def yolov5_to_image_coordinates(text_file, image_width=640, image_height=640): """ Convert YOLOv5 bounding box coordinates to normal image coordinates. Args: box (tuple): YOLOv5 bounding box coordinates in the format (x_center, y_center, width, height). image_width (int): Width of the image. image_height (int): Height of the image. Returns: tuple: Normal image coordinates in the format (x_min, y_min, x_max, y_max). """ with open(text_file, 'r') as f: xywh_text = f.read() xywh_splitted = xywh_text.split('\n') if xywh_splitted[-1] == '': xywh_splitted = xywh_text.split('\n')[0] else: conf_list = [float(i.split(' ')[-1]) for i in xywh_splitted] max_conf_idx = conf_list.index(max(conf_list)) xywh_splitted = xywh_text.split('\n')[max_conf_idx] yolo_box = [float(i) for i in xywh_splitted.split(' ')[1:-1]] # yolo_box = [float(i) for i in xywh_text.replace('\n', '').split(' ')[1:]] x_center, y_center, width, height = yolo_box # Convert from normalized to absolute coordinates x_min = int((x_center - width / 2) * image_width) y_min = int((y_center - height / 2) * image_height) x_max = int((x_center + width / 2) * image_width) y_max = int((y_center + height / 2) * image_height) x1, y1, x2, y2 = y_min, x_min, y_max, x_max return x1, y1, x2, y2