Repository navigation
Expand file tree
/
Copy pathemo.py
More file actions
185 lines (162 loc) · 6.36 KB
/
Copy pathemo.py
File metadata and controls
185 lines (162 loc) · 6.36 KB
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
import cv2
import torch
import torchvision.transforms as transforms
from torchvision import models
from PIL import Image, ImageDraw
import mediapipe as mp
import numpy as np
import time
import sqlite3
import json
import uuid
from datetime import datetime
class SQLiteDatabase:
def __init__(self, db_path="emotion_detection.db"):
self.db_path = db_path
self.setup_database()
def setup_database(self):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
# Create sessions table
cursor.execute('''
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
start_time TIMESTAMP,
device_id TEXT
)
''')
# Create detections table
cursor.execute('''
CREATE TABLE IF NOT EXISTS detections (
detection_id TEXT PRIMARY KEY,
session_id TEXT,
timestamp TIMESTAMP,
emotion TEXT,
confidence REAL,
face_location TEXT,
FOREIGN KEY (session_id) REFERENCES sessions (session_id)
)
''')
conn.commit()
def create_session(self):
session_id = str(uuid.uuid4())
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO sessions (session_id, start_time, device_id)
VALUES (?, ?, ?)
''', (session_id, datetime.now(), 'WEBCAM-0'))
conn.commit()
return session_id
def store_detection(self, session_id, emotion, confidence, face_location):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO detections
(detection_id, session_id, timestamp, emotion, confidence, face_location)
VALUES (?, ?, ?, ?, ?, ?)
''', (
str(uuid.uuid4()),
session_id,
datetime.now(),
emotion,
float(confidence),
json.dumps(face_location)
))
conn.commit()
def get_recent_detections(self, limit=10):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT timestamp, emotion, confidence
FROM detections
ORDER BY timestamp DESC
LIMIT ?
''', (limit,))
return cursor.fetchall()
class EmotionRecognitionModel:
def __init__(self):
self.model = models.resnet18(pretrained=True)
self.model.fc = torch.nn.Linear(self.model.fc.in_features, 7)
self.model.eval()
self.emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
def predict(self, image):
try:
with torch.no_grad():
image = image.convert('RGB')
input_tensor = self.transform(image).unsqueeze(0)
output = self.model(input_tensor)
confidence, predicted = torch.max(output, 1)
return self.emotion_labels[predicted.item()], confidence.item()
except Exception as e:
print(f"Prediction error: {e}")
return "Unknown", 0.0
class EmotionDetectionSystem:
def __init__(self):
self.emotion_model = EmotionRecognitionModel()
self.face_detection = mp.solutions.face_detection.FaceDetection(
min_detection_confidence=0.2)
self.db = SQLiteDatabase()
self.session_id = self.db.create_session()
def process_frame(self, frame):
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(rgb_frame)
results = self.face_detection.process(rgb_frame)
if results.detections:
for detection in results.detections:
bboxC = detection.location_data.relative_bounding_box
h, w, _ = frame.shape
x = max(0, int(bboxC.xmin * w))
y = max(0, int(bboxC.ymin * h))
width = min(int(bboxC.width * w), w - x)
height = min(int(bboxC.height * h), h - y)
face = pil_image.crop((x, y, x + width, y + height))
emotion, confidence = self.emotion_model.predict(face)
face_location = {
"x": x,
"y": y,
"width": width,
"height": height
}
self.db.store_detection(
self.session_id,
emotion,
confidence,
face_location
)
draw = ImageDraw.Draw(pil_image)
draw.rectangle([x, y, x + width, y + height],
outline="green", width=2)
draw.text((x, y - 10),
f"{emotion} ({confidence:.2f})",
fill="green")
return cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
def run(self):
cap = cv2.VideoCapture(0)
try:
while True:
ret, frame = cap.read()
if not ret:
break
processed_frame = self.process_frame(frame)
cv2.imshow('Emotion Recognition', processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cap.release()
cv2.destroyAllWindows()
self.face_detection.close()
def main():
try:
system = EmotionDetectionSystem()
system.run()
except Exception as e:
print(f"System error: {e}")
if __name__ == "__main__":
main()