Face Detection Based Attendance System
- Create a Main Folder named "Face Detection Based Attendance System" in VS Code.
- Create a file named "add_faces.py"
add_faces.py
import cv2
video=cv2.VideoCapture(0)
while True:
ret,frame=video.read()
cv2.imshow("Frame",frame)
k=cv2.waitKey(1)
if k==ord('q'):
break
video.release()
cv2.destroyAllWindows()
- Open a new terminal and type "python add_faces.py"
- This will open your web camera. So, the process is getting started. Click "Q" to exit camera.
Create a Folder named "Data". In that folder, create a file named "haarcascade_frontalface_default.xml"
haarcascade_frontalface_default.xml
Now, write code in add_faces.py as,
add_faces.py
import cv2
video=cv2.VideoCapture(0)
facedetect=cv2.CascadeClassifier('data\haarcascade_frontalface_default.xml')
while True:
ret,frame=video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.3 ,5)
for (x,y,w,h) in faces:
cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
cv2.imshow("Frame",frame)
k=cv2.waitKey(1)
if k==ord('q'):
break
video.release()
cv2.destroyAllWindows()
- Open terminal and type "python add_faces.py"
- This will open your web camera. So, the process is again started. And this time, our face is surrounded by a rectangular box. Click "Q" to exit camera.
Now, write code in add_faces.py as,
add_faces.py
import cv2
video=cv2.VideoCapture(0)
facedetect=cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
faces_data=[]
i=0
while True:
ret,frame=video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.3 ,5)
for (x,y,w,h) in faces:
crop_img=frame[y:y+h, x:x+w, :]
resized_img=cv2.resize(crop_img, (50,50))
if len(faces_data)<=100 and i%10==0:
faces_data.append(resized_img)
i=i+1
cv2.putText(frame, str(len(faces_data)), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (50,50,255), 1)
cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
cv2.imshow("Frame",frame)
k=cv2.waitKey(1)
if k==ord('q') or len(faces_data)==100:
break
video.release()
cv2.destroyAllWindows()
- Open terminal and type "python add_faces.py"
- This will open your web camera. So, the process is again started. And this time, our face is surrounded by a rectangular box and timer will gets started. Click "Q" to exit camera.
Now, write code in add_faces.py as,
add_faces.py
import cv2
import pickle
import numpy as np
import os
video=cv2.VideoCapture(0)
facedetect=cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
faces_data=[]
i=0
name=input("Enter Your Name: ")
while True:
ret,frame=video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.3 ,5)
for (x,y,w,h) in faces:
crop_img=frame[y:y+h, x:x+w, :]
resized_img=cv2.resize(crop_img, (50,50))
if len(faces_data)<=100 and i%10==0:
faces_data.append(resized_img)
i=i+1
cv2.putText(frame, str(len(faces_data)), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (50,50,255), 1)
cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
cv2.imshow("Frame",frame)
k=cv2.waitKey(1)
if k==ord('q') or len(faces_data)==100:
break
video.release()
cv2.destroyAllWindows()
faces_data=np.asarray(faces_data)
faces_data=faces_data.reshape(100, -1)
if 'names.pkl' not in os.listdir('data/'):
names=[name]*100
with open('data/names.pkl', 'wb') as f:
pickle.dump(names, f)
else:
with open('data/names.pkl', 'rb') as f:
names=pickle.load(f)
names=names+[name]*100
with open('data/names.pkl', 'wb') as f:
pickle.dump(names, f)
if 'faces_data.pkl' not in os.listdir('data/'):
with open('data/faces_data.pkl', 'wb') as f:
pickle.dump(faces_data, f)
else:
with open('data/faces_data.pkl', 'rb') as f:
faces=pickle.load(f)
faces=np.append(faces, faces_data, axis=0)
with open('data/faces_data.pkl', 'wb') as f:
pickle.dump(faces, f)
- Open terminal and type "python add_faces.py"
- First it asks to "Enter your name:". Then enter our name.
- This will open your web camera. So, the process is again started. And this time, our face is surrounded by a rectangular box and timer will gets started.
- It will take 100 frames of our photo. Try to rotate face up to 360 degrees to take in various angles.
- After 100 seconds, camera will automatically close.
- Now, automatically "faces_data.pkl" and "names_pkl" files are created in "data" folder.
To see add_faces.py Click here
Now, create a file named "test.py" to test the input data given in add_faces.py file.test.py
from sklearn.neighbors import KNeighborsClassifier
import cv2
import pickle
import numpy as np
import os
import csv
video=cv2.VideoCapture(0)
facedetect=cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
with open('data/names.pkl', 'rb') as w:
LABELS=pickle.load(w)
with open('data/faces_data.pkl', 'rb') as f:
FACES=pickle.load(f)
knn=KNeighborsClassifier(n_neighbors=5)
knn.fit(FACES, LABELS)
while True:
ret,frame=video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.3 ,5)
for (x,y,w,h) in faces:
crop_img=frame[y:y+h, x:x+w, :]
resized_img=cv2.resize(crop_img, (50,50)).flatten().reshape(1,-1)
output=knn.predict(resized_img)
cv2.putText(frame, str(output[0]), (x,y-15), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 1)
cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
cv2.imshow("Frame",frame)
k=cv2.waitKey(1)
if k==ord('q'):
break
video.release()
cv2.destroyAllWindows()
- Open a new terminal and type "python test.py"
- This will open your web camera. So, the process is getting started.
- It will identify your face and displays your name.
- Click "Q" to exit camera.
test.py
from sklearn.neighbors import KNeighborsClassifier
import cv2
import pickle
import numpy as np
import os
import csv
video=cv2.VideoCapture(0)
facedetect=cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
with open('data/names.pkl', 'rb') as w:
LABELS=pickle.load(w)
with open('data/faces_data.pkl', 'rb') as f:
FACES=pickle.load(f)
knn=KNeighborsClassifier(n_neighbors=5)
knn.fit(FACES, LABELS)
while True:
ret,frame=video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.3 ,5)
for (x,y,w,h) in faces:
crop_img=frame[y:y+h, x:x+w, :]
resized_img=cv2.resize(crop_img, (50,50)).flatten().reshape(1,-1)
output=knn.predict(resized_img)
cv2.rectangle(frame, (x,y), (x+w, y+h), (0,0,255), 1)
cv2.rectangle(frame,(x,y),(x+w,y+h),(50,50,255),2)
cv2.rectangle(frame,(x,y-40),(x+w,y),(50,50,255),-1)
cv2.putText(frame, str(output[0]), (x,y-15), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 1)
cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
cv2.imshow("Frame",frame)
k=cv2.waitKey(1)
if k==ord('q'):
break
video.release()
cv2.destroyAllWindows()
- Open terminal and type "python test.py"
- This will open your web camera. So, the process is getting started.
- It will identify your face and displays your name.
- This time name and face are displayed in a rectangular box.
- Click "Q" to exit camera.
Create a folder named "Attendance". In that folder, create a file named "Attendance_20-08-2024.csv" file.
Now, write code as follows:
test.py
from sklearn.neighbors import KNeighborsClassifier
import cv2
import pickle
import numpy as np
import os
import csv
import time
from datetime import datetime
from win32com.client import Dispatch
def speak(str1):
speak=Dispatch(("SAPI.SpVoice"))
speak.Speak(str1)
video=cv2.VideoCapture(0)
facedetect=cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
with open('data/names.pkl', 'rb') as w:
LABELS=pickle.load(w)
with open('data/faces_data.pkl', 'rb') as f:
FACES=pickle.load(f)
print('Shape of Faces matrix --> ', FACES.shape)
knn=KNeighborsClassifier(n_neighbors=5)
knn.fit(FACES, LABELS)
imgBackground=cv2.imread("background.png")
COL_NAMES = ['NAME', 'TIME']
while True:
ret,frame=video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces=facedetect.detectMultiScale(gray, 1.3 ,5)
for (x,y,w,h) in faces:
crop_img=frame[y:y+h, x:x+w, :]
resized_img=cv2.resize(crop_img, (50,50)).flatten().reshape(1,-1)
output=knn.predict(resized_img)
ts=time.time()
date=datetime.fromtimestamp(ts).strftime("%d-%m-%Y")
timestamp=datetime.fromtimestamp(ts).strftime("%H:%M-%S")
exist=os.path.isfile("Attendance/Attendance_" + date + ".csv")
cv2.rectangle(frame, (x,y), (x+w, y+h), (0,0,255), 1)
cv2.rectangle(frame,(x,y),(x+w,y+h),(50,50,255),2)
cv2.rectangle(frame,(x,y-40),(x+w,y),(50,50,255),-1)
cv2.putText(frame, str(output[0]), (x,y-15), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 1)
cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)
attendance=[str(output[0]), str(timestamp)]
imgBackground[162:162 + 480, 55:55 + 640] = frame
cv2.imshow("Frame",imgBackground)
k=cv2.waitKey(1)
if k==ord('o'):
speak("Attendance Taken..")
time.sleep(5)
if exist:
with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile:
writer=csv.writer(csvfile)
writer.writerow(attendance)
csvfile.close()
else:
with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile:
writer=csv.writer(csvfile)
writer.writerow(COL_NAMES)
writer.writerow(attendance)
csvfile.close()
if k==ord('q'):
break
video.release()
cv2.destroyAllWindows()
- Open terminal and type "python test.py"
- This will open your web camera. So, the process is getting started.
- It will identify your face and displays your name.
- Your face is displayed in a rectangular box.
- Click "o" to take attendance.
- Click "Q" to exit camera.
- Attendance data is stored in "Attendance_20-08-2024.csv" file.
To see, test.py Click here
app.py
import streamlit as st
import pandas as pd
import time
from datetime import datetime
ts=time.time()
date=datetime.fromtimestamp(ts).strftime("%d-%m-%Y")
timestamp=datetime.fromtimestamp(ts).strftime("%H:%M-%S")
df=pd.read_csv("Attendance/Attendance_" + date + ".csv")
st.dataframe(df.style.highlight_max(axis=0))
- Open terminal and type "streamlit run app.py"
![]() |
Fig: Streamlit app |
- This will open new web page.
- Run test.py code
- Then the process gets started. After clicking on "o", the attendance time will be noted.
- This can be shown in Streamlit web page.
- Click "Q" to exit camera.
- Also, Attendance data is stored in "Attendance_20-08-2024.csv" file.
app.py
import streamlit as st
import pandas as pd
import time
from datetime import datetime
ts=time.time()
date=datetime.fromtimestamp(ts).strftime("%d-%m-%Y")
timestamp=datetime.fromtimestamp(ts).strftime("%H:%M-%S")
from streamlit_autorefresh import st_autorefresh
count = st_autorefresh(interval=2000, limit=100, key="fizzbuzzcounter")
if count == 0:
st.write("Count is zero")
elif count % 3 == 0 and count % 5 == 0:
st.write("FizzBuzz")
elif count % 3 == 0:
st.write("Fizz")
elif count % 5 == 0:
st.write("Buzz")
else:
st.write(f"Count: {count}")
df=pd.read_csv("Attendance/Attendance_" + date + ".csv")
st.dataframe(df.style.highlight_max(axis=0))
![]() |
Fig: Streamlit web page |
- Run test.py code
- Then the process gets started. After clicking on "o", the attendance time will be noted.
- This can be shown in Streamlit web page.
- Click "Q" to exit camera.
- Also, Attendance data is stored in "Attendance_20-08-2024.csv" file.
Click Here for full code: Click here
Comments
Post a Comment