-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStudentFaceRecording.py
223 lines (139 loc) · 5.71 KB
/
StudentFaceRecording.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import Tkinter as tk
import os
import cv2
from Tkinter import *
from PIL import Image, ImageTk
from tkMessageBox import *
#Global variables
width = 800
height = 600
emotionList=['Angry','Happy','Sad','Fearful','Suprised','Disgusted', 'Normal']
dictionaryForCheckEmotions = {"Angry":False, "Happy":False, "Sad": False, "Fearful": False, "Suprised": False, "Disgusted":False, "Normal":False}
selectingEmotion = ""
originFolder = os.getcwd()
print originFolder
personNumber = 0
personImageNumber = 0
userID = ''
# Taking the user id
def UserIDDoneButtonPressed():
global userID
userID = entryUserID.get()
global personImageNumber
personImageNumber = 0
print userID
# directory checking and creating
def directoryCheckCreate():
for directoryName in emotionList:
folderName = os.path.join(originFolder, directoryName)
if not os.path.isdir(folderName):
os.mkdir(folderName)
def checkPathIsThereIfnCreate():
if not os.path.isdir(originFolder):
os.makedirs(originFolder)
for directoryName in emotionList:
folderName = os.path.join(originFolder, directoryName)
os.mkdir(folderName)
else:
directoryPath.delete(0, END)
directoryPath.insert(END, originFolder)
directoryCheckCreate()
def changeDirButtonAction():
r = askyesno(title = 'Do you want to change directory path?', message = 'After change needed directories under this directory will be created if there is not any.')
if r :
global originFolder
originFolder = directoryPath.get()
checkPathIsThereIfnCreate()
else:
showinfo("Information", "We didn't changed any thing")
os.chdir(originFolder)
#Fotograflari cekilen ogrencinin cekimi bitiginde siradaki ogrenciye gececek ve file ismi olarak siradaki ogrenci yazacak olan yapi
def StudentPictureDone():
global dictionaryForCheckEmotions
global personNumber
global personImageNumber
untakenEmotionList = ""
for controlDictionary in dictionaryForCheckEmotions:
if not dictionaryForCheckEmotions[controlDictionary]:
untakenEmotionList = untakenEmotionList + ", " + controlDictionary
if not untakenEmotionList == "":
showwarning("Warning", "Please,take pictures of %s' emotion(s)!"%untakenEmotionList)
else:
print untakenEmotionList
personNumber += 1
personImageNumber = 0
entryUserID.delete(0, 'end')
dictionaryForCheckEmotions = {"Angry":False, "Happy":False, "Sad": False, "Fearful": False, "Suprised": False, "Disgusted":False, "Normal":False}
# Ogrencinin kacinci fotografini cektigini belirten ve bu fotograflari kaydeden fonksiyon
def takePicturePressed():
global selectingEmotion
if entryUserID.get() == '':
showwarning("User ID is empty", "Please, fill the UserID!")
else:
global personImageNumber
print userID
checkImageIsNone = cv2.imread('%s/%s_%i.png'%(selectingEmotion,userID,personImageNumber))
print checkImageIsNone
if checkImageIsNone is None:
cv2.imwrite('%s/%s_%i.png'%(selectingEmotion,userID,personImageNumber),newImage)
personImageNumber += 1
if not dictionaryForCheckEmotions[selectingEmotion]:
dictionaryForCheckEmotions[selectingEmotion] = True
else:
personImageNumber += 1
takePicturePressed()
def CurSelect(evt):
global selectingEmotion
selectingEmotion = str((emotionListBox.get(emotionListBox.curselection())))
print selectingEmotion
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
#Tkinter ile button kurulumu icin gerekli root variable' i
root = tk.Tk()
root.minsize(800,620)
root.resizable(width = False, height = False)
# list box view
emotionListBox = Listbox(root, width = 11, height = 4, font = ('times',13))
emotionListBox.bind('<<ListboxSelect>>',CurSelect)
for items in emotionList:
emotionListBox.insert(END, items)
emotionListBox.place(x = 97, y = 512)
scrollbar = Scrollbar(root)
scrollbar.place(x = 81, y = 512, height = 95)
emotionListBox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=emotionListBox.yview)
directoryPath = Entry(root, width=60)
directoryPath.place(x = 80, y = 4)
directoryPath.insert(END, originFolder)
changeDir = tk.Button(root, text = 'Change Direction', command=changeDirButtonAction)
changeDir.place(x = 586, y = 0)
takePictureButton = tk.Button(root, text="Take a Picture", command=takePicturePressed)
takePictureButton.place(x = 321, y = 514)
passAnotherStudentButton = tk.Button(root, text="Student Picture Done", command=StudentPictureDone)
passAnotherStudentButton.place(x = 301,y = 550)
labelUserID = Label(root, text = "UserID")
labelUserID.place(x = 525,y = 538)
entryUserID = Entry(root, bd = 3)
entryUserID.place(x = 595, y = 536)
userIDDoneButton = Button(root, text = "Done", command = UserIDDoneButtonPressed)
userIDDoneButton.place(x = 610, y = 570)
root.bind('<Escape>', lambda e: root.quit())
# images taken by the camera
lmain = tk.Label(root, width = 640, height = 480)
lmain.place(x = 80, y = 30)
# Ogrencilerin kendilerini gormelerini saglayan icerisinde imshow metodu yer alan show frame fonksiyonu
def show_frame():
_, frame = cap.read()
frame = cv2.flip(frame, 1)
global newImage
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
newImage = frame
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
imageSaved = cv2image
lmain.after(100, show_frame)
show_frame()
root.mainloop()