Code
Code
import mysql.connector
import cv2
class Student:
def __init__(self,root):
self.root=root
self.root.geometry("1530x790+0+0")
#===============variable============
self.var_dep=StringVar()
self.var_course=StringVar()
self.var_year=StringVar()
self.var_semester=StringVar()
self.var_std_id=StringVar()
self.var_std_name=StringVar()
self.var_div=StringVar()
self.var_gender=StringVar()
self.var_dob=StringVar()
self.var_email=StringVar()
self.var_phone=StringVar()
self.var_roll=StringVar()
img=img.resize((500,130))
self.photoimg=ImageTk.PhotoImage(img)
f_lbl=Label(self.root,image=self.photoimg)
f_lbl.place(x=0,y=0,width=500,height=130)
img1=Image.open(r"H:\face_emotion_detection\image collection/student3.jpg")
img1=img1.resize((500,130))
self.photoimg1=ImageTk.PhotoImage(img1)
f_lbl=Label(self.root,image=self.photoimg1)
f_lbl.place(x=500,y=0,width=500,height=130)
img2=Image.open(r"H:\face_emotion_detection\image collection/child.jpg")
img2=img2.resize((500,130))
self.photoimg2=ImageTk.PhotoImage(img2)
f_lbl=Label(self.root,image=self.photoimg2)
f_lbl.place(x=1000,y=0,width=500,height=130)
img3=Image.open(r"H:\face_emotion_detection\image collection/bg.jpg")
img3=img3.resize((1530,710))
self.photoimg3=ImageTk.PhotoImage(img3)
bg_img=Label(self.root,image=self.photoimg3)
bg_img.place(x=0,y=130,width=1530,height=710)
title_lbl.place(x=0,y=0,width=1350,height=45)
main_frame=Frame(bg_img,bd=2,bg="white")
main_frame.place(x=0,y=50,width=1480,height=600)
Left_frame.place(x=10,y=10,width=660,height=580)
img_left=img_left.resize((720,130))
self.photoimg_left=ImageTk.PhotoImage(img_left)
f_lbl=Label(Left_frame,image=self.photoimg_left)
f_lbl.place(x=5,y=0,width=720,height=130)
current_course_frame=LabelFrame(Left_frame,bd=2,relief=RIDGE,text="Current course
information",font=("times new roman",12,"bold"))
current_course_frame.place(x=5,y=135,width=720,height=115)
# Department
dep_label.grid(row=0,column=0,padx=10,sticky=W)
dep_combo.current(0)
dep_combo.grid(row=0,column=1,padx=2,pady=10,sticky=W)
# Course
course_label.grid(row=0,column=2,padx=10,sticky=W)
course_combo=ttk.Combobox( current_course_frame,textvariable=self.var_course,font=("times new
roman",13,"bold"),state="readonly",width=20)
course_combo["values"]=("Select Course","FE","SE","TE","BE")
course_combo.current(0)
course_combo.grid(row=0,column=3,padx=2,pady=10,sticky=W)
#year
year_label.grid(row=1,column=0,padx=10,sticky=W)
year_combo["values"]=("Select Year","2020-21","2021-22","2022-23","2023-24")
year_combo.current(0)
year_combo.grid(row=1,column=1,padx=2,pady=10,sticky=W)
#Semester
semester_label.grid(row=1,column=2,padx=10,sticky=W)
semester_combo=ttk.Combobox(current_course_frame,textvariable=self.var_semester,font=("times
new roman",13,"bold"),state="readonly",width=20)
semester_combo["values"]=("Select
Semester","Semester1","Semester2","Semester3","SEmester4")
semester_combo.current(0)
semester_combo.grid(row=1,column=3,padx=2,pady=10,sticky=W)
class_student_frame=LabelFrame(Left_frame,bd=2,bg="white",relief=RIDGE,text="Class Student
Information",font=("times new roman",12,"bold"))
class_student_frame.place(x=5,y=250,width=720,height=305)
# studentId
studentId_label.grid(row=0,column=0,padx=10,sticky=W)
studentID_entry=ttk.Entry(class_student_frame,textvariable=self.var_std_id,width=20,font=("times
new roman",13,"bold"))
studentID_entry.grid(row=0,column=1,padx=10,pady=5,sticky=W)
#student name
studentName_label=Label(class_student_frame,text="StudentName:",font=("times new
roman",13,"bold"))
studentName_label.grid(row=0,column=2,pady=5,padx=10,sticky=W)
studentName_entry=ttk.Entry(class_student_frame,textvariable=self.var_std_name,width=16,font=("
times new roman",13,"bold"))
studentName_entry.grid(row=0,column=3,padx=10,pady=5,sticky=W)
# class didvison
class_div_label.grid(row=1,column=0,pady=5,padx=10,sticky=W)
div_combo["values"]=("A","B","C")
div_combo.current(0)
div_combo.grid(row=1,column=1,padx=10,pady=5,sticky=W)
# Roll No
roll_no_label.grid(row=1,column=2,pady=5,padx=10,sticky=W)
roll_no_entry=ttk.Entry(class_student_frame,textvariable=self.var_roll,width=16,font=("times new
roman",13,"bold"))
roll_no_entry.grid(row=1,column=3,padx=10,pady=5,sticky=W)
# gender
gender_label.grid(row=2,column=0,pady=5,padx=10,sticky=W)
gender_combo=ttk.Combobox( class_student_frame,textvariable=self.var_gender,font=("times new
roman",13,"bold"),state="readonly",width=18)
gender_combo["values"]=("Male","Female","Other")
gender_combo.current(0)
gender_combo.grid(row=2,column=1,padx=10,pady=5,sticky=W)
# Dob
dob_label.grid(row=2,column=2,pady=5,padx=10,sticky=W)
dob_entry=ttk.Entry(class_student_frame,textvariable=self.var_dob,width=16,font=("times new
roman",13,"bold"))
dob_entry.grid(row=2,column=3,padx=10,pady=5,sticky=W)
email_label.grid(row=3,column=0,pady=5,padx=10,sticky=W)
email_entry=ttk.Entry(class_student_frame,textvariable=self.var_email,width=20,font=("times new
roman",13,"bold"))
email_entry.grid(row=3,column=1,padx=10,pady=5,sticky=W)
# phone NO
phone_label.grid(row=3,column=2,pady=5,padx=10,sticky=W)
phone_entry=ttk.Entry(class_student_frame,textvariable=self.var_phone,width=16,font=("times
new roman",13,"bold"))
phone_entry.grid(row=3,column=3,padx=10,pady=5,sticky=W)
#radio button
self.var_radio1=StringVar()
radiobtn1.grid(row=6,column=0)
self.var_radio2=StringVar()
radiobtn2.grid(row=6,column=1)
# bbutton frame
btn_frame=Frame(class_student_frame,bd=2,relief=RIDGE,bg="white")
btn_frame.place(x=0,y=155,width=715,height=70)
save_btn=Button(btn_frame,text="Save",command=self.add_data,width=22,font=("times new
roman",9,"bold"),bg="blue",fg="white")
save_btn.grid(row=0,column=0)
update_btn=Button(btn_frame,text="Update",command=self.update_data,width=22,font=("times
new roman",9,"bold"),bg="blue",fg="white")
update_btn.grid(row=0,column=1)
delete_btn=Button(btn_frame,text="Delete",command=self.delete_data,width=22,font=("times
new roman",9,"bold"),bg="blue",fg="white")
delete_btn.grid(row=0,column=2)
reset_btn=Button(btn_frame,text="Reset",command=self.reset_data,width=22,font=("times new
roman",9,"bold"),bg="blue",fg="white")
reset_btn.grid(row=0,column=3)
# frame2
btn_frame1=Frame(class_student_frame,bd=2,relief=RIDGE,bg="white")
btn_frame1.place(x=0,y=180,width=710,height=75)
take_photo_btn=Button(btn_frame1,command=self.generate_dataset,text="Take Photo
Sample",width=49,font=("times new roman",8,"bold"),bg="blue",fg="white")
take_photo_btn.grid(row=0,column=0)
update_photo_btn.grid(row=0,column=1)
# right frame
img_right=Image.open(r"H:\face_emotion_detection\image collection/IMG-20241123-
WA0063.jpg")
img_right=img_right.resize((720,130))
self.photoimg_right=ImageTk.PhotoImage(img_right)
f_lbl=Label(Right_frame,image=self.photoimg_right)
f_lbl.place(x=5,y=0,width=650,height=130)
search_frame=LabelFrame(Right_frame,bd=2,bg="white",relief=RIDGE,text="Search
System",font=("times new roman",12,"bold"))
search_frame.place(x=5,y=135,width=710,height=70)
search_label.grid(row=0,column=0,padx=10,pady=5,sticky=W)
search_combo=ttk.Combobox(search_frame,font=("times new
roman",13,"bold"),state="readonly",width=20)
search_combo["values"]=("Select_Semester","Student_Name","Semester","Roll_No","Phone_No")
search_combo.current(0)
search_combo.grid(row=0,column=1,padx=2,pady=10,sticky=W)
search_entry=ttk.Entry(search_frame,width=13,font=("times new roman",13,"bold"))
search_entry.grid(row=0,column=2,padx=10,pady=5,sticky=W)
search_btn=Button(search_frame,text="Search",width=13,font=("times new
roman",9,"bold"),bg="blue",fg="white")
search_btn.grid(row=0,column=3,padx=4)
showAll_btn.grid(row=0,column=4,padx=4)
table_frame=Frame(Right_frame,bd=2,bg="white",relief=RIDGE,)
table_frame.place(x=5,y=210,width=650,height=250)
scroll_x=ttk.Scrollbar(table_frame,orient=HORIZONTAL)
scroll_y=ttk.Scrollbar(table_frame,orient=VERTICAL)
self.student_table=ttk.Treeview(table_frame,column=("dep","course","phoneNo","dob","div","Rollno
","Gender","student_name","Email","year","Semester","name","Id"),xscrollcommand=scroll_x.set,ys
crollcommand=scroll_y.set)
scroll_x.pack(side=BOTTOM,fill=X)
scroll_y.pack(side=RIGHT,fill=Y)
scroll_x.config(command=self.student_table.xview)
scroll_y.config(command=self.student_table.yview)
self.student_table.heading("dep",text="Department")
self.student_table.heading("course",text="Course")
self.student_table.heading("year",text="Year")
self.student_table.heading("Gender",text="Gender")
self.student_table.heading("dob",text="D.O.B")
self.student_table.heading("div",text="Division")
self.student_table.heading("phoneNo",text="phoneNo")
self.student_table.heading("Email",text="Email")
self.student_table["show"]="headings"
self.student_table.column("dep",width=100)
self.student_table.column("course",width=100)
self.student_table.column("year",width=100)
self.student_table.column("Gender",width=50)
self.student_table.column("dob",width=100)
self.student_table.column("div",width=100)
self.student_table.column("phoneNo",width=100)
self.student_table.column("Email",width=100)
self.student_table.pack(fill=BOTH,expand=1)
self.student_table.bind("<ButtonRelease>",self.get_cursor)
self.fetch_data()
def add_data(self):
else:
try:
conn=mysql.connector.connect(host="localhost",username="root",password="Indra@2004",database
="face_recognizer")
my_cursor=conn.cursor()
self.var_dep.get(),
self.var_course.get(),
self.var_year.get(),
self.var_semester.get(),
self.var_std_id.get(),
self.var_std_name.get(),
self.var_div.get(),
self.var_roll.get(),
self.var_gender.get(),
self.var_dob.get(),
self.var_email.get(),
self.var_phone.get(),
self.var_radio1.get()
))
conn.commit()
self.fetch_data()
conn.close()
messagebox.showerror("Error",f"Due To :{str(es)}",parent=self.root)
def fetch_data(self):
conn=mysql.connector.connect(host="localhost",username="root",password="Indra@2004",database
="face_recognizer")
my_cursor=conn.cursor()
data=my_cursor.fetchall()
if len(data)!=0:
self.student_table.delete(*self.student_table.get_children())
for i in data:
self.student_table.insert("",END,values=i)
conn.commit()
conn.close()
def get_cursor(self,event=""):
cursor_focus=self.student_table.focus()
content=self.student_table.item(cursor_focus)
data=content["values"]
self.var_dep.set(data[0],)
self.var_course.set(data[1],)
self.var_year.set(data[2],)
self.var_semester.set(data[3],)
self.var_std_id.set(data[4],)
self.var_std_name.set(data[5],)
self.var_div.set(data[6],)
self.var_roll.set(data[7],)
self.var_gender.set(data[8],)
self.var_dob.set(data[9],)
self.var_email.set(data[10],)
self.var_phone.set(data[11],)
self.var_radio1.set(data[12],)
#================ update function===============
def update_data(self):
else:
try:
if Upadate>0:
conn=mysql.connector.connect(host="localhost",username="root",password="Indra@2004",database
="face_recognizer")
my_cursor=conn.cursor()
self.var_dep.get(),
self.var_course.get(),
self.var_year.get(),
self.var_semester.get(),
self.var_std_name.get(),
self.var_div.get(),
self.var_roll.get(),
self.var_gender.get(),
self.var_dob.get(),
self.var_email.get(),
self.var_phone.get(),
self.var_radio1.get(),
self.var_std_id.get()
))
else:
if not Upadate:
return
conn.commit()
self.fetch_data()
conn.close()
messagebox.showerror("Error",f"Due To:{str(es)}",parent=self.root)
def delete_data(self):
if self.var_std_id.get()=="":
else:
try:
conn=mysql.connector.connect(host="localhost",username="root",password="Indra@2004",database
="face_recognizer")
my_cursor=conn.cursor()
val=(self.var_std_id.get(),)
my_cursor.execute(sql,val)
else:
if not delete:
return
conn.commit()
self.fetch_data()
conn.close()
messagebox.showerror("Error",f"Due To:{str(es)}",parent=self.root)
# ================Reset Fuction==============
def reset_data(self):
self.var_dep.set("Select Department")
self.var_course.set("Select Course")
self.var_year.set("Select Year")
self.var_semester.set("Select Semester")
self.var_std_id.set("")
self.var_std_name.set("")
self.var_div.set("Select Division")
self.var_roll.set("")
self.var_gender.set("Male")
self.var_dob.set("")
self.var_email.set("")
self.var_phone.set("")
self.var_radio1.set("")
def generate_dataset(self):
else:
try:
conn=mysql.connector.connect(host="localhost",username="root",password="Indra@2004",database
="face_recognizer")
my_cursor=conn.cursor()
myresult=my_cursor.fetchall()
id=0
for x in myresult:
id+=1
self.var_dep.get(),
self.var_course.get(),
self.var_year.get(),
self.var_semester.get(),
self.var_std_name.get(),
self.var_div.get(),
self.var_roll.get(),
self.var_gender.get(),
self.var_dob.get(),
self.var_email.get(),
self.var_phone.get(),
self.var_radio1.get(),
self.var_std_id.get()==id+1
))
conn.commit()
self.fetch_data()
self.reset_data()
conn.close()
face_classifier=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def face_cropped(img):
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=face_classifier.detectMultiScale(gray,1.3,5)
#scalling factor=1.3
#Minimum NEighbour=5
face_cropped=img[y:y+h,x:x+w]
return face_cropped
cap=cv2.VideoCapture(0)
img_id=0
while True:
ret,my_frame=cap.read()
img_id+=1
face=cv2.resize(face_cropped(my_frame),(450,450))
face=cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
file_name_path="data/user."+str(id)+"."+str(img_id)+".jpg"
cv2.imwrite(file_name_path,face)
cv2.putText(face,str(img_id),(50,50),cv2.FONT_HERSHEY_COMPLEX,2,(0,255,255,0),2)
cv2.imshow("Crooped Face",face)
if cv2.waitKey(1)==13 or int(img_id)==100:
break
cap.release()
cv2.destroyAllWindows()
messagebox.showinfo("Result","Generating data sets compled!!!!")
messagebox.showerror("Error",f"Due To:{str(es)}",parent=self.root)
if __name__ == "__main__":
root=Tk()
obj=Student(root)
root.mainloop()