Skip to content

Added Face Recognition Script #115

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

Open
wants to merge 4 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
97 changes: 97 additions & 0 deletions Face_Recognition/Face_Data.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Required Imports\n",
"import cv2\n",
"import numpy as np\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Detecting face and saving data\n",
"cap = cv2.VideoCapture(0)\n",
"\n",
"detector = cv2.CascadeClassifier(\"./datasets/haarcascade_frontalface_default.xml\")\n",
"\n",
"name = input(\"Enter your name : \")\n",
"\n",
"frames = []\n",
"outputs = []\n",
"\n",
"while True:\n",
"\n",
" ret, frame = cap.read()\n",
"\n",
" if ret:\n",
" faces = detector.detectMultiScale(frame,1.1,4)\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",
" cv2.imshow(\"My Face\", gray)\n",
" cv2.imshow(\"My Screen\", frame)\n",
" \n",
"\n",
" key = cv2.waitKey(1)\n",
"\n",
" if key == ord(\"q\"):\n",
" break\n",
" if key == ord(\"c\"):\n",
" # cv2.imwrite(name + \".jpg\", frame)\n",
" frames.append(gray.flatten())\n",
" outputs.append([name])\n",
"\n",
"X = np.array(frames)\n",
"y = np.array(outputs)\n",
"\n",
"data = np.hstack([y, X])\n",
"\n",
"f_name = \"face_data.npy\"\n",
"\n",
"if os.path.exists(f_name):\n",
" old = np.load(f_name)\n",
" data = np.vstack([old, data])\n",
"\n",
"np.save(f_name, data)\n",
"\n",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
87 changes: 87 additions & 0 deletions Face_Recognition/Face_Recog.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Required Imports\n",
"import cv2\n",
"import numpy as np\n",
"from sklearn.neighbors import KNeighborsClassifier"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Detecting face\n",
"data = np.load(\"face_data.npy\")\n",
"\n",
"# print(data.shape, data.dtype)\n",
"X = data[:, 1:].astype(int)\n",
"y = data[:, 0]\n",
"model = KNeighborsClassifier()\n",
"model.fit(X, y)\n",
"cap = cv2.VideoCapture(0)\n",
"detector = cv2.CascadeClassifier(\"./datasets/haarcascade_frontalface_default.xml\")\n",
"while True:\n",
"\n",
" ret, frame = cap.read()\n",
"\n",
" if ret:\n",
" faces = detector.detectMultiScale(frame,1.5,4)\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",
" cv2.putText(frame, str(out[0]), (x, y - 10), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 2)\n",
" \n",
" cv2.imshow(\"My Face\", gray)\n",
"\n",
" cv2.imshow(\"My Screen\", frame)\n",
"\n",
"\n",
" key = cv2.waitKey(1)\n",
"\n",
" if key == ord(\"q\"):\n",
" break\n",
"\n",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
22 changes: 22 additions & 0 deletions Face_Recognition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Face_Recognition
This script helps you to recognize faces by a pre-trained ML model.
# Quick Start
- Clone this repository.

git clone https://github.com/geekquad/Image-Processing-OpenCV.git

- Change Directory

cd Image-Processing-OpenCV
cd Face_Recognition

- Run Face_data.ipynb file to collect data for model training

Input title of data.
Press `C` to Capture image and `Q` to exit window.

- Run Face_Recog.ipynb file for image recogntion


# Screenshot
![](datasets/capture.PNG)
Binary file added Face_Recognition/datasets/capture.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading