Facial Recognitoin Attendance python project only working for one person
Published: 2023-11-06 10:09:16

Hi, I was writing a code to facially recognize students of a class in a picture and mark their attendance in excel sheet, and update that sheet daily with data and name, I was using bill gates and elon musk's images as student's dataset, but I'm shown error which is:

`Traceback (most recent call last):
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 4211, in _set_value
    iindex = self.index.get_loc(index)
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\range.py", line 394, in get_loc
    self._check_indexing_error(key)
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 5925, in _check_indexing_error
    raise InvalidIndexError(key)
pandas.errors.InvalidIndexError: 0    False
1     True
Name: Name, dtype: bool

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Attendance_Project\try_wentgood.py", line 41, in <module>
    attendance.at[attendance['Name'] == name, date.today()] = 1
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexing.py", line 2442, in __setitem__
    return super().__setitem__(key, value)
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexing.py", line 2397, in __setitem__
    self.obj._set_value(*key, value=value, takeable=self._takeable)
  File "C:\Users\manan\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 4230, in _set_value
    raise InvalidIndexError(
pandas.errors.InvalidIndexError: You can only assign a scalar value not a <class 'int'>
`

And my code is:

```
import face_recognition
import os
import cv2
import pandas as pd
from datetime import date

# Create a dataframe to store attendance
attendance = pd.DataFrame(columns=['Name', date.today()])

# Load student data from the dataset
dataset_dir = 'D:/Attendance_Project/dataset'

student_data = {}
for student_name in os.listdir(dataset_dir):
    student_folder = os.path.join(dataset_dir, student_name)
    student_faces = []

    for filename in os.listdir(student_folder):
        if filename.endswith(".jpg"):
            student_face = face_recognition.load_image_file(os.path.join(student_folder, filename))
            student_faces.append(face_recognition.face_encodings(student_face)[0])

    student_data[student_name] = student_faces

# Load the class picture
class_picture_path = "C:/Users/manan/Downloads/class1.jpg"
class_picture = face_recognition.load_image_file(class_picture_path)

# Find faces in the class picture
face_locations = face_recognition.face_locations(class_picture)
face_encodings = face_recognition.face_encodings(class_picture, face_locations)

# Compare detected faces to student data and mark attendance
for i, face_encoding in enumerate(face_encodings):
    for name, student_faces in student_data.items():
        for student_face_encoding in student_faces:
            matches = face_recognition.compare_faces([student_face_encoding], face_encoding, tolerance=0.6)

            if True in matches:
                if name in attendance['Name'].values:
                    attendance.at[attendance['Name'] == name, date.today()] = 1
                else:
                    attendance = attendance.append({'Name': name, date.today(): 1}, ignore_index=True)

# Save attendance to an Excel file
attendance.to_excel('attendance.xlsx', index=False)
```

Earlier, when I was using only elon musk's dataset, that time it was working fine, and then I also included bill gates in dataset, after that, I am getting this error, I thought that it can only identify 1 person, for confirmation, I kept only bill gates in dataset, but it also don't identify bill gates, I then reverted to "only" Elon Musk again, and it worked again, please help me...