-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model.py
More file actions
117 lines (101 loc) · 5.29 KB
/
test_model.py
File metadata and controls
117 lines (101 loc) · 5.29 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
from PIL import Image
import os
import joblib
def test_dictionary():
data = {'image':[], 'label':[]}
num_correct, num_photos, num_happy, num_sad, num_angry, num_disgusted, num_surprised, num_scared, num_neutral = 0, 0, 0, 0, 0, 0, 0, 0, 0
num_correct_happy, num_correct_sad, num_correct_angry, num_correct_disgusted, num_correct_surprised, num_correct_scared, num_correct_neutral = 0, 0, 0, 0, 0, 0, 0
current = os.path.dirname(os.path.abspath(__file__))
relative_training_path = os.path.join(current, "dataset", "test")
print("Creating dataframe...")
for image_file in os.listdir(os.path.join(relative_training_path, "angry")):
path = os.path.join(relative_training_path, "angry", image_file)
with Image.open(path) as image:
flat_image = np.array(image).flatten()
data['image'].append(flat_image)
data['label'].append('angry')
num_angry += 1
for image_file in os.listdir(os.path.join(relative_training_path, "disgusted")):
path = os.path.join(relative_training_path, "disgusted", image_file)
with Image.open(path) as image:
flat_image = np.array(image).flatten()
data['image'].append(flat_image)
data['label'].append('disgusted')
num_disgusted += 1
for image_file in os.listdir(os.path.join(relative_training_path, "fearful")):
path = os.path.join(relative_training_path, "fearful", image_file)
with Image.open(path) as image:
flat_image = np.array(image).flatten()
data['image'].append(flat_image)
data['label'].append('fearful')
num_scared += 1
for image_file in os.listdir(os.path.join(relative_training_path, "happy")):
path = os.path.join(relative_training_path, "happy", image_file)
with Image.open(path) as image:
flat_image = np.array(image).flatten()
data['image'].append(flat_image)
data['label'].append('happy')
num_happy += 1
for image_file in os.listdir(os.path.join(relative_training_path, "neutral")):
path = os.path.join(relative_training_path, "neutral", image_file)
with Image.open(path) as image:
flat_image = np.array(image).flatten()
data['image'].append(flat_image)
data['label'].append('neutral')
num_neutral += 1
for image_file in os.listdir(os.path.join(relative_training_path, "sad")):
path = os.path.join(relative_training_path, "sad", image_file)
with Image.open(path) as image:
flat_image = np.array(image).flatten()
data['image'].append(flat_image)
data['label'].append('sad')
num_sad += 1
for image_file in os.listdir(os.path.join(relative_training_path, "surprised")):
path = os.path.join(relative_training_path, "surprised", image_file)
with Image.open(path) as image:
flat_image = np.array(image).flatten()
data['image'].append(flat_image)
data['label'].append('surprised')
num_surprised += 1
print("dataframe created.")
model = joblib.load("emotion_detector.pkl")
print("testing model...")
for i in range(len(data['image'])):
if i == len(data['image'])//2:
print('50% done testing.')
if i == len(data['image'])//4:
print('25% done testing.')
if i == 3 * len(data['image'])//4:
print('75% done testing.')
img = data['image'][i]
reshaped_img = np.array(img.reshape(1, -1))
prediction = model.predict(reshaped_img)
if prediction == data['label'][i]:
num_correct += 1
if prediction == "angry":
num_correct_angry += 1
if prediction == "happy":
num_correct_happy += 1
if prediction == "sad":
num_correct_sad += 1
if prediction == "surprised":
num_correct_surprised += 1
if prediction == "neutral":
num_correct_neutral += 1
if prediction == "fearful":
num_correct_scared += 1
if prediction == "disgusted":
num_correct_disgusted += 1
num_photos += 1
percent_accuracy = num_correct/num_photos * 100
print(f'The model is {percent_accuracy}% accurate overall.')
print(f'{num_correct_angry/num_angry * 100}% accurate for angry, getting {num_correct_angry} correct out of {num_angry}')
print(f'{num_correct_sad/num_sad * 100}% accurate for sad, getting {num_correct_sad} correct out of {num_sad}')
print(f'{num_correct_happy/num_happy * 100}% accurate for happy, getting {num_correct_happy} correct out of {num_happy}')
print(f'{num_correct_surprised/num_surprised * 100}% accurate for surprised, getting {num_correct_surprised} correct out of {num_surprised}')
print(f'{num_correct_neutral/num_neutral * 100}% accurate for neutral, getting {num_correct_neutral} correct out of {num_neutral}')
print(f'{num_correct_disgusted/num_disgusted * 100}% accurate for disgusted, getting {num_correct_disgusted} correct out of {num_disgusted}')
print(f'{num_correct_scared/num_scared * 100}% accurate for scared, getting {num_correct_scared} correct out of {num_scared}')
if __name__ == "__main__":
test_dictionary()