LovnishVerma commited on
Commit
cd12098
·
verified ·
1 Parent(s): 3f022f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -64,20 +64,25 @@ def train_recognizer():
64
  faces = []
65
  labels = []
66
  for name in os.listdir(KNOWN_FACES_DIR):
67
- for filename in os.listdir(os.path.join(KNOWN_FACES_DIR, name)):
68
- filepath = os.path.join(KNOWN_FACES_DIR, name, filename)
 
 
 
 
 
69
  image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
70
  if image is not None:
71
  faces.append(image)
72
  labels.append(name)
 
 
73
  label_ids = {name: idx for idx, name in enumerate(set(labels))}
74
  label_ids_rev = {idx: name for name, idx in label_ids.items()}
75
  labels = [label_ids[label] for label in labels]
76
- if faces:
77
- face_recognizer.train(faces, np.array(labels))
78
  return label_ids_rev
79
 
80
- label_ids_rev = train_recognizer()
81
 
82
  # Sidebar options
83
  sidebar_choice = st.sidebar.selectbox("Choose an option", ["Emotion Detection", "Register New Face", "View Recent Activity"])
 
64
  faces = []
65
  labels = []
66
  for name in os.listdir(KNOWN_FACES_DIR):
67
+ person_dir = os.path.join(KNOWN_FACES_DIR, name)
68
+ if not os.path.isdir(person_dir): # Skip files, process only directories
69
+ continue
70
+ for filename in os.listdir(person_dir):
71
+ filepath = os.path.join(person_dir, filename)
72
+ if not filepath.lower().endswith(('.jpg', '.jpeg', '.png')): # Ensure valid image formats
73
+ continue
74
  image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
75
  if image is not None:
76
  faces.append(image)
77
  labels.append(name)
78
+ if not faces or not labels:
79
+ raise ValueError("No valid training data found in the known faces directory.")
80
  label_ids = {name: idx for idx, name in enumerate(set(labels))}
81
  label_ids_rev = {idx: name for name, idx in label_ids.items()}
82
  labels = [label_ids[label] for label in labels]
83
+ face_recognizer.train(faces, np.array(labels))
 
84
  return label_ids_rev
85
 
 
86
 
87
  # Sidebar options
88
  sidebar_choice = st.sidebar.selectbox("Choose an option", ["Emotion Detection", "Register New Face", "View Recent Activity"])