-
Notifications
You must be signed in to change notification settings - Fork 308
/
cvs_to_numpy.py
87 lines (72 loc) · 2.59 KB
/
cvs_to_numpy.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from constants import CASC_PATH, SIZE_FACE, EMOTIONS, SAVE_DATASET_IMAGES_FILENAME, SAVE_DATASET_LABELS_FILENAME, SAVE_DIRECTORY, DATASET_CSV_FILENAME
import cv2
import pandas as pd
import numpy as np
from PIL import Image
from os.path import join
cascade_classifier = cv2.CascadeClassifier(CASC_PATH)
def format_image(image):
if len(image.shape) > 2 and image.shape[2] == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
image = cv2.imdecode(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
gray_border = np.zeros((150, 150), np.uint8)
gray_border[:, :] = 200
gray_border[
int((150 / 2) - (SIZE_FACE / 2)): int((150 / 2) + (SIZE_FACE / 2)),
int((150 / 2) - (SIZE_FACE / 2)): int((150 / 2) + (SIZE_FACE / 2))
] = image
image = gray_border
faces = cascade_classifier.detectMultiScale(
image,
scaleFactor=1.3,
minNeighbors=5
)
# None is we don't found an image
if not len(faces) > 0:
return None
max_area_face = faces[0]
for face in faces:
if face[2] * face[3] > max_area_face[2] * max_area_face[3]:
max_area_face = face
# Chop image to face
face = max_area_face
image = image[face[1]:(face[1] + face[2]), face[0]:(face[0] + face[3])]
# Resize image to network size
try:
image = cv2.resize(image, (SIZE_FACE, SIZE_FACE),
interpolation=cv2.INTER_CUBIC) / 255.
except Exception:
print("[+] Problem during resize")
return None
return image
def emotion_to_vec(x):
d = np.zeros(len(EMOTIONS))
d[x] = 1.0
return d
def flip_image(image):
return cv2.flip(image, 1)
def data_to_image(data):
data_image = np.fromstring(
str(data), dtype=np.uint8, sep=' ').reshape((SIZE_FACE, SIZE_FACE))
data_image = Image.fromarray(data_image).convert('RGB')
data_image = np.array(data_image)[:, :, ::-1].copy()
data_image = format_image(data_image)
return data_image
data = pd.read_csv(join(SAVE_DIRECTORY, DATASET_CSV_FILENAME))
labels = []
images = []
index = 1
total = data.shape[0]
for index, row in data.iterrows():
emotion = emotion_to_vec(row['emotion'])
image = data_to_image(row['pixels'])
if image is not None:
labels.append(emotion)
images.append(image)
# images.append(flip_image(image))
index += 1
print("Progress: {}/{} {:.2f}%".format(index, total, index * 100.0 / total))
print("Total: " + str(len(images)))
np.save(join(SAVE_DIRECTORY, SAVE_DATASET_IMAGES_FILENAME), images)
np.save(join(SAVE_DIRECTORY, SAVE_DATASET_LABELS_FILENAME), labels)