File size: 1,046 Bytes
8b6b2e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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()
        
    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