Fer2013表情识别数据集还原为jpg

Fiona ·
更新时间:2024-11-13
· 808 次阅读

Fer2013数据集下载

https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data

把csv文件分为train、test、val三类

# -*- coding: utf-8 -*- import csv import os database_path = r'/Users/zhaodongyu/Desktop/vision and image/Project' datasets_path = r'/Users/zhaodongyu/Desktop/vision and image/Project/datasets' csv_file = os.path.join(database_path, 'fer2013.csv') train_csv = os.path.join(datasets_path, 'train.csv') val_csv = os.path.join(datasets_path, 'val.csv') test_csv = os.path.join(datasets_path, 'test.csv') with open(csv_file) as f: csvr = csv.reader(f) header = next(csvr) rows = [row for row in csvr] trn = [row[:-1] for row in rows if row[-1] == 'Training'] csv.writer(open(train_csv, 'w+')).writerows([header[:-1]] + trn) print(len(trn)) val = [row[:-1] for row in rows if row[-1] == 'PublicTest'] csv.writer(open(val_csv, 'w+')).writerows([header[:-1]] + val) print(len(val)) tst = [row[:-1] for row in rows if row[-1] == 'PrivateTest'] csv.writer(open(test_csv, 'w+')).writerows([header[:-1]] + tst) print(len(tst))

把每个csv按照标签还原为jpg图像

# -*- coding: utf-8 -*- import csv import os from PIL import Image import numpy as np datasets_path = r'/Users/zhaodongyu/Desktop/vision and image/Project/datasets' train_csv = os.path.join(datasets_path, 'train.csv') val_csv = os.path.join(datasets_path, 'val.csv') test_csv = os.path.join(datasets_path, 'test.csv') train_set = os.path.join(datasets_path, 'train') val_set = os.path.join(datasets_path, 'val') test_set = os.path.join(datasets_path, 'test') for save_path, csv_file in [(train_set, train_csv), (val_set, val_csv), (test_set, test_csv)]: if not os.path.exists(save_path): os.makedirs(save_path) num = 1 with open(csv_file) as f: csvr = csv.reader(f) header = next(csvr) for i, (label, pixel) in enumerate(csvr): pixel = np.asarray([float(p) for p in pixel.split()]).reshape(48, 48) subfolder = os.path.join(save_path, label) if not os.path.exists(subfolder): os.makedirs(subfolder) im = Image.fromarray(pixel).convert('L') image_name = os.path.join(subfolder, '{:05d}.jpg'.format(i)) print(image_name) im.save(image_name)


作者:Slayer_Zhao



数据集 数据 jpg 还原

需要 登录 后方可回复, 如果你还没有账号请 注册新账号