-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui.py
406 lines (336 loc) · 14.4 KB
/
gui.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from Tkinter import *
import tkMessageBox as messagebox
import boto3
import json
import csv
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import tkFileDialog as fd
import os
import PIL.Image
import PIL.ImageTk
import numpy # Import numpy
from drawnow import *
import datetime
import cv2
import time
import face_recognition
window = Tk()
window.title("Student | REAX")
window.geometry('300x400')
spc = Label(window, text = "", font = ("Arial Bold",20))
spc.pack()
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
image = PIL.Image.open(resource_path("logo.png")).resize((200, 200), PIL.Image.ANTIALIAS)
photo = PIL.ImageTk.PhotoImage(image)
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
def create_report():
inf2a = []
inf2ccs = []
inf2cse = []
dmmr = []
#with open('feedback.csv') as csv_file:
with open('feedback.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
line_count += 1
else:
if (row[1] == 'INF2A'):
inf2a.append(row[2])
elif (row[1] == 'INF2C-CS'):
inf2ccs.append(row[2])
elif (row[1] == 'INF2C-SE'):
inf2cse.append(row[2])
elif (row[1] == 'DMMR'):
dmmr.append(row[2])
course_arrays = [inf2a, inf2ccs, inf2cse, dmmr]
for course in course_arrays:
mixedcount = 0
positivecount = 0
negativecount = 0
neutralcount = 0
total = 0
for feedback in course:
total += 1
comprehend = boto3.client(service_name='comprehend', region_name='us-east-1')
result = json.dumps(comprehend.detect_sentiment(Text=feedback, LanguageCode='en'), sort_keys=True, indent=4)
stuff = json.loads(result)
for k in stuff.items():
if 'Sentiment' in k:
result = k[1].lower()
if (result == "positive"):
positivecount += 1
if (result == "negative"):
negativecount += 1
if (result == "neutral"):
neutralcount += 1
if (result == "mixed"):
mixedcount += 1
# for course in course_arrays:
# Data to plot
title = ""
if course == inf2a:
title = "INF2A"
elif course == inf2ccs:
title = "INF2C-CS"
elif course == inf2cse:
title = "INF2C-SE"
elif course == dmmr:
title = "DMMR"
plt.title(title)
labels = 'Positive', 'Negative', 'Mixed', 'Neutral'
sizes = [positivecount * 100/ total, negativecount * 100/ total, mixedcount * 100/ total, neutralcount * 100/ total]
colors = ['yellowgreen', 'lightcoral', 'lightskyblue', 'gold']
plt.pie(sizes)
patches, texts = plt.pie(sizes, colors=colors, startangle=90)
plt.legend(patches, labels, loc="best")
plt.axis('equal')
plt.tight_layout()
#plt.show()
plt.savefig(title + '_report.png')
messagebox.showinfo('MESSAGE', 'Reports are successfully generated!')
exit()
def view_statistics():
inf2a = []
inf2ccs = []
inf2cse = []
dmmr = []
#with open('feedback.csv') as csv_file:
with open('feedback.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
line_count += 1
else:
line_count += 1
if (row[1] == 'INF2A'):
inf2a.append(row[2])
elif (row[1] == 'INF2C-CS'):
inf2ccs.append(row[2])
elif (row[1] == 'INF2C-SE'):
inf2cse.append(row[2])
elif (row[1] == 'DMMR'):
dmmr.append(row[2])
course_arrays = [inf2a, inf2ccs, inf2cse, dmmr]
f = open('output.txt','w')
print >> f, "Course Feedback Statistics for INFORMATICS 2018/19"
print >> f, ""
print >> f, "#######################################"
for course in course_arrays:
mixedcount = 0
positivecount = 0
negativecount = 0
neutralcount = 0
total = 0
for feedback in course:
total += 1
comprehend = boto3.client(service_name='comprehend', region_name='us-east-1')
result = json.dumps(comprehend.detect_sentiment(Text=feedback, LanguageCode='en'), sort_keys=True, indent=4)
stuff = json.loads(result)
for k in stuff.items():
if 'Sentiment' in k:
result = k[1].lower()
if (result == "positive"):
positivecount += 1
if (result == "negative"):
negativecount += 1
if (result == "neutral"):
neutralcount += 1
if (result == "mixed"):
mixedcount += 1
#print("COURSE NAME: " + row[1])
print >>f, ""
if course == inf2a:
print >>f, "COURSE NAME: INF2A"
elif course == inf2ccs:
print >>f, "COURSE NAME: INF2C-CS"
elif course == inf2cse:
print >>f, "COURSE NAME: INF2C-SE"
elif course == dmmr:
print >>f, "COURSE NAME: DMMR"
print >>f, "POSITIVE: " + "%.2f" % (positivecount * 100/ total) + "% " + str(positivecount) + "/" + str(total)
print >>f, "NEGATIVE: " + "%.2f" % (negativecount * 100/ total) + "%" + str(negativecount) + "/" + str(total)
print >>f, "MIXED: " + "%.2f" % (mixedcount * 100/ total) + "%" + str(mixedcount) + "/" + str(total)
print >>f, "NEUTRAL: " + "%.2f" % (neutralcount * 100/ total) + "%" + str(neutralcount) + "/" + str(total)
print >>f, ""
print >>f, "#######################################"
print >> f, ""
print >> f, "Total number of feedback received = " + str(line_count - 1)
messagebox.showinfo('MESSAGE', 'Complete statistics are successfully printed to output.txt!')
BUCKET = "com-oxfordhack-enable"
KEY = "ouput.txt"
IMAGE_ID = KEY
FEATURES_BLACKLIST = ("Landmarks", "Emotions", "Pose", "Quality", "BoundingBox", "Confidence")
COLLECTION = "my-collection-id"
s3 = boto3.client('s3')
file_name = '/Users/Edon/Desktop/oxhack-master/output.txt'
key_name = 'output.txt'
s3.upload_file(file_name, BUCKET, key_name)
def view_all():
os.system("open " + str(resource_path("feedback.csv")))
def facedet():
startTime = datetime.datetime.now()
BUCKET = "com-oxfordhack-enable"
KEY = "test5.jpg"
IMAGE_ID = KEY
FEATURES_BLACKLIST = ("Landmarks", "Emotions", "Pose", "Quality", "BoundingBox", "Confidence")
COLLECTION = "my-collection-id"
s3 = boto3.client('s3')
file_name = '/Users/Edon/Desktop/test5.jpg'
key_name = 'test5.jpg'
def detect_faces(bucket, key, attributes=['ALL'], region="us-east-1"):
rekognition = boto3.client("rekognition", region)
response = rekognition.detect_faces(
Image={
"S3Object": {
"Bucket": bucket,
"Name": key,
}
},
Attributes=attributes,
)
return response['FaceDetails']
def index_faces(bucket, key, collection_id, image_id=None, attributes=(), region="us-east-1"):
rekognition = boto3.client("rekognition", region)
response = rekognition.index_faces(
Image={
"S3Object": {
"Bucket": bucket,
"Name": key,
}
},
CollectionId=collection_id,
ExternalImageId=image_id,
DetectionAttributes=attributes,
)
return response['FaceRecords']
happiness = []
calmness = []
plt.ion() # Tell matplotlib you want interactive mode to plot live data
def makeFig(): # Create a function that makes our desired plot
plt.ylim(0, 100) # Set y min and max values
plt.title('Happiness and Calmness against time') # Plot the title
plt.grid(True) # Turn the grid on
plt.ylabel('Percentage') # Set ylabels
plt.plot(happiness, 'ro-', label='Happiness')
plt.legend(loc='upper right') # plot the legend
plt.plot(calmness, 'b^-', label='Calmness')
plt.ticklabel_format(useOffset=False) # Force matplotlib to NOT autoscale y axis
plt.legend(loc='upper left') # plot the legend
video_capture = cv2.VideoCapture(0)
# Initialize some variables
face_locations = []
face_location = []
face_encodings = []
face_names = []
known_face_names = []
known_face_encodings = []
process_this_frame = True
emotionType = ""
while True:
maxpercent = 0
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
# Only process every other frame of video to save time
if (datetime.datetime.now() - startTime).total_seconds() >= 10:
known_face_encodings = []
known_face_names = []
cv2.imwrite('/Users/Edon/Desktop/test5.jpg', frame)
s3.upload_file(file_name, BUCKET, key_name)
face_location = face_recognition.face_locations(rgb_small_frame)
known_face_encodings = (face_recognition.face_encodings(rgb_small_frame, face_locations))
averageCalm = 0
averageHappy = 0
count = 0
for face in detect_faces(BUCKET, KEY):
count += 1
print "Face ({Confidence}%)".format(**face)
for emotion in face['Emotions']:
if emotion['Type'] == 'CALM':
averageCalm += emotion['Confidence']
elif emotion['Type'] == 'HAPPY':
averageHappy += emotion['Confidence']
calmness.append(averageCalm / count)
happiness.append(averageHappy / count)
for face in detect_faces(BUCKET, KEY):
maxpercent = 0
print "Face ({Confidence}%)".format(**face)
for emotion in face['Emotions']:
print" {Type} : {Confidence}%".format(**emotion)
if maxpercent < emotion['Confidence']:
maxpercent = emotion['Confidence']
emotionType = emotion['Type']
# print" {Type} : {Confidence}%".format(**emotion)
known_face_names.append(emotionType)
print(emotionType)
startTime = datetime.datetime.now()
# Grab a single frame of video
drawnow(makeFig) # Call drawnow to update our live graph
plt.pause(.000001) # Pause Briefly. Important to keep drawnow from crashing
# Resize frame of video to 1/4 size for faster face recognition processing
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding, 0.55)
name = ""
# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
# See if the face is a match for the known face(s)
# If a match was found in known_face_encodings, just use the first one.
face_names.append(name)
process_this_frame = not process_this_frame
# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
exit()
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
lbl = Label(window, text = "", font = ("Arial Bold",20))
lbl.pack()
btn1 = Button(window, text = "Generate Report", command = create_report, font = 30)
btn1.pack()
btn2 = Button(window, text = "View Statistics", command = view_statistics, font = 30)
btn2.pack()
btn3 = Button(window, text = "Full Feedback List", command = view_all, font = 30)
btn3.pack()
btn4 = Button(window, text = "View live graph", command = facedet, font = 30)
btn4.pack()
window.mainloop()