将信息从 CSV 保存为文本

我有一个数据框如下:


image_id  x   y    w   h   x_center  y_center img_height img_width  lable

01       500 400  250 500    309.4     543.5    2500       4000       0

01       560 430  260 570    306.7     553.4    2200       3000       0

01       540 440  270 580    387.8     563.5    2700       2000       0

02       545 340  250 590    377.8     543.5    2100       2030       1

04       546 240  240 500    367.8     553.5    2300       2000       2

04       586 270  640 400    467.8     556.5    2400       1000       2

我需要为每个 YOLO 模型类型文本文件保存这些信息image_id。为此,我需要将信息保存为


labels x_center y_center w h 

另外,在将信息保存到文本文件中时,我需要按图像宽度 ( )和图像高度 ( )x_center标准化和。wimg_widthy_centerheightimg_height


我的试用


df = pd.read_csv('data.csv')

df_img_id = df.groupby('image_id')


for index, row in df_img_id:

    img_w = row['img_width']

    img_h = row['img_height']


    with open(f'{row['imgage_id']}.txt], 'w+') as f1:

        ft.write()

        f1.close()

卡在这一点上。:(


PIPIONE
浏览 46回答 1
1回答

慕标琳琳

我们可以通过以下方式实现。让我们首先创建一个虚拟数据框。import pandas as pdimport randominfo = {    'image_id': ['01', '01', '01', '02', '04', '04'],    'x':random.sample(range(500, 600), 6),    'y':random.sample(range(200, 500), 6),    'w':random.sample(range(200, 300), 6),    'h':random.sample(range(400, 600), 6),    'x_center':random.sample(range(250, 460), 6),    'y_center':random.sample(range(250, 460), 6),    'img_height':random.sample(range(2100, 3000), 6),     'img_width':random.sample(range(1100, 4000), 6),     'labels':[0,0,0,1,2,2]}df = pd.DataFrame(data=info)df.head()--------------------------    image_id    x    y   w  h   x_center  y_center  img_height  img_width   labels0     01       561  435 290 449 303        318      2105        2806         01     01       583  447 265 427 394        421      2338        2047         02     01       520  417 262 592 429        395      2947        3388         03     02       516  415 214 470 455        319      2649        1594         14     04       522  386 204 514 343        394      2847        1770         2接下来,我们将获取groupyby图像ID并迭代每一行。df_image_id = df.groupby('image_id') # group by idfor _ , row in df_image_id:    for _ , each in row.iterrows(): # iterate each samples within same id         img_w = each['img_width']          img_h = each['img_height']        content = [            each['labels'],             each['x_center']/each['img_width'],            each['y_center']/each['img_height'],            each['w']/each['img_width'],            each['h']/each['img_height']        ]        id = each['image_id']        with open(f'{id}.txt', 'a') as f1:            f1.write(" ".join(str(x) for x in content)+ '\n')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python