LovnishVerma commited on
Commit
bbe4d10
·
verified ·
1 Parent(s): 8475675

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
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 pymongo
10
  import os
11
  import tempfile
12
 
13
- # MongoDB Atlas Connection String
14
- MONGO_URI = "mongodb+srv://test:test@cluster0.sxci1.mongodb.net/?retryWrites=true&w=majority"
15
- client = pymongo.MongoClient(MONGO_URI)
16
- db = client.get_database("emotion_detection")
17
- collection = db.get_collection("face_data")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 MongoDB
101
  if name != "Unknown":
102
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
103
- document = {
104
- "name": name,
105
- "emotion": emotion,
106
- "timestamp": timestamp
107
- }
108
- if not collection.find_one({"name": name, "timestamp": timestamp}):
109
- collection.insert_one(document)
 
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 MongoDB records
161
  st.markdown("### Recent Records")
162
- records = collection.find().sort("timestamp", -1).limit(5)
 
 
 
 
 
163
  for record in records:
164
  col1, col2, col3 = st.columns(3)
165
- col1.write(f"**Name**: {record['name']}")
166
- col2.write(f"**Emotion**: {record['emotion']}")
167
- col3.write(f"**Timestamp**: {record['timestamp']}")
 
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]}")