0

这是关于FER2013 数据集的。数据由 48x48 像素的人脸灰度图像组成。CSV 文件包含三列(情感、像素、使用情况),其中使用具有三个值中的任何一个 - 训练、私人测试和公共测试。我想读取像素数组,将它们转换为图像并将它们保存在根据其使用类型命名的相应文件夹中。

我需要可以执行上述操作的 python 代码。以下是我的代码

import pandas as pd
import numpy as np
from PIL import Image

df=pd.read_csv("fer2013.csv")
for rows in df:
  
   arr=np.array(df['pixels'])
   print(arr)
   print(arr.shape)
   img = Image.fromarray(arr.reshape(48,48), 'L')
   img.save("dataset/df['Usage']/img.jpg", "JPEG")

上面的代码显示错误:

无法将大小为 35887 的数组重塑为形状 (48,48)。

4

1 回答 1

0

如果有任何疑问(因为我一直在使用 FER 数据集):

import pandas as pd
import numpy as np
from PIL import Image

df = pd.read_csv('fer2013.csv')
for image_pixels in df.iloc[1:,1]: #column 2 has the pixels. Row 1 is column name.
    image_string = image_pixels.split(' ') #pixels are separated by spaces.
    image_data = np.asarray(image_string, dtype=np.uint8).reshape(48,48)
    img = Image.fromarray(image_data) #final image
于 2019-03-05T14:22:12.413 回答