This repository was archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmusic_player.py
More file actions
173 lines (143 loc) · 5.9 KB
/
music_player.py
File metadata and controls
173 lines (143 loc) · 5.9 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
"""A python script that will play a playlist based on facial expression."""
# Import packages.
import numpy as np
import cv2
import dlib
import math
import os.path
# My classes.
from constants import PRED, HAAR, HAAR2, HAAR3, HAAR4
from emotion_recognition import SVM
from face_aligner import FaceAligner
print(__doc__)
# Set Face Detectors.
faceDet = cv2.CascadeClassifier(HAAR)
faceDet2 = cv2.CascadeClassifier(HAAR2)
faceDet3 = cv2.CascadeClassifier(HAAR3)
faceDet4 = cv2.CascadeClassifier(HAAR4)
faceDet5 = dlib.get_frontal_face_detector() # dlib's face detector
# Build the required objects.
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
predictor = dlib.shape_predictor(PRED) # file must be in dir
fa = FaceAligner(predictor, desiredFaceWidth=380)
data = {}
font = cv2.FONT_HERSHEY_SIMPLEX
def get_face_recs(image):
"""Get dlib rectangle around the face."""
detections = faceDet5(image, 1)
haar_detections = []
if not len(detections) > 0: # dlib's detector will work over 50% of the time
haar_detections = faceDet.detectMultiScale(image, scaleFactor=1.1,
minNeighbors=10, minSize=(5, 5),
flags=cv2.CASCADE_SCALE_IMAGE)
haar_detections2 = faceDet2.detectMultiScale(image, scaleFactor=1.1,
minNeighbors=10, minSize=(5, 5),
flags=cv2.CASCADE_SCALE_IMAGE)
haar_detections3 = faceDet3.detectMultiScale(image, scaleFactor=1.1,
minNeighbors=10, minSize=(5, 5),
flags=cv2.CASCADE_SCALE_IMAGE)
haar_detections4 = faceDet4.detectMultiScale(image, scaleFactor=1.1,
minNeighbors=10, minSize=(5, 5),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(haar_detections) > 0:
for (x, y, w, h) in haar_detections:
dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))
detections.append(dlib_rect)
break # if found, no point in making another feature vector
elif len(haar_detections2) > 0:
for (x, y, w, h) in haar_detections2:
dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))
detections.append(dlib_rect)
break
elif len(haar_detections3) > 0:
for (x, y, w, h) in haar_detections3:
dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))
detections.append(dlib_rect)
break
elif len(haar_detections4) > 0:
for (x, y, w, h) in haar_detections4:
dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))
detections.append(dlib_rect)
break
return detections
def get_landmarks(image):
"""As in svms.py, used to create feature vectors to train on."""
detections = get_face_recs(image)
# We may detect 0, 1 or many faces. Loop through each face detected.
for i, j in enumerate(detections):
# Draw facial landmarks with the predictor class.
shape = predictor(image, j)
xlist = []
ylist = []
# Store X and Y coordinates in separate lists.
for i in range(1, 68): # 68 because we're looking for 68 landmarks
xlist.append(float(shape.part(i).x))
ylist.append(float(shape.part(i).y))
# Find both coordinates for the centre of gravity (middle point).
xmean = np.mean(xlist)
ymean = np.mean(ylist)
# Calculate the distance from centre to other points in both axes.
xcentral = [(x-xmean) for x in xlist]
ycentral = [(y-ymean) for y in ylist]
# Condition the vectors.
landmarks_vectorised = []
for x, y, w, z in zip(xcentral, ycentral, xlist, ylist):
landmarks_vectorised.append(w)
landmarks_vectorised.append(z)
meannp = np.asarray((ymean, xmean))
coornp = np.asarray((z, w))
dist = np.linalg.norm(coornp-meannp)
landmarks_vectorised.append(dist)
landmarks_vectorised.append((math.atan2(y, x)*360)/(math.pi*2))
if len(detections) < 1:
return "error"
return landmarks_vectorised
# Build and train the classifier we're using.
SVM = SVM()
if (os.path.isfile('svm.pkl')):
SVM.load()
else:
SVM.train()
SVM.save()
# Open video capture.
cap = cv2.VideoCapture(0)
if cap.isOpened() is False:
print("[Err] Capture failed to open.")
cv2.namedWindow("test")
while (cap.isOpened()):
# Capture frame-by-frame.
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow("test", frame)
if not ret:
break
k = cv2.waitKey(1)
if k % 256 == 27:
# ESC pressed
print("Quit")
break
elif k % 256 == 32:
# SPACE pressed
lm = "error"
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face_rect = []
detections = get_face_recs(frame)
for detection in detections:
clahe_image = clahe.apply(gray)
aligned = fa.align(clahe_image, detection)
lm = get_landmarks(aligned)
face_rect = detection
break
if lm is not "error":
sample = np.array([lm])
sample.reshape(1, -1)
emotion = SVM.predict(sample)
print("Emotion detected: {}".format(emotion.capitalize()))
cv2.putText(frame, emotion.capitalize(),
(50, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 2)
cv2.imshow("Frame", frame)
# TODO : MUSIC PLAYER
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()