Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021_123
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2021_123
2021_123
Commits
9094370c
Commit
9094370c
authored
Jul 01, 2021
by
IT18121384 Thilakarathna U.G.C.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Face Detect
parent
b14aaf34
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
177 additions
and
0 deletions
+177
-0
Face.ipynb
Face.ipynb
+177
-0
No files found.
Face.ipynb
0 → 100644
View file @
9094370c
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "3b9ee3f5",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e83447d",
"metadata": {},
"outputs": [],
"source": [
"def assure_path_exists(path):\n",
" dir = os.path.dirname(path)\n",
" if not os.path.exists(dir):\n",
" os.makedirs(dir)\n",
" \n",
"face_id = input('enter Id')\n",
"vid_cam = cv2.VideoCapture(0)\n",
"face_detector = cv2.CascadeClassifier(\"C:/Users/Thila/AppData/Local/Programs/Python/Python39/Scripts/face/haarcascade_frontalface_default.xml\")\n",
"count = 0\n",
"assure_path_exists(\"C:/Users/Thila/AppData/Local/Programs/Python/Python39/Scripts/face/data\")\n",
"\n",
"while True:\n",
" _, image_frame = vid_cam.read()\n",
" gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY)\n",
" faces = face_detector.detectMultiScale(gray,1.3,5)\n",
" for (x,y,w,h) in faces:\n",
" cv2.rectangle(image_frame, (x, y), (x + w, y + h), (255, 0, 0),2)\n",
" count +=1\n",
" cv2.imwrite(\"C:/Users/Thila/AppData/Local/Programs/Python/Python39/Scripts/face/data/User.\" + str(face_id) + \".\" + str(count) + \".jpg\", gray [y:y+h, x:x+w])\n",
" cv2.imshow(\"frame\", image_frame)\n",
" \n",
" if cv2.waitKey(1)==13 or int(count)==100:\n",
" break\n",
" \n",
" elif count >=50:\n",
" print(\"Suceesfully Captured\")\n",
" break\n",
" \n",
"vid_cam.release()\n",
"cv2.destroyAllWindows()\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8c70d36",
"metadata": {},
"outputs": [],
"source": [
"import os, cv2\n",
"import numpy as np\n",
"from PIL import Image"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4416305d",
"metadata": {},
"outputs": [],
"source": [
"recognizer = cv2.face.LBPHFaceRecognizer_create()\n",
"detector = cv2.CascadeClassifier(\"C:/Users/Thila/AppData/Local/Programs/Python/Python39/Scripts/face/haarcascade_frontalface_default.xml\")\n",
"\n",
"def getImagesAndLabels(path):\n",
" imagePaths=[os.path.join(path,f) for f in os.listdir(path)]\n",
" faceSamples=[]\n",
" Ids=[]\n",
" for imagePath in imagePaths:\n",
" pilImage=Image.open(imagePath).convert('L')\n",
" imageNp=np.array(pilImage,'uint8')\n",
" Id=int(os.path.split(imagePath)[-1].split(\".\")[1])\n",
" faces=detector.detectMultiScale(imageNp)\n",
" for(x,y,w,h) in faces:\n",
" faceSamples.append(imageNp[y:y+h,x:x+w])\n",
" Ids.append(Id)\n",
" return faceSamples,Ids\n",
"\n",
"faces,Ids = getImagesAndLabels(\"C:/Users/Thila/AppData/Local/Programs/Python/Python39/Scripts/face/data\")\n",
"s = recognizer.train(faces, np.array(Ids))\n",
"print(\"Successfully trained\")\n",
"recognizer.write(\"C:/Users/Thila/AppData/Local/Programs/Python/Python39/Scripts/face/trainer.yml\")\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c2cc4f2",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "343b84ba",
"metadata": {},
"outputs": [],
"source": [
"face_detector =cv2.CascadeClassifier(\"C:/Users/Thila/AppData/Local/Programs/Python/Python39/Scripts/face/haarcascade_frontalface_default.xml\")\n",
"cap = cv2.VideoCapture(0)\n",
"recognizer = cv2.face.LBPHFaceRecognizer_create()\n",
"recognizer.read(\"C:/Users/Thila/AppData/Local/Programs/Python/Python39/Scripts/face/trainer.yml\")\n",
"id =0\n",
"font = cv2.FONT_HERSHEY_SIMPLEX\n",
"while True:\n",
" ret, img = cap.read()\n",
" gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
" faces = face_detector.detectMultiScale(gray,1.3,5)\n",
" for(x,y,w,h) in faces:\n",
" roi_gray =gray[y:y+h, x:x+w]\n",
" cv2.rectangle(img, (x,y), (x+w, y+h),(255,0,0),2)\n",
" id, conf = recognizer.predict(roi_gray)\n",
" if(id==1):\n",
" id=\"Gihan\"\n",
" elif(id==2):\n",
" id=\"Gayumi\"\n",
" else:\n",
" id= \"Unknow face\"\n",
" cv2.putText(img, str(id) + \"\" + str(conf), (x, y - 10), font, 0.55, (120,255,120),1)\n",
" #cv2.cv.PutText(cv2.cv.fromarray(img),str(id),(x,y+h),font,255)\n",
" \n",
" cv2.imshow(\"frame\", img)\n",
" if cv2.waitKey(100) & 0xFF == ord(\"q\"):\n",
" break\n",
"vid_cam.release()\n",
"cv2.destroyAllWindows()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdfb3133",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment