Converting images to csv file in python

前端 未结 4 1534
耶瑟儿~
耶瑟儿~ 2020-12-03 12:51

I have converted my image into a csv file and it\'s like a matrix but I want it to be a single row. How can I convert all of the images in dataset into a csv file (each ima

4条回答
  •  生来不讨喜
    2020-12-03 13:26

    import os
    import pandas as pd
    
    path = 'path-to-the-folder'
    os.chdir(path)
    lists = os.listdir(path)
    labels = []
    file_lst = []
    
    for folder in lists:
        files = os.listdir(path +"/"+folder)
        for file in files:
          path_file = path + "/" + folder + "/" + file
          file_lst.append(path_file)
          labels.append(folder)
    
    dictP_n = {"path": file_lst,
               "label_name": labels,
              "label": labels}   
    
    data  = pd.DataFrame(dictP_n, index = None)
    data = data.sample(frac=1)
    data['label'] = data['label'].replace({"class1": 0, "class2": 1 })
    data.to_csv("path-to-save-location//file_name.csv", index =None)
    

提交回复
热议问题