LovnishVerma commited on
Commit
d39c3d7
·
verified ·
1 Parent(s): 6652f65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -4,10 +4,10 @@ import streamlit as st
4
  from PIL import Image
5
  import numpy as np
6
  import cv2
 
7
  from keras.models import load_model
8
  from datetime import datetime
9
  from huggingface_hub import HfApi
10
- import face_recognition
11
 
12
  # Constants
13
  KNOWN_FACES_DIR = "known_faces" # Directory to save user images
@@ -30,7 +30,9 @@ REPO_TYPE = "space" # 'space' type for Streamlit-based projects
30
  # Load emotion detection model
31
  model = load_model('CNN_Model_acc_75.h5')
32
  emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
33
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
 
 
34
 
35
  # Initialize the SQLite database
36
  def initialize_database():
@@ -92,19 +94,18 @@ def save_image_to_hugging_face(image, name, roll_no):
92
  # Process each frame for emotion detection and face recognition
93
  def process_frame(frame, known_face_encodings, known_face_names):
94
  gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
95
- faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
96
 
97
  face_locations = []
98
  face_encodings = []
99
  face_names = []
100
- for (x, y, w, h) in faces:
101
- roi_color = frame[y:y+h, x:x+w]
102
- # Face recognition
103
- rgb_frame = frame[:, :, ::-1] # Convert to RGB (face_recognition expects RGB)
104
- face_encoding = face_recognition.face_encodings(rgb_frame, [(y, x + w, y + h, x)])[0]
105
 
106
  # Check if the detected face matches any known face
107
- matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
108
  name = "Unknown"
109
 
110
  if True in matches:
@@ -112,6 +113,7 @@ def process_frame(frame, known_face_encodings, known_face_names):
112
  name = known_face_names[first_match_index]
113
 
114
  # Emotion detection
 
115
  face_roi = cv2.resize(roi_color, (48, 48))
116
  face_roi = np.expand_dims(face_roi, axis=0)
117
  face_roi = face_roi / float(48)
@@ -119,8 +121,8 @@ def process_frame(frame, known_face_encodings, known_face_names):
119
  emotion = emotion_labels[np.argmax(predictions[0])]
120
 
121
  # Display name and emotion text on face
122
- cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
123
- cv2.putText(frame, f"{name} - {emotion}", (x, y+h), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
124
 
125
  return frame
126
 
@@ -141,8 +143,8 @@ rows = cursor.fetchall()
141
 
142
  for row in rows:
143
  name, image_path = row
144
- image = face_recognition.load_image_file(image_path)
145
- encoding = face_recognition.face_encodings(image)[0]
146
  known_face_encodings.append(encoding)
147
  known_face_names.append(name)
148
 
@@ -181,7 +183,7 @@ if st.button("Register"):
181
  save_to_database(name, roll_no, image_path)
182
 
183
  # Update the known faces list
184
- known_face_encodings.append(face_recognition.face_encodings(image)[0])
185
  known_face_names.append(name)
186
 
187
  st.success(f"Student {name} registered successfully!")
 
4
  from PIL import Image
5
  import numpy as np
6
  import cv2
7
+ import dlib
8
  from keras.models import load_model
9
  from datetime import datetime
10
  from huggingface_hub import HfApi
 
11
 
12
  # Constants
13
  KNOWN_FACES_DIR = "known_faces" # Directory to save user images
 
30
  # Load emotion detection model
31
  model = load_model('CNN_Model_acc_75.h5')
32
  emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
33
+ face_detector = dlib.get_frontal_face_detector() # Use Dlib's face detector
34
+ face_predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') # Landmarks model for face recognition
35
+ face_rec_model = dlib.face_recognition_model_v1('dlib_face_recognition_resnet_model_v1.dat') # Face recognition model
36
 
37
  # Initialize the SQLite database
38
  def initialize_database():
 
94
  # Process each frame for emotion detection and face recognition
95
  def process_frame(frame, known_face_encodings, known_face_names):
96
  gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
97
+ faces = face_detector(gray_frame)
98
 
99
  face_locations = []
100
  face_encodings = []
101
  face_names = []
102
+ for face in faces:
103
+ # Get the landmarks of the face
104
+ shape = face_predictor(gray_frame, face)
105
+ face_encoding = np.array(face_rec_model.compute_face_descriptor(frame, shape))
 
106
 
107
  # Check if the detected face matches any known face
108
+ matches = [np.allclose(face_encoding, known_encoding) for known_encoding in known_face_encodings]
109
  name = "Unknown"
110
 
111
  if True in matches:
 
113
  name = known_face_names[first_match_index]
114
 
115
  # Emotion detection
116
+ roi_color = frame[face.top():face.bottom(), face.left():face.right()]
117
  face_roi = cv2.resize(roi_color, (48, 48))
118
  face_roi = np.expand_dims(face_roi, axis=0)
119
  face_roi = face_roi / float(48)
 
121
  emotion = emotion_labels[np.argmax(predictions[0])]
122
 
123
  # Display name and emotion text on face
124
+ cv2.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 2)
125
+ cv2.putText(frame, f"{name} - {emotion}", (face.left(), face.bottom() + 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
126
 
127
  return frame
128
 
 
143
 
144
  for row in rows:
145
  name, image_path = row
146
+ image = Image.open(image_path)
147
+ encoding = dlib.face_recognition_model_v1.compute_face_descriptor(image)[0]
148
  known_face_encodings.append(encoding)
149
  known_face_names.append(name)
150
 
 
183
  save_to_database(name, roll_no, image_path)
184
 
185
  # Update the known faces list
186
+ known_face_encodings.append(dlib.face_recognition_model_v1.compute_face_descriptor(image)[0])
187
  known_face_names.append(name)
188
 
189
  st.success(f"Student {name} registered successfully!")