File size: 1,430 Bytes
8b6b2e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906934d
69c8334
 
 
 
 
 
 
 
906934d
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
30
31
32
33
34
35
36
37
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