-
Notifications
You must be signed in to change notification settings - Fork 9
/
csv2Image.py
67 lines (53 loc) · 1.91 KB
/
csv2Image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Courtesy of Kaggle user 'NoBugs'
# Transforms string of numbers in .csv to image and saves it
import numpy as np
import cv2
import pandas as pd
import random
import os
curdir = os.path.abspath(os.path.dirname(__file__))
def gen_record(csvfile,channel):
data = pd.read_csv(csvfile,delimiter=',',dtype='a')
labels = np.array(data['emotion'],np.float)
imagebuffer = np.array(data['pixels'])
images = np.array([np.fromstring(image,np.uint8,sep=' ') for image in imagebuffer])
del imagebuffer
num_shape = int(np.sqrt(images.shape[-1]))
images.shape = (images.shape[0],num_shape,num_shape)
dirs = set(data['Usage'])
subdirs = set(labels)
class_dir = {}
for dr in dirs:
dest = os.path.join(curdir,dr)
class_dir[dr] = dest
if not os.path.exists(dest):
os.mkdir(dest)
data = zip(labels,images,data['Usage'])
for d in data:
label = int(d[0])
if label == 1: # avoid disgust labels
continue
if label > 1: # avoid labels 0, 2, 3, 4, 5, 6,
label -= 1
destdir = os.path.join(class_dir[d[-1]],str(label))
if not os.path.exists(destdir):
os.mkdir(destdir)
img = d[1]
filepath = unique_name(destdir,d[-1])
print('[^_^] Write image to %s' % filepath)
if not filepath:
continue
sig = cv2.imwrite(filepath,img)
if not sig:
print('Error')
exit(-1)
def unique_name(pardir,prefix,suffix='jpg'):
filename = '{0}_{1}.{2}'.format(prefix,random.randint(1,10**8),suffix)
filepath = os.path.join(pardir,filename)
if not os.path.exists(filepath):
return filepath
unique_name(pardir,prefix,suffix)
if __name__ == '__main__':
filename = './fer2013/fer2013.csv'
filename = os.path.join(curdir,filename)
gen_record(filename,1)