Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions Face Recognition/Facedetect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import cv2

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)

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

key = cv2.waitKey(1)

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

cap.release()
cv2.destroyAllWindows()
52 changes: 52 additions & 0 deletions Face Recognition/Facerecog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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)

print(out)

cv2.imshow("My Face", gray)

cv2.imshow("My Screen", frame)


key = cv2.waitKey(1)

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

cap.release()
cv2.destroyAllWindows()
52 changes: 52 additions & 0 deletions Face Recognition/Facerecog.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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("../../datasets/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)

print(out)

cv2.imshow("My Face", gray)

cv2.imshow("My Screen", frame)


key = cv2.waitKey(1)

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

cap.release()
cv2.destroyAllWindows()
Binary file added Face Recognition/Sample Output.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Face Recognition/face_data.npy
Binary file not shown.
56 changes: 56 additions & 0 deletions Face Recognition/face_recog_collector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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)
data = np.vstack([old, data])

np.save(f_name, data)

cap.release()
cv2.destroyAllWindows()
Loading