Reputation: 77
I've a dataframe as follows:
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
I need to save these informatin for YOLO model type Text file for each image_id
. To do that, I need to save info as
labels x_center y_center w h
Also While saving information into text file, I need to normalize the x_center
and w
by the image width (img_width
) and y_center
and height
by the image height (img_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()
Stuck at this point. :(
Upvotes: 1
Views: 81
Reputation: 17219
We can achieve it as following. Let's create a dummy data frame first.
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
Next, we will groupyby
the image id and iterate over each row.
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')
Upvotes: 1