def create_new_batch(self): global class_codes class_name = self.tv_class.get() number_of_studs = int(self.tv_number.get()) batchexists = os.path.exists( os.path.join(os.getcwd(), "extras", class_name)) if batchexists: messagebox.showerror("Error", "This batch name already exists") return if number_of_studs < 1 or number_of_studs > 99: messagebox.showerror("Error", "Number of students not in allowed range!") return images_dir = os.path.join(os.getcwd(), "images", class_name) unrecog_studs_dir = os.path.join(os.getcwd(), "images", class_name, "unrecognized students") os.makedirs(unrecog_studs_dir) for i in range(number_of_studs): os.makedirs(os.path.join(images_dir, "s" + str(i).zfill(2))) studs_list_file = os.path.join(os.getcwd(), "student's list", f"{class_name}.xlsx") wb = Workbook() wb.guess_types = True ws = wb.active ws["A1"] = 0 ws["B1"] = number_of_studs wb.save(studs_list_file) classes_file = os.path.join(os.getcwd(), "extras", "classes.xlsx") wb = load_workbook(classes_file) ws = wb.active row = ws["A1"].value ws["A1"] = row + 1 ws["A" + str(row + 2)] = class_name wb.save(classes_file) sl = StudentsList(class_name) sl.make_pkl_file() atten_reg_dir = os.path.join(os.getcwd(), "extras", class_name) os.makedirs(atten_reg_dir) wb = excel.attendance_workbook(class_name) messagebox.showinfo( "Batch Successfully Created", "All the necessary Directories and Files created" f"for Batch-Code {class_name}", ) class_codes.add(class_name) self.tv_class.delete(0, END) self.tv_number.delete(0, END) self.tv_number.insert(0, "") self.controller.show_frame("StartPage")
def validate(self, page_name, c_code, id, pwd): global class_codes if id == manager_id and pwd == manager_pass and c_code in class_codes: frame = self.frames[page_name] global current_class current_class = c_code print("current class", current_class) if current_class != 'Marauders': # frame.lb_class_code.config(text="Batch Code : {}".format(current_class)) # frame.lb_class_code['text']="Batch Code : {}".format(current_class) StudentsList(current_class).make_pkl_file() if page_name == "ManagerPanelPage": global FaceTrainObj FaceTrainObj = FaceTrain(current_class) elif page_name == "StudentPanelPage": global current_class_obj current_class_obj = MainFile(current_class) elif current_class == 'Marauders' and page_name == 'StudentPanelPage': messagebox.showerror('Error', 'Only Manager Panel is valid for this class-code') return else: frame = self.frames['CreateNewBatchPage'] frame.tkraise() else: messagebox.showerror('Error', 'Incorrect Credentials !!')
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 doWork(self): global current_class students_list_file = os.path.join(os.getcwd(), "student's list", f'{current_class}.xlsx') wb = load_workbook(students_list_file) ws = wb.active total_studs = ws['B1'].value currently_studs = ws['A1'].value if currently_studs == total_studs: messagebox.showinfo("Batch Full", "No more accomodation for any new student") return ws['A1'] = currently_studs + 1 row_here = currently_studs + 2 student_name = self.tv_name.get() ws['A' + str(row_here)] = student_name roll_no = currently_studs roll_no = str(roll_no).zfill(2) ws['B' + str(row_here)] = current_class + roll_no wb.save(students_list_file) sl = StudentsList(current_class) sl.make_pkl_file() atten_reg = os.path.join(os.getcwd(), 'extras', current_class, f'{current_class}.xlsx') wb = load_workbook(atten_reg) today = datetime.now().date() month = today.strftime('%B') try: # If current month exists ws = wb[month] row_here = excel.gap + int(roll_no) ws.merge_cells(start_row=row_here, start_column=3, end_row=row_here, end_column=4) ws.cell(row=row_here, column=1, value=str(int(roll_no) + 1)) fill_roll = excel.PatternFill(fgColor='A5EFAF', fill_type='solid') fill_name = excel.PatternFill(fgColor='E9EFA5', fill_type='solid') ws.cell(row=row_here, column=2, value=current_class + roll_no).fill = fill_roll ws.cell(row=row_here, column=3, value=student_name).fill = fill_name cellrange = 'A' + str(row_here) + ':AN' + str(row_here) excel.set_border(ws, cellrange) first_date_column = excel.get_column_letter(excel.date_column) days_in_month = monthrange(today.year, today.month)[1] last_date_column = excel.get_column_letter(excel.date_column + days_in_month - 1) sum_cell_column = excel.date_column + days_in_month + 1 fill_total = excel.PatternFill(fgColor='25F741', fill_type='solid') ws.cell(row=row_here, column=sum_cell_column, value="=SUM(" + first_date_column + str(row_here) + ":" + last_date_column + str(row_here) + ")").fill = fill_total wb.save(atten_reg) except: # If current month does not exist wb = excel.attendance_workbook(current_class) image_dir = os.path.join(os.getcwd(), 'images', current_class, 's' + roll_no) messagebox.showinfo('Student Added', f"{student_name} admitted!\n" "Click OK to proceed for capturing training images. " "Make sure that your surroundings are well lit") i = 5 face_detected_right = False xml_file = os.path.join(os.getcwd(), 'haarcascade_frontalface_default.xml') while i > 0: while face_detected_right is False: img_path, frame = capture() face_detected_right = detect_faces(xml_file, frame) img_path = os.path.join( os.getcwd(), 'images', current_class, "s"+str(roll_no).zfill(2), os.path.basename(img_path) ) cv2.imwrite(img_path, frame) cv2.destroyAllWindows() face_detected_right = False i = i - 1 if i == 0: messagebox.showinfo("Message", "Training images added successfully!") messagebox.showinfo('Enrollment Number', f'Roll Number of student: {current_class + roll_no}') self.tv_name.delete(0, END)
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