Book Image

Python Robotics Projects

By : Prof. Diwakar Vaish
Book Image

Python Robotics Projects

By: Prof. Diwakar Vaish

Overview of this book

Robotics is a fast-growing industry. Multiple surveys state that investment in the field has increased tenfold in the last 6 years, and is set to become a $100-billion sector by 2020. Robots are prevalent throughout all industries, and they are all set to be a part of our domestic lives. This book starts with the installation and basic steps in configuring a robotic controller. You'll then move on to setting up your environment to use Python with the robotic controller. You'll dive deep into building simple robotic projects, such as a pet-feeding robot, and more complicated projects, such as machine learning enabled home automation system (Jarvis), vision processing based robots and a self-driven robotic vehicle using Python. By the end of this book, you'll know how to build smart robots using Python.
Table of Contents (24 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

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...