Reader small image

You're reading from  Python Robotics Projects

Product typeBook
Published inMay 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788832922
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Prof. Diwakar Vaish
Prof. Diwakar Vaish
author image
Prof. Diwakar Vaish

Prof. Diwakar Vaish is a robotics scientist and the inventor of Manav (India's first indigenous humanoid robot), the world's first mind-controlled wheelchair, brain cloning, and the world's cheapest ventilator. He has also been a guest lecturer at over 13 IITs and various other institutions. He is the founder of A-SET Robotics, a leading robotics research company based in New Delhi.
Read more about Prof. Diwakar Vaish

Right arrow

Recognizing the face


Now that we have learned how to make our system learn, it's time to use that learned data and recognize the face. So without much talking, let's go ahead and understand how this would be done:

import numpy as np
import cv2

faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cam = cv2.VideoCapture(0)
rec = cv2.face.LBPHFaceRecognizer_create()

rec.read("recognizer/trainningData.yml")
id = 0
font = cv2.FONT_HERSHEY_SIMPLEX

while True:

   ret, img = cam.read()
   gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
   faces = faceDetect.detectMultiScale(gray,1.3,5)

   for (x,y,w,h) in faces:
       cv2.rectangle(img, (x,y), (x+w, y+h), (0,0,255), 2)
       id, conf = rec.predict(gray[y:y+h, x:x+w])

       if id==1:
           id = "BEN"
       cv2.putText(img, str(id), (x,y+h),font,2, (255,0,0),1,)

    cv2.imshow("face", img)

    if cv2.waitKey(1)==ord('q'):
       break

cam.release()
cv2.destroyAllWindows()

In this code, there are not a lot of new things...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Python Robotics Projects
Published in: May 2018Publisher: PacktISBN-13: 9781788832922

Author (1)

author image
Prof. Diwakar Vaish

Prof. Diwakar Vaish is a robotics scientist and the inventor of Manav (India's first indigenous humanoid robot), the world's first mind-controlled wheelchair, brain cloning, and the world's cheapest ventilator. He has also been a guest lecturer at over 13 IITs and various other institutions. He is the founder of A-SET Robotics, a leading robotics research company based in New Delhi.
Read more about Prof. Diwakar Vaish