Face Detect

parent b14aaf34
{
"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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment