示例#1
0
def attendance_workbook(class_name):
    sl = StudentsList(class_name)
    tupl = sl.load_pkl_file()
    global NAMES, ROLLS, NUMBER_OF_STUDENTS
    NAMES = tupl[0]
    ROLLS = tupl[1]
    NUMBER_OF_STUDENTS = len(NAMES)
    # Current length of NAMES is len(NAMES)
    filename = make_file_name(class_name)
    try:
        wb = load_workbook(filename)
    except:
        wb = Workbook()
    wb.guess_types = True
    if not current_month_exists(wb):
        createWorksheet(wb)
    wb.save(filename)
    return wb
    def capture_and_mark(self):
        sl = StudentsList(self.class_name)
        students, roll_numbers = sl.load_pkl_file()

        FaceDetectObj = FaceDetect(self.class_name)

        Yes = True
        No = False
        Cancel = None

        i = 0
        while i <= 2:
            captured_image = None
            frame = None

            students_present = []
            while len(students_present) == 0:
                captured_image, frame = capture()
                students_present = FaceDetectObj.recognize(captured_image, roll_numbers)
                if students_present == "No Training Data":
                    return

            try:
                name_student_present = students[roll_numbers.index(students_present[0])]
            except:
                messagebox.showerror(
                    "Error",
                    "Recognized student not in database\nUnable to mark attendance",
                )
                return

            response = messagebox.askyesnocancel(
                "Confirm your identity",
                students_present[0] + "\n" + name_student_present,
            )

            if response is Yes:
                wb = excel.attendance_workbook(self.class_name)
                excel.mark_present(wb, students_present, self.class_name)
                img_path = os.path.join(
                    os.getcwd(),
                    "images",
                    self.class_name,
                    "s" + students_present[0][-2:],
                    os.path.basename(captured_image),
                )
                cv2.imwrite(img_path, frame)
                os.remove(captured_image)
                messagebox.showinfo(
                    "Attendance Confirmation", "Your attendance is marked!"
                )
                break
            elif response is Cancel:
                break
            elif response is No:
                if i == 2:
                    img_path = os.path.join(
                        os.getcwd(),
                        "images",
                        self.class_name,
                        "unrecognized students",
                        os.path.basename(captured_image),
                    )
                    cv2.imwrite(img_path, frame)
                    messagebox.showinfo(
                        "Unrecognized Student",
                        "You were not recognized as any student of this class.\nYour attendance will be marked later if you really are",
                    )
                    cv2.imwrite(img_path, frame)
                os.remove(captured_image)

            i += 1