code: import cv2,os
  import numpy as np
  from PIL import Image
  cam = cv2.VideoCapture(0)
  
  recognizer = cv2.createLBPHFaceRecognizer()
  detector=cv2.CascadeClassifier('frontface.xml')
  
  
  '''
  Below function converts data into yml
  '''
  def getImagesAndLabels(path):
      imagePaths=[os.path.join(path,f) for f in os.listdir(path)] 
      faceSamples=[]
      Ids=[]
      for imagePath in imagePaths:
          pilImage=Image.open(imagePath).convert('L')
          imageNp=np.array(pilImage,'uint8')
          Id=int(os.path.split(imagePath)[-1].split(".")[1])
          faces=detector.detectMultiScale(imageNp)
          for (x,y,w,h) in faces:
              faceSamples.append(imageNp[y:y+h,x:x+w])
              Ids.append(Id)
      return faceSamples,Ids
  
  Id=raw_input('enter your id')
  sampleNum=0
  while(True):
      ret, img = cam.read()
      gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      faces = detector.detectMultiScale(gray, 1.3, 5)
      for (x,y,w,h) in faces:
          #cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) 
          sampleNum=sampleNum+1
          #saving the captured face in the dataset folder
          cv2.imwrite("dataSet/User."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
  
          cv2.imshow('frame',img)
      #wait for 100 miliseconds 
      if cv2.waitKey(100) & 0xFF == ord('q'):
          break
      # break if the sample number is morethan 20
      elif sampleNum>30:
          break
  cam.release()
  faces,Ids = getImagesAndLabels('dataSet')
  recognizer.train(faces, np.array(Ids))
  recognizer.save('trainner/trainner.yml')
  cv2.destroyAllWindows()
  
  
  
apis: numpy.array