top of page

Face Recognition System in Python Part #1


 

This script uses a python libraries called pillow, numpy and opencv. those can be download the whl file in http://pypi.python.org and install it using pip.

------- here is the code ------

import cv2 #importing the opencv library import numpy as np #importing numpy

faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #importing the cam = cv2.VideoCapture(0) #haar-cascade file and reading the camera idq = input('enter User id') #accepting user id sampleNum=0 #initializing the sample number variable

while True: ret,img=cam.read() #reading the frames gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #converting the frames to gray scale faces=faceDetect.detectMultiScale(gray,1.3,5) #detecting faces in the frame for x,y,w,h in faces: sampleNum=sampleNum+1 #increment the sample number tesfa = "dataset/user."+str(idq)+"." +str(sampleNum) + ".jpg" #declare the file name cv2.imwrite(tesfa,gray[y:y+h, x:x+w]) #writing the image file cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2) #drawing a rectangle around the face cv2.waitKey(100) #wait 100 millisecond

cv2.imshow("Face",img) #display the frame if sampleNum>20: #decide the number of samples you want break #break if it exceeds the your desired number

cam.release() #release the camera cv2.destroyAllWindows() #destroy all the windows


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Recent Posts
Search By Tags
No tags yet.
Connect
  • Google+ Social Icon
  • Facebook Social Icon
  • LinkedIn Social Icon
  • Twitter Social Icon
bottom of page