如何根据存储在csv文件中的id定义数据帧来读取图像?

2024-09-30 12:16:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建pandas dataframe并根据保存在csv文件中的图像ID从目录中读取图像。csv文件包括稍后用于回归任务的图像ID和标签。加载脚本时,图像不会存储在image变量中。有人能帮我更正这个脚本吗

labelPath =  "E:/Notebooks/labels.csv" #path for the csv file
cols = ["id","label"] #defining the columns name in the csv file
all_df = pd.read_csv(labelPath, sep=",", header=None, names=cols)

def read_img(self,id, folder='image_files'):
        
    images=[]
    """
    Read image by it's id
    """
    file='E:/Notebooks/' + folder + '/' + str(id) + '.tif'
    for filepath in os.listdir(file):
        images.append(cv2.imread(file+'/{0}'.format(filepath),flags=(cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)))
    return images

更新:csv文件中数据的示例

^{tb1}$

Tags: 文件csvthe图像image脚本idfor

热门问题