LovnishVerma commited on
Commit
6f924cf
·
verified ·
1 Parent(s): 2d75c41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -4,7 +4,6 @@ import numpy as np
4
  import time
5
  import os
6
  from keras.models import load_model
7
- import face_recognition
8
  from PIL import Image
9
  import tempfile
10
 
@@ -37,10 +36,13 @@ def load_known_faces():
37
  for image_name in os.listdir(folder_path):
38
  if image_name.endswith(('.jpg', '.jpeg', '.png')):
39
  image_path = os.path.join(folder_path, image_name)
40
- image = face_recognition.load_image_file(image_path)
41
- encoding = face_recognition.face_encodings(image)
 
 
 
42
  if encoding:
43
- known_faces.append(encoding[0])
44
  known_names.append(image_name.split('.')[0]) # Assuming file name is the person's name
45
  load_known_faces()
46
 
@@ -63,13 +65,11 @@ def process_frame(frame):
63
  predictions = model.predict(face_roi)
64
  emotion = emotion_labels[np.argmax(predictions[0])]
65
 
66
- # Face recognition
67
- face_encoding = face_recognition.face_encodings(frame, [(y, x+w, y+h, x)])[0] # Get face encoding
68
- matches = face_recognition.compare_faces(known_faces, face_encoding)
69
  name = "Unknown"
70
- if True in matches:
71
- first_match_index = matches.index(True)
72
- name = known_names[first_match_index]
73
 
74
  # Draw bounding box and label on the frame
75
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
 
4
  import time
5
  import os
6
  from keras.models import load_model
 
7
  from PIL import Image
8
  import tempfile
9
 
 
36
  for image_name in os.listdir(folder_path):
37
  if image_name.endswith(('.jpg', '.jpeg', '.png')):
38
  image_path = os.path.join(folder_path, image_name)
39
+ image = cv2.imread(image_path)
40
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
41
+ faces = cv2.face.LBPHFaceRecognizer_create()
42
+ faces.train([gray], np.array([0])) # This is simplified, train with multiple images
43
+ encoding = faces.getHistograms()
44
  if encoding:
45
+ known_faces.append(encoding)
46
  known_names.append(image_name.split('.')[0]) # Assuming file name is the person's name
47
  load_known_faces()
48
 
 
65
  predictions = model.predict(face_roi)
66
  emotion = emotion_labels[np.argmax(predictions[0])]
67
 
68
+ # Face recognition using LBPH
69
+ label, confidence = cv2.face.LBPHFaceRecognizer_create().predict(roi_gray)
 
70
  name = "Unknown"
71
+ if confidence < 100:
72
+ name = known_names[label]
 
73
 
74
  # Draw bounding box and label on the frame
75
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)