亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將信息從 CSV 保存為文本

將信息從 CSV 保存為文本

PIPIONE 2023-12-29 15:55:00
我有一個數據框如下:image_id  x   y    w   h   x_center  y_center img_height img_width  lable01       500 400  250 500    309.4     543.5    2500       4000       001       560 430  260 570    306.7     553.4    2200       3000       001       540 440  270 580    387.8     563.5    2700       2000       002       545 340  250 590    377.8     543.5    2100       2030       104       546 240  240 500    367.8     553.5    2300       2000       204       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()卡在這一點上。:(
查看完整描述

1 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

我們可以通過以下方式實現。讓我們首先創建一個虛擬數據框。


import pandas as pd

import random


info = {

    '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   labels

0     01       561  435 290 449 303        318      2105        2806         0

1     01       583  447 265 427 394        421      2338        2047         0

2     01       520  417 262 592 429        395      2947        3388         0

3     02       516  415 214 470 455        319      2649        1594         1

4     04       522  386 204 514 343        394      2847        1770         2

接下來,我們將獲取groupyby圖像ID并迭代每一行。


df_image_id = df.groupby('image_id') # group by id


for _ , 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')


查看完整回答
反對 回復 2023-12-29
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號