Convert Kitti labels to Yolo

Hi, Trying to convert Kitti label format to Yolo. But after converting the bbox is misplaced. this is kitti bounding box

download (1)

this is conversion code:

def convertToYoloBBox(bbox, size):
# Yolo uses bounding bbox coordinates and size relative to the image size.
# This is taken from https://pjreddie.com/media/files/voc_label.py .
dw = 1. / size[0]
dh = 1. / size[1]
x = (bbox[0] + bbox[1]) / 2.0
y = (bbox[2] + bbox[3]) / 2.0
w = bbox[1] - bbox[0]
h = bbox[3] - bbox[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)


convert =convertToYoloBBox([kitti_bbox[0],kitti_bbox[1],kitti_bbox[2],kitti_bbox[3]],image.shape[:2])

The function does some normalization which is essential for yolo and outputs following:

(0.14763590391908976, 0.3397063758389261, 0.20452591656131477, 0.01810402684563757)

but when i try to check if the normalization is being done correctly with this code:

x = int(convert[0] * image.shape[0])
y = int(convert[1] * image.shape[1])
width = x+int(convert[2] * image.shape[0]) 
height = y+ int(convert[3] * image.shape[1])

cv.rectangle(image, (int(x), int(y)), (int(width), int(height)), (255,0,0), 2 )

Bounding box is misplaced:
download (2)

Any suggestions ? Is conversion fucntion correct? or the problem is in the checking code ?

I reckon the problem is located when doing

x = int(convert[0] * image.shape[0])
y = int(convert[1] * image.shape[1])

As convert[0] contains the x position but centered. Same happens with convert[1]. This center position introduces the offset in the bbox you drawed.

UPDATE
Based on your code, replace as follows

x = int((convert[0] - convert[2]/2))*im.size[0]
y = int((convert[1] - convert[3]/2))*im.size[1]
width = int(convert[2]*im.size[0])
height = int(convert[3]*im.size[1])

cv.rectangle(image, (int(x), int(y)), (int(width), int(height)), (255,0,0), 2 )```