LovnishVerma commited on
Commit
5aaa444
·
verified ·
1 Parent(s): d710ae8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -30,6 +30,7 @@ emotion_labels = ['angry', 'fear', 'happy', 'neutral', 'sad', 'surprise']
30
  # Load known faces (from images in a folder)
31
  known_faces = []
32
  known_names = []
 
33
 
34
  def load_known_faces():
35
  folder_path = "known_faces" # Place your folder with known faces here
@@ -38,12 +39,18 @@ def load_known_faces():
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
 
49
  # Face detection using OpenCV
@@ -66,7 +73,7 @@ def process_frame(frame):
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]
 
30
  # Load known faces (from images in a folder)
31
  known_faces = []
32
  known_names = []
33
+ face_recognizer = cv2.face.LBPHFaceRecognizer_create()
34
 
35
  def load_known_faces():
36
  folder_path = "known_faces" # Place your folder with known faces here
 
39
  image_path = os.path.join(folder_path, image_name)
40
  image = cv2.imread(image_path)
41
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
42
+ # Detect face in the image
43
+ faces = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml').detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
44
+
45
+ for (x, y, w, h) in faces:
46
+ roi_gray = gray[y:y+h, x:x+w]
47
+ # We only need the face, so we crop it and store it for training
48
+ known_faces.append(roi_gray)
49
  known_names.append(image_name.split('.')[0]) # Assuming file name is the person's name
50
+
51
+ # Train the recognizer with the known faces
52
+ face_recognizer.train(known_faces, np.array([i for i in range(len(known_faces))]))
53
+
54
  load_known_faces()
55
 
56
  # Face detection using OpenCV
 
73
  emotion = emotion_labels[np.argmax(predictions[0])]
74
 
75
  # Face recognition using LBPH
76
+ label, confidence = face_recognizer.predict(roi_gray)
77
  name = "Unknown"
78
  if confidence < 100:
79
  name = known_names[label]