Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,29 @@ import torch
|
|
| 6 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
| 7 |
from keras.models import load_model
|
| 8 |
from PIL import Image
|
| 9 |
-
import
|
| 10 |
import os
|
| 11 |
import tempfile
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Page title
|
| 20 |
st.markdown("<h1 style='text-align: center;'>Emotion Detection with Face Recognition</h1>", unsafe_allow_html=True)
|
|
@@ -97,16 +111,17 @@ def process_frame(frame):
|
|
| 97 |
|
| 98 |
name = recognize_face(face_embedding)
|
| 99 |
|
| 100 |
-
# Save record in
|
| 101 |
if name != "Unknown":
|
| 102 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
# Display result
|
| 112 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
|
@@ -157,11 +172,16 @@ elif upload_choice == "Upload Video":
|
|
| 157 |
video_source = cv2.VideoCapture(tfile.name)
|
| 158 |
video_feed(video_source)
|
| 159 |
|
| 160 |
-
# Display recent
|
| 161 |
st.markdown("### Recent Records")
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
for record in records:
|
| 164 |
col1, col2, col3 = st.columns(3)
|
| 165 |
-
col1.write(f"**Name**: {record[
|
| 166 |
-
col2.write(f"**Emotion**: {record[
|
| 167 |
-
col3.write(f"**Timestamp**: {record[
|
|
|
|
| 6 |
from facenet_pytorch import MTCNN, InceptionResnetV1
|
| 7 |
from keras.models import load_model
|
| 8 |
from PIL import Image
|
| 9 |
+
import sqlite3
|
| 10 |
import os
|
| 11 |
import tempfile
|
| 12 |
|
| 13 |
+
# SQLite Database Connection
|
| 14 |
+
DB_NAME = "emotion_detection.db"
|
| 15 |
+
|
| 16 |
+
# Initialize SQLite Database
|
| 17 |
+
def initialize_database():
|
| 18 |
+
conn = sqlite3.connect(DB_NAME)
|
| 19 |
+
cursor = conn.cursor()
|
| 20 |
+
cursor.execute("""
|
| 21 |
+
CREATE TABLE IF NOT EXISTS face_data (
|
| 22 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 23 |
+
name TEXT NOT NULL,
|
| 24 |
+
emotion TEXT NOT NULL,
|
| 25 |
+
timestamp TEXT NOT NULL
|
| 26 |
+
)
|
| 27 |
+
""")
|
| 28 |
+
conn.commit()
|
| 29 |
+
conn.close()
|
| 30 |
+
|
| 31 |
+
initialize_database()
|
| 32 |
|
| 33 |
# Page title
|
| 34 |
st.markdown("<h1 style='text-align: center;'>Emotion Detection with Face Recognition</h1>", unsafe_allow_html=True)
|
|
|
|
| 111 |
|
| 112 |
name = recognize_face(face_embedding)
|
| 113 |
|
| 114 |
+
# Save record in SQLite
|
| 115 |
if name != "Unknown":
|
| 116 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 117 |
+
conn = sqlite3.connect(DB_NAME)
|
| 118 |
+
cursor = conn.cursor()
|
| 119 |
+
cursor.execute("""
|
| 120 |
+
INSERT INTO face_data (name, emotion, timestamp)
|
| 121 |
+
VALUES (?, ?, ?)
|
| 122 |
+
""", (name, emotion, timestamp))
|
| 123 |
+
conn.commit()
|
| 124 |
+
conn.close()
|
| 125 |
|
| 126 |
# Display result
|
| 127 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
|
|
|
| 172 |
video_source = cv2.VideoCapture(tfile.name)
|
| 173 |
video_feed(video_source)
|
| 174 |
|
| 175 |
+
# Display recent SQLite records
|
| 176 |
st.markdown("### Recent Records")
|
| 177 |
+
conn = sqlite3.connect(DB_NAME)
|
| 178 |
+
cursor = conn.cursor()
|
| 179 |
+
cursor.execute("SELECT name, emotion, timestamp FROM face_data ORDER BY timestamp DESC LIMIT 5")
|
| 180 |
+
records = cursor.fetchall()
|
| 181 |
+
conn.close()
|
| 182 |
+
|
| 183 |
for record in records:
|
| 184 |
col1, col2, col3 = st.columns(3)
|
| 185 |
+
col1.write(f"**Name**: {record[0]}")
|
| 186 |
+
col2.write(f"**Emotion**: {record[1]}")
|
| 187 |
+
col3.write(f"**Timestamp**: {record[2]}")
|