fer2013的csv文件转换为ImageNet格式(以图片格式保存在各自类别的文件夹中)

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)
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是一个对称矩阵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值