-
Notifications
You must be signed in to change notification settings - Fork 3
/
auto.py
68 lines (55 loc) · 1.92 KB
/
auto.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
from imageLoader import imageload
from network import CNN
from time import sleep
import os
import dlib
import json
import sys
# Load the Classifier Network
network = CNN.EmotionClassifier()
network.load_network_state("0.15_lr.npz")
# get our face detector
face_detector = dlib.get_frontal_face_detector()
emotions = ["Contentness", "Happiness", "Sadness", "Surprise", "Fear", "Anger", "Disgust", "Contempt"]
def search_in_files(dir_="/in/"):
for root, name, files in os.walk(os.getcwd() + dir_):
files = sorted([file for file in files if file.endswith(".png") or file.endswith(".jpg")], key=lambda x: x[:-4])
fnames = len(files)*[os.getcwd() + dir_]
for j, f in enumerate(files):
fnames[j] += f
return fnames
return []
def predict_file(filename):
faces, coords = imageload.extract_faces_with_coords(filename, face_detector, face_size=196)
results = network.predict(faces.reshape(-1, 1, 196, 196))
return results, coords
def write_files(files, results, coords):
try:
for i, file in enumerate(files):
f = open(file[:-4]+str(i)+".out", 'w+')
obj = [int(results[i]), int(coords[i][0]), int(coords[i][1]), int(coords[i][2]), int(coords[i][3])]
print(results)
json.dump(obj, f)
except Exception:
for i, file in enumerate(files):
f = open(file[:-4]+str(i)+".out", 'w+')
obj = [8, 0, 0, 0, 0]
print("Face not detected")
json.dump(obj, f)
def get_emotion_from_code(emotion=0):
return emotions[emotion]
def move_files(files):
for file in files:
os.remove(file)
i = 1
print("Ready..")
while True:
files = serach_in_files()
for f in files:
try:
emotions, coords = predict_file(f)
write_files([f], emotions, coords)
except Exception:
print("Error in facial prediction")
move_files(files)
sleep(0.05)