How to convert Bounding Box coordinates to Yolo Coordinates?

883 Views Asked by At

I am trying to convert Bounding box coordinates to Yolo coordinates. The bounding box coordinates are not in the typical format. They look like this:

1,-1,855,884,94,195,1,-1,-1,-1
1,-1,1269,830,103,202,0,-1,-1,-1
1,-1,1023,909,86,170,0,-1,-1,-1
1,-1,879,681,76,191,0,-1,-1,-1

How do I use the 1s, -1s, and 0s to convert these coordinates to Yolo format?

I tried this code to convert them to Yolo:

def convert(filename_str, coords):
    os.chdir("..")
    image = cv2.imread(filename_str + ".jpg")
    coords[2] -= coords[0]
    coords[3] -= coords[1]
    x_diff = int(coords[2]/2)
    y_diff = int(coords[3]/2)
    coords[0] = coords[0]+x_diff
    coords[1] = coords[1]+y_diff
    coords[0] /= int(image.shape[1])
    coords[1] /= int(image.shape[0])
    coords[2] /= int(image.shape[1])
    coords[3] /= int(image.shape[0])
    os.chdir("Label")
    return coords

I get negative Yolo coordinates with this format:

0 0.2871825876662636 0.5 -0.46009673518742444 -0.637962962962963
0 0.4147521160822249 0.4777777777777778 -0.7049576783555018 -0.5814814814814815
0 0.3355501813784764 0.5 -0.5665054413542926 -0.6842592592592592

Thanks in advance

1

There are 1 best solutions below

0
On

Try this:

def convert(size, box):
    dw = 1./size[0]
    dh = 1./size[1]
    x = (box[0] + box[1])/2.0
    y = (box[2] + box[3])/2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

im=Image.open(img_path)
w= int(im.size[0])
h= int(im.size[1])


print(xmin, xmax, ymin, ymax) #define your x,y coordinates
b = (xmin, xmax, ymin, ymax)
bb = convert((w,h), b)