Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions attendance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import cv2
import numpy as np
from sklearn.neighbors import KNeighborsClassifier

data = np.load("face_data.npy")

print(data.shape, data.dtype)

X = data[ : , 1: ].astype(int)
y = data[:, 0]

model = KNeighborsClassifier()
model.fit(X, y)

cap = cv2.VideoCapture(0)

detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
while True:

ret, frame = cap.read()

if ret:
faces = detector.detectMultiScale(frame)

for face in faces:
x, y, w, h = face

cut = frame[y:y+h, x:x+w]

fix = cv2.resize(cut, (100, 100))
gray = cv2.cvtColor(fix, cv2.COLOR_BGR2GRAY)

out = model.predict([gray.flatten()])

cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

cv2.putText(frame, str(out[0]), (x, y - 10), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 2)

f=open(attendance.txt,"a+")
x.write(str(out[0]+"\n"))

cv2.imshow("My Face", gray)

cv2.imshow("My Screen", frame)


key = cv2.waitKey(1)

if key == ord("q"):
break

cap.release()
cv2.destroyAllWindows()
58 changes: 58 additions & 0 deletions dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

import cv2
import numpy as np

import os

cap = cv2.VideoCapture(0)

detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

name = input("Enter your name : ")

frames = []
outputs = []

while True:

ret, frame = cap.read()

if ret:
faces = detector.detectMultiScale(frame)

for face in faces:
x, y, w, h = face

cut = frame[y:y+h, x:x+w]

fix = cv2.resize(cut, (100, 100))
gray = cv2.cvtColor(fix, cv2.COLOR_BGR2GRAY)

cv2.imshow("My Screen", frame)
cv2.imshow("My Face", gray)

key = cv2.waitKey(1)

if key == ord("q"):
break
if key == ord("c"):
# cv2.imwrite(name + ".jpg", frame)
frames.append(gray.flatten())
outputs.append([name])

X = np.array(frames)
y = np.array(outputs)

data = np.hstack([y, X])

f_name = "face_data.npy"

if os.path.exists(f_name):
old = np.load(f_name)
print(old.shape)
data = np.vstack((old, data))

np.save(f_name, data)

cap.release()
cv2.destroyAllWindows()
Binary file added face_data.npy
Binary file not shown.
Loading