Skip to content

Commit af404fb

Browse files
authored
Merge pull request #50 from theashishgavade/patch-14
Update Test.py
2 parents 1421330 + fdd232a commit af404fb

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

Test.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
from keras.models import load_model
44
from keras.preprocessing.image import img_to_array
55
import smtplib
6+
import os
7+
from collections import Counter
8+
from datetime import datetime
69

710
# Load the face classifier and emotion classifier
811
face_classifier = cv2.CascadeClassifier('/Users/durgeshthakur/Deep Learning Stuff/Emotion Classification/haarcascade_frontalface_default.xml')
912
classifier = load_model('/Users/durgeshthakur/Deep Learning Stuff/Emotion Classification/Emotion_little_vgg.h5')
1013

1114
# Define class labels for emotions
1215
class_labels = ['Angry', 'Happy', 'Neutral', 'Sad', 'Surprise']
16+
emotion_count = Counter()
1317

1418
def face_detector(img):
1519
# Convert image to grayscale
@@ -55,24 +59,35 @@ def face_detector(img):
5559
# Make a prediction on the ROI and lookup the class
5660
preds = classifier.predict(roi)[0]
5761
label = class_labels[preds.argmax()]
62+
emotion_count[label] += 1 # Update emotion count
5863
label_position = (x, y)
5964
cv2.putText(frame, label, label_position, cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
6065
else:
6166
cv2.putText(frame, 'No Face Found', (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
6267

68+
# Display the most common emotion
69+
if emotion_count:
70+
most_common_emotion = emotion_count.most_common(1)[0][0]
71+
cv2.putText(frame, f'Most Common: {most_common_emotion}', (20, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
72+
6373
cv2.imshow('Emotion Detector', frame)
6474

65-
if cv2.waitKey(1) & 0xFF == ord('q'):
75+
key = cv2.waitKey(1) & 0xFF
76+
if key == ord('q'): # Press 'q' to quit
6677
break
78+
elif key == ord('s'): # Press 's' to take a screenshot
79+
screenshot_filename = f"screenshot_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
80+
cv2.imwrite(screenshot_filename, frame)
81+
print(f"Screenshot saved as {screenshot_filename}")
6782

6883
# Email notification logic
6984
sender_mail = '[email protected]'
7085
receivers_mail = ['[email protected]']
7186
message = """From: From Person <%s>
7287
To: To Person <%s>
73-
Subject: Sending SMTP e-mail
74-
This is a test e-mail message.
75-
""" % (sender_mail, ', '.join(receivers_mail))
88+
Subject: Emotion Detection Notification
89+
Most Common Emotion Detected: %s
90+
""" % (sender_mail, ', '.join(receivers_mail), most_common_emotion)
7691

7792
try:
7893
smtpObj = smtplib.SMTP('localhost')

0 commit comments

Comments
 (0)