How to convert a yolo darknet format into .csv file
I have a few annotations that is originally in YOLO format. I need to convert it into yolo csv format in order to train with my transformers model.
Sample .csv file I need:
Sample annotation file in CSV format
The csv attributes include: image_id, width, height and coordinates of the image's bounding box.
Any help would be appreciated!

first of all i should say there is no straight way to convert those format into csv. you should read files and parse their data.
Step 1: import libraries
we need to read txt files ( yolo labels ) from a directory and save them into csv. so we need these libraries :
import os
import glob
import pandas as pd
import numpy as np

Step 2: get list of your yolo labels
open directory where you have your yolo labels with OS library and read txt files with glob:
os.chdir(r'D:\karami\Labeled\train1\labels')
myFiles = glob.glob('*.txt')

my labels are in my labels folder so i set that directory to that.
Step 3: read lines and split data
you have your labels list in myFiles variable
you should iterate on it and read the first line of each file and split its data.
in the image you shared we have absolute coordinate of bounding boxes. according to yolo darknet document we have :

object-class x_center y_center width height
x_center,y_center,width,height - float values relative to width and
height of image, it can be equal from (0.0 to 1.0] for example:  =
&lt;absolute_x&gt; / &lt;image_width&gt; or  = &lt;absolute_height&gt; /
&lt;image_height&gt;

you can see full description in here
so we need to multiply our coordinates into width and height of images.
yolo file doesnt contain image size, so you should define them before
after that use your splitted string. then save your data frame. code :
width=1024
height=1024
image_id=0
final_df=[]
for item in myFiles:
    row=[]
    bbox_temp=[]
    with open(item, 'rt') as fd:
        first_line = fd.readline()
        splited = first_line.split();
        
        row.append(image_id)
        row.append(width)
        row.append(height)
        try:
            bbox_temp.append(float(splited[1])*width)
            bbox_temp.append(float(splited[2])*height)
            bbox_temp.append(float(splited[3])*width)
            bbox_temp.append(float(splited[4])*height)
            row.append(bbox_temp)
            final_df.append(row)
        except:
            print(&quot;file is not in YOLO format!&quot;)
df = pd.DataFrame(final_df,columns=['image_id', 'width', 'height','bbox'])
df.to_csv(&quot;saved.csv&quot;,index=False)

here is my csv file :

