INTRODUCTION
K-nearest neighbor definition
As a classification algorithm, kNN assigns a new data point to the majority set within its neighbors. As a regression algorithm, kNN makes a prediction based on the average of the values closest to the query point.
Watch On YOUTUBE
FULL CODE 😃👇
DATASET.PY
import cv2
import pickle
import numpy as np
import os
video=cv2.VideoCapture(0)
facedetect=cv2.CascadeClassifier('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 len(faces_data)==50:
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)
ATTENDANCE.PY CODE:
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('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().......
...........
FULL CODE AND SOURCES 😃👇
https://tpi.li/faceattendancesourcecodeFOLLOW US :
1.FREETECH YOUTUBE CHANNEL →
https://www.youtube.com/@FREETECH-xu1ob
SUPPORT ME 😟
FREE C++ SKILLSHARE COURSE
FREE C SKILLSHARE COURSE
All Courses 😃👇
https://linktr.ee/Freetech2024
All Products 😃👇
https://linktr.ee/rockstararun
HP Laptop 🤩👇
Asus Laptop 🤩👇
THANKS FOR READING 😁😁
No comments:
Post a Comment