|
3 | 3 | from keras.models import load_model |
4 | 4 | from keras.preprocessing.image import img_to_array |
5 | 5 | import smtplib |
| 6 | +import os |
| 7 | +from collections import Counter |
| 8 | +from datetime import datetime |
6 | 9 |
|
7 | 10 | # Load the face classifier and emotion classifier |
8 | 11 | face_classifier = cv2.CascadeClassifier('/Users/durgeshthakur/Deep Learning Stuff/Emotion Classification/haarcascade_frontalface_default.xml') |
9 | 12 | classifier = load_model('/Users/durgeshthakur/Deep Learning Stuff/Emotion Classification/Emotion_little_vgg.h5') |
10 | 13 |
|
11 | 14 | # Define class labels for emotions |
12 | 15 | class_labels = ['Angry', 'Happy', 'Neutral', 'Sad', 'Surprise'] |
| 16 | +emotion_count = Counter() |
13 | 17 |
|
14 | 18 | def face_detector(img): |
15 | 19 | # Convert image to grayscale |
@@ -55,24 +59,35 @@ def face_detector(img): |
55 | 59 | # Make a prediction on the ROI and lookup the class |
56 | 60 | preds = classifier.predict(roi)[0] |
57 | 61 | label = class_labels[preds.argmax()] |
| 62 | + emotion_count[label] += 1 # Update emotion count |
58 | 63 | label_position = (x, y) |
59 | 64 | cv2.putText(frame, label, label_position, cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3) |
60 | 65 | else: |
61 | 66 | cv2.putText(frame, 'No Face Found', (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3) |
62 | 67 |
|
| 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 | + |
63 | 73 | cv2.imshow('Emotion Detector', frame) |
64 | 74 |
|
65 | | - if cv2.waitKey(1) & 0xFF == ord('q'): |
| 75 | + key = cv2.waitKey(1) & 0xFF |
| 76 | + if key == ord('q'): # Press 'q' to quit |
66 | 77 | 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}") |
67 | 82 |
|
68 | 83 | # Email notification logic |
69 | 84 | sender_mail = 'sender@fromdomain.com' |
70 | 85 | receivers_mail = ['receiver@todomain.com'] |
71 | 86 | message = """From: From Person <%s> |
72 | 87 | 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) |
76 | 91 |
|
77 | 92 | try: |
78 | 93 | smtpObj = smtplib.SMTP('localhost') |
|
0 commit comments