0

I am trying to generate some annotation for image files that I have created for training , I am pasting object image on the top of background image and getting the x,y coordinates of the location where the object image is pasted ,

The bounding box for the pasted object is calculated as (x, (x+w), y , (y+h))

box = (x, (w+w), y , (y+h)) # w,h are width and height of the object image

I am converting this to yolo annotation using this function :

def convert_boxes_to_yolo(box, frame):
# frame is a tuple containing background image width and height 
        # x = box[0][0]
        # y = box[0][1]
        # w = box[1][0] - box[0][0]
        # h = box[1][1] - box[0][1]
        x,y,w,h = box

        print( frame.shape)
        xc = float((x + w/2.0) / frame.shape[1])
        yc = float((y + h/2.0) / frame.shape[0])
        wc = float(w / frame.shape[1])
        hc = float(h / frame.shape[0])

        return (str(xc), str(yc), str(wc), str(hc))

and using this function to plot the bounding box , which looks correct :

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('Omen_6_image_generated.png')
dh, dw, _ = img.shape

#dh, dw = (35, 400)

fl = open('Omen_6_image_generated.txt', 'r')
data = fl.readlines()
fl.close()

for dt in data:

    # Split string to float
    _, x, y, w, h = map(float, dt.split())

    # Taken from https://github.com/pjreddie/darknet/blob/810d7f797bdb2f021dbe65d2524c2ff6b8ab5c8b/src/image.c#L283-L291
    # via https://stackoverflow.com/questions/44544471/how-to-get-the-coordinates-of-the-bounding-box-in-yolo-object-detection#comment102178409_44592380
    l = int((x - w / 2) * dw)
    r = int((x + w / 2) * dw)
    t = int((y - h / 2) * dh)
    b = int((y + h / 2) * dh)
    
    if l < 0:
        l = 0
    if r > dw - 1:
        r = dw - 1
    if t < 0:
        t = 0
    if b > dh - 1:
        b = dh - 1

    cv2.rectangle(img, (l, t), (r, b), (0, 0, 255), 1)

image = Image.fromarray(img.astype('uint8'), 'RGB')
image.show()

The bounding box is plotted correctly but the online annotation tools are not able to parse the file.

For example the plotting code correctly plots the bounding box for the shared image and annotation file below but the AI annotation tool like https://www.makesense.ai/ is not able to parse it , also if you look the same image in labelImg results look wrong.

link to both image and yolo_file: https://drive.google.com/drive/folders/13ZTVrzswtcvXRBo6kJAhiITxx-IzOi-_?usp=sharing

0 Answers0