[英]how to read csv file and pass its values to a function parameters
I have a csv file having one column and the values it contains like this: here我有一个 csv 文件,其中有一列及其包含的值如下所示: here
and this function还有这个 function
def yolo_to_coco(x_center, y_center, w, h, image_w, image_h):
w = w * image_w
h = h * image_h
x1 = ((2 * x_center * image_w) - w)/2
y1 = ((2 * y_center * image_h) - h)/2
return [x1, y1, w, h]``
when I pass value to this function image_w
and image_h
I am passing from my own it is not in the csv当我将值传递给这个 function
image_w
和image_h
时,我是从我自己传递的,它不在 csv 中
test = yolo_to_coco(53.854, 2912.798, 398.71, 57.202,1024,1024)
the get the required result得到所需的结果
[-148993.02399999998, 2953417.7279999997, 408279.04, 58574.848]
I want to get each value from this file and pass to this function and the store in another csv file.我想从这个文件中获取每个值并传递给这个 function 并存储在另一个 csv 文件中。
You can use dataframe.apply(function)
method.您可以使用
dataframe.apply(function)
方法。 With this method you can apply this function to every row.使用此方法,您可以将此 function 应用于每一行。 Let's create a dummy df.
让我们创建一个虚拟 df。
import pandas as pd
d = {'bbox': [[53.854, 2912.798, 398.71, 57.202],[31, 12,120, 20],[17, 72,170, 720]]}
df = pd.DataFrame(data=d)
But you don't need to create dummy df.但是您不需要创建虚拟 df。 You will read as
你会读作
df = pd.read_csv ('/content/drive/MyDrive/bbox_file.csv')
let's create function. First argument will be row value.让我们创建 function。第一个参数将是行值。 We will specify extra 2 arguments (image_w, image_h)
我们将指定额外的 2 arguments (image_w, image_h)
def yolo_to_coco(bbox, image_w, image_h):
row_list = list(bbox[1:-1].split(","))
row_list = [float(i) for i in row_list]
x_center, y_center, w, h = row_list
w = w * image_w
h = h * image_h
x1 = ((2 * x_center * image_w) - w)/2
y1 = ((2 * y_center * image_h) - h)/2
return [x1, y1, w, h]
Now, We can apply this function to bbox column.You can pass any image_w,image_h values.现在,我们可以将这个 function 应用于 bbox 列。您可以传递任何 image_w、image_h 值。 It will create a pandas.series
它将创建一个 pandas.series
df_coco_series = df["bbox"].apply(yolo_to_coco,image_w = 1024,image_h = 1024)
Finally converting to pandas.dataframe最后转换为 pandas.dataframe
df_coco = df_coco_series.to_frame()
print(df_coco)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.