-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.py
138 lines (113 loc) · 3.96 KB
/
capture.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
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
import cv2
import argparse
import time
import os
import Update_Model
import glob
import random
import eel
#import winsound
frequency=2500
duration=1000
eel.init('WEB')
emotions=["angry", "happy", "sad", "neutral"]
fishface = cv2.face.FisherFaceRecognizer_create()
font = cv2.FONT_HERSHEY_SIMPLEX
'''try:
fishface.load("model.xml")
except:
print("No trained model found... --update will create one.")'''
parser=argparse.ArgumentParser(description="Options for emotions based music player(Updating the model)")
parser.add_argument("--update", help="Call for taking new images and retraining the model.", action="store_true")
args=parser.parse_args()
facedict={}
video_capture=cv2.VideoCapture(0)
facecascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def crop(clahe_image, face):
for (x, y, w, h) in face:
faceslice=clahe_image[y:y+h, x:x+w]
faceslice=cv2.resize(faceslice, (350, 350))
facedict["face%s" %(len(facedict)+1)]=faceslice
return faceslice
def grab_face():
ret, frame=video_capture.read()
#cv2.imshow("Video", frame)
cv2.imwrite('test.jpg', frame)
cv2.imwrite("images/main%s.jpg" %count, frame)
gray=cv2.imread('test.jpg',0)
#gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
clahe=cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe_image=clahe.apply(gray)
return clahe_image
def detect_face():
clahe_image=grab_face()
face=facecascade.detectMultiScale(clahe_image, scaleFactor=1.1, minNeighbors=15, minSize=(10, 10), flags=cv2.CASCADE_SCALE_IMAGE)
if len(face)>=1:
faceslice=crop(clahe_image, face)
#return faceslice
else:
print("No/Multiple faces detected!!, passing over the frame")
def save_face(emotion):
print("\n\nLook "+emotion+" untill the timer expires and keep the same emotion for some time.")
#winsound.Beep(frequency, duration)
print('\a')
for i in range(0, 5):
print(5-i)
time.sleep(1)
while len(facedict.keys())<16:
detect_face()
for i in facedict.keys():
path, dirs, files = next(os.walk("dataset/%s" %emotion))
file_count = len(files)+1
cv2.imwrite("dataset/%s/%s.jpg" %(emotion, (file_count)), facedict[i])
facedict.clear()
def update_model(emotions):
print("Update mode for model is ready")
checkForFolders(emotions)
for i in range(0, len(emotions)):
save_face(emotions[i])
print("Collected the images, looking nice! Now updating the model...")
Update_Model.update(emotions)
print("Model train successful!!")
def checkForFolders(emotions):
for emotion in emotions:
if os.path.exists("dataset/%s" %emotion):
pass
else:
os.makedirs("dataset/%s" %emotion)
def identify_emotions():
prediction=[]
confidence=[]
for i in facedict.keys():
pred, conf=fishface.predict(facedict[i])
cv2.imwrite("images/%s.jpg" %i, facedict[i])
prediction.append(pred)
confidence.append(conf)
output=emotions[max(set(prediction), key=prediction.count)]
print("You seem to be %s" %output)
facedict.clear()
return output;
#songlist=[]
#songlist=sorted(glob.glob("songs/%s/*" %output))
#random.shuffle(songlist)
#os.startfile(songlist[0])
count=0
@eel.expose
def getEmotion():
count=0
while True:
count=count+1
detect_face()
if args.update:
update_model(emotions)
break
elif count==10:
fishface.read("model.xml")
return identify_emotions()
break
#eel.start('main.html', options=web_app_options)
#options={'host':'file', 'port': '//'}
eel.start('main.html')#//WD_INNOVATIVE//main.html')
#, options)
'''van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from: http://www.paulvangent.com/2016/06/30/making-an-emotion-aware-music-player/
'''