Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Face Recognition #110

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
33,314 changes: 33,314 additions & 0 deletions Face Recognition/Dataset/haarcascade_frontalface_default.xml

Large diffs are not rendered by default.

155 changes: 155 additions & 0 deletions Face Recognition/Face Recognition/Facerecog.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"from sklearn.neighbors import KNeighborsClassifier\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = np.load(\"face_data.npy\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(data.shape, data.dtype)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X = data[:, 1:].astype(int)\n",
"y = data[:, 0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = KNeighborsClassifier()\n",
"model.fit(X, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cap = cv2.VideoCapture(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"detector = cv2.CascadeClassifier(\"haarcascade_frontalface_default.xml\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"while True:\n",
"\n",
" ret, frame = cap.read()\n",
"\n",
" if ret:\n",
" faces = detector.detectMultiScale(frame)\n",
"\n",
" for face in faces:\n",
" x, y, w, h = face\n",
"\n",
" cut = frame[y:y+h, x:x+w]\n",
"\n",
" fix = cv2.resize(cut, (100, 100))\n",
" gray = cv2.cvtColor(fix, cv2.COLOR_BGR2GRAY)\n",
"\n",
" out = model.predict([gray.flatten()])\n",
"\n",
" cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)\n",
"\n",
" cv2.putText(frame, str(out[0]), (x, y - 10), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 2)\n",
"\n",
" print(out)\n",
"\n",
" cv2.imshow(\"My Face\", gray)\n",
"\n",
" cv2.imshow(\"My Screen\", frame)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" key = cv2.waitKey(1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" if key == ord(\"q\"):\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cap.release()\n",
"cv2.destroyAllWindows()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
52 changes: 52 additions & 0 deletions Face Recognition/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()
106 changes: 106 additions & 0 deletions Face Recognition/Face/Facedetect.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cap = cv2.VideoCapture(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"detector = cv2.CascadeClassifier(\"haarcascade_frontalface_default.xml\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"while True:\n",
"\n",
" ret, frame = cap.read()\n",
"\n",
" if ret:\n",
" faces = detector.detectMultiScale(frame)\n",
"\n",
" for face in faces:\n",
" x, y, w, h = face\n",
"\n",
" cut = frame[y:y+h, x:x+w]\n",
"\n",
" fix = cv2.resize(cut, (100, 100))\n",
" gray = cv2.cvtColor(fix, cv2.COLOR_BGR2GRAY)\n",
"\n",
" cv2.imshow(\"My Screen\", frame)\n",
" cv2.imshow(\"My Face\", gray)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" key = cv2.waitKey(1)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if key == ord(\"q\"):\n",
" break\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cap.release()\n",
"cv2.destroyAllWindows()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Binary file added Face Recognition/Face/face_data.npy
Binary file not shown.
Loading