1、fer2013数据集下载
fer2013源文件fer2013.csv的下载:
链接:BD网盘 提取码:nq6h
2、转换代码
代码文件不传CSDN资源,直接给代码
# -*- encoding: utf-8 -*-
"""
@File : csv2img.py
@Contact : thgpddl@163.com
@Modify Time @Author @Version @Desciption
------------ ------- -------- -----------
2022/5/24 20:10 thgpddl 1.0 None
"""
import os
import cv2
import pandas as pd
import numpy as np
from tqdm import tqdm # 进度条可视化
def prepare_data(data):
image_array = np.zeros(shape=(len(data), 48, 48), dtype="uint8")
for i, row in enumerate(data.index):
image = np.fromstring(data.loc[row, 'pixels'], dtype=int, sep=' ')
image = np.reshape(image, (48, 48))
image_array[i] = image
return image_array
def process(csv_path="fer2013.csv", out_dir_path="Fer2013Image"):
fer2013 = pd.read_csv(csv_path)
img_count=0
for i in range(7):
print("emotion class:", i)
clas = fer2013[fer2013['emotion'] == i] # 得到csv中类别为i的数据
image_array = prepare_data(clas) # 整理图像数据,返回[samples, H, W]格式的图像,意味着有samples张图片,每张为H*W
dir_path = os.path.join(out_dir_path, str(i)) # 定义类别为i的子文件夹路径
assert not os.path.exists(dir_path),print("输出文件夹已存在,请删除或指定新的输出文件夹")
os.makedirs(dir_path) # 创建类别为i的子文件夹
bs, h, w = image_array.shape
for b in tqdm(range(bs)): # 遍历每一个sample
img = image_array[b] # img.shape=48*48
cv2.imwrite(os.path.join(dir_path, str(b) + '.jpg'), img)
img_count+=bs
print("共有图片{}张\t处理了图片{}张".format(fer2013.shape[0],img_count))
if __name__=="__main__":
csv_path="fer2013.csv" # csv文件路径
out_dir_path="Fer2013Image" # 输出文件夹的路径
process(csv_path=csv_path,out_dir_path=out_dir_path)