简体   繁体   中英

How to convert YOLO format annotations to x1, y1, x2, y2 coordinates in Python?

I would like to know how to convert annotations in YOLO format (eg, center_X, center_y, width, height = 0.069824, 0.123535, 0.104492, 0.120117) to x1, y1, x2, y2 coordinates?

Given that the upper-left corner of the image is [0,0]: For the upper-left corner you have to do [x,y] = [center_X, center_Y] - 1/2 * [width, height] . For the bottom-right corner [x,y] = [center_X, center_Y] + 1/2 * [width, height] .

If I recall correctly:

x1 = (center_X-width/2)*image_width
x2 = (center_X+width/2)*image_width
y1 = (center_y-height/2)*image_height
y2 = (center_y+height/2)*image_height
def get_coord(label_file, img_width, img_height):

lfile = open(label_file)
coords = []
all_coords = []

for line in lfile:
    l = line.split(" ")
    
    coords = list(map(float, list(map(float, l[1:5]))))
    x1 = float(img_width) * (2.0 * float(coords[0]) - float(coords[2])) / 2.0
    y1 = float(img_height) * (2.0 * float(coords[1]) - float(coords[3])) / 2.0
    x2 = float(img_width) * (2.0 * float(coords[0]) + float(coords[2])) / 2.0
    y2 = float(img_height) * (2.0 * float(coords[1]) + float(coords[3])) / 2.0
    tmp = [x1, y1, x2, y2]
    all_coords.append(list(map(int, tmp)))
lfile.close()
return  all_coords

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM